Share via


HOW TO:取得裝置 ID 和名稱

更新:2007 年 11 月

若要取得裝置的名稱,請使用 Dns.GetHostName 屬性。一般的預設名稱會是 "PocketPC"。

範例

在這個範例中,當表單載入時,會在訊息方塊中顯示裝置 ID 和名稱。

若要取得裝置 ID 或序號,您必須使用平台叫用存取原生 (Native) Windows CE KernelIoControl 函式。

Imports System
Imports System.Drawing
Imports System.ComponentModel
Imports System.Diagnostics
Imports System.Windows.Forms
Imports System.Runtime.InteropServices
Imports System.Text
Imports Microsoft.VisualBasic

Public Class DeviceID
    Inherits System.Windows.Forms.Form

    Declare Function KernelIoControl Lib "CoreDll.dll" _
        (ByVal dwIoControlCode As Int32, _
        ByVal lpInBuf As IntPtr, _
        ByVal nInBufSize As Int32, _
        ByVal lpOutBuf() As Byte, _
        ByVal nOutBufSize As Int32, _
        ByRef lpBytesReturned As Int32) As Boolean

    Public Sub New()

        Me.Text = "DeviceID"

        ' Display OK close button.
        Me.MinimizeBox = False

    End Sub

    Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
        MyBase.Dispose(disposing)
    End Sub

    Shared Sub Main()
        Application.Run(New DeviceID)
    End Sub 

    Private Shared METHOD_BUFFERED As Int32 = 0
    Private Shared FILE_ANY_ACCESS As Int32 = 0
    Private Shared FILE_DEVICE_HAL As Int32 = &H101

    Private Const ERROR_NOT_SUPPORTED As Int32 = &H32
    Private Const ERROR_INSUFFICIENT_BUFFER As Int32 = &H7A

    Private Shared IOCTL_HAL_GET_DEVICEID As Int32 = _
        (&H10000 * FILE_DEVICE_HAL) Or (&H4000 * FILE_ANY_ACCESS) _
        Or (&H4 * 21) Or METHOD_BUFFERED

    Private Shared Function GetDeviceID() As String

        ' Initialize the output buffer to the size of a 
        ' Win32 DEVICE_ID structure 
        Dim outbuff(19) As Byte
        Dim dwOutBytes As Int32
        Dim done As Boolean = False

        Dim nBuffSize As Int32 = outbuff.Length

        ' Set DEVICEID.dwSize to size of buffer.  Some platforms look at
        ' this field rather than the nOutBufSize param of KernelIoControl
        ' when determining if the buffer is large enough.
        BitConverter.GetBytes(nBuffSize).CopyTo(outbuff, 0)
        dwOutBytes = 0

        ' Loop until the device ID is retrieved or an error occurs.
        While Not done
            If KernelIoControl(IOCTL_HAL_GET_DEVICEID, IntPtr.Zero, _
                0, outbuff, nBuffSize, dwOutBytes) Then
                done = True
            Else
                Dim errnum As Integer = Marshal.GetLastWin32Error()
                Select Case errnum
                    Case ERROR_NOT_SUPPORTED
                        Throw New NotSupportedException( _
                            "IOCTL_HAL_GET_DEVICEID is not supported on this device", _
                            New Win32Exception(errnum))

                    Case ERROR_INSUFFICIENT_BUFFER

                        ' The buffer is not big enough for the data.  The
                        ' required size is in the first 4 bytes of the output 
                        ' buffer (DEVICE_ID.dwSize).
                        nBuffSize = BitConverter.ToInt32(outbuff, 0)
                        outbuff = New Byte(nBuffSize) {}

                        ' Set DEVICEID.dwSize to size of buffer.  Some
                        ' platforms look at this field rather than the
                        ' nOutBufSize param of KernelIoControl when
                        ' determining if the buffer is large enough.
                        BitConverter.GetBytes(nBuffSize).CopyTo(outbuff, 0)

                    Case Else
                        Throw New Win32Exception(errnum, "Unexpected error")
                End Select
            End If
        End While

        ' Copy the elements of the DEVICE_ID structure.
        Dim dwPresetIDOffset As Int32 = BitConverter.ToInt32(outbuff, &H4)
        Dim dwPresetIDSize As Int32 = BitConverter.ToInt32(outbuff, &H8)
        Dim dwPlatformIDOffset As Int32 = BitConverter.ToInt32(outbuff, &HC)
        Dim dwPlatformIDSize As Int32 = BitConverter.ToInt32(outbuff, &H10)
        Dim sb As New StringBuilder
        Dim i As Integer

        For i = dwPresetIDOffset To (dwPresetIDOffset + dwPresetIDSize) - 1
            sb.Append(String.Format("{0:X2}", outbuff(i)))
        Next i

        sb.Append("-")

        For i = dwPlatformIDOffset To (dwPlatformIDOffset + dwPlatformIDSize) - 1
            sb.Append(String.Format("{0:X2}", outbuff(i)))
        Next i

        Return sb.ToString()
    End Function

    Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Try
            ' Show the device ID.
            Dim deviceID As String = GetDeviceID()
            MessageBox.Show("Device ID: " + deviceID)

            ' Show the device name.
            Dim deviceName As String = System.Net.Dns.GetHostName()
            MessageBox.Show("Device Name: " & deviceName)

        Catch ex As Exception
            MessageBox.Show(ex.Message.ToString())
        End Try
    End Sub
End Class
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Diagnostics;
using System.Windows.Forms;
using System.Runtime.InteropServices;
using System.Text;

namespace DeviceID
{
    /// <summary>
    /// Summary description for DeviceID.
    /// </summary>
    public class DeviceID : System.Windows.Forms.Form
    {

    public DeviceID()
        {
            //
            // Required for Windows Form Designer support
            //
            InitializeComponent();

            //
            // TODO: Add any constructor code after InitializeComponent call
            //
        }

        /// <summary>
        /// Clean up any resources being used.
        /// </summary>
        protected override void Dispose( bool disposing )
        {
            base.Dispose( disposing );
        }

        #region Windows Form Designer generated code
        /// <summary>
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// </summary>
        private void InitializeComponent()
        {
            // 
            // DeviceID
            // 
            this.Text = "DeviceID";
            this.Load += new System.EventHandler(this.DeviceID_Load);

        }
        static void Main() 
        {
            Application.Run(new DeviceID());
        }
        #endregion

        private static Int32 METHOD_BUFFERED = 0;
        private static Int32 FILE_ANY_ACCESS = 0;
        private static Int32 FILE_DEVICE_HAL = 0x00000101;

        private const Int32 ERROR_NOT_SUPPORTED = 0x32;
        private const Int32 ERROR_INSUFFICIENT_BUFFER = 0x7A;

        private static Int32 IOCTL_HAL_GET_DEVICEID = 
            ((FILE_DEVICE_HAL) << 16) | ((FILE_ANY_ACCESS) << 14) 
            | ((21) << 2) | (METHOD_BUFFERED);

        [DllImport("coredll.dll", SetLastError=true)]
        private static extern bool KernelIoControl(Int32 dwIoControlCode, 
            IntPtr lpInBuf, Int32 nInBufSize, byte[] lpOutBuf, 
            Int32 nOutBufSize, ref Int32 lpBytesReturned);

        private static string GetDeviceID()
        {
            // Initialize the output buffer to the size of a 
            // Win32 DEVICE_ID structure.
            byte[] outbuff = new byte[20];
            Int32  dwOutBytes;
            bool done = false;

            Int32 nBuffSize = outbuff.Length;

            // Set DEVICEID.dwSize to size of buffer.  Some platforms look at
            // this field rather than the nOutBufSize param of KernelIoControl
            // when determining if the buffer is large enough.
            BitConverter.GetBytes(nBuffSize).CopyTo(outbuff, 0);  
            dwOutBytes = 0;

            // Loop until the device ID is retrieved or an error occurs.
            while (! done)
            {
                if (KernelIoControl(IOCTL_HAL_GET_DEVICEID, IntPtr.Zero, 
                    0, outbuff, nBuffSize, ref dwOutBytes))
                {
                    done = true;
                }
                else
                {
                    int error = Marshal.GetLastWin32Error();
                    switch (error)
                    {
                    case ERROR_NOT_SUPPORTED:
                        throw new NotSupportedException(
                            "IOCTL_HAL_GET_DEVICEID is not supported on this device",
                            new Win32Exception(error));

                    case ERROR_INSUFFICIENT_BUFFER:

                        // The buffer is not big enough for the data.  The
                        // required size is in the first 4 bytes of the output
                        // buffer (DEVICE_ID.dwSize).
                        nBuffSize = BitConverter.ToInt32(outbuff, 0);
                        outbuff = new byte[nBuffSize];

                        // Set DEVICEID.dwSize to size of buffer.  Some
                        // platforms look at this field rather than the
                        // nOutBufSize param of KernelIoControl when
                        // determining if the buffer is large enough.
                        BitConverter.GetBytes(nBuffSize).CopyTo(outbuff, 0);
                        break;

                    default:
                        throw new Win32Exception(error, "Unexpected error");
                    }
                }
            }

            // Copy the elements of the DEVICE_ID structure.
            Int32 dwPresetIDOffset = BitConverter.ToInt32(outbuff, 0x4);
            Int32 dwPresetIDSize = BitConverter.ToInt32(outbuff, 0x8);
            Int32 dwPlatformIDOffset = BitConverter.ToInt32(outbuff, 0xc);
            Int32 dwPlatformIDSize = BitConverter.ToInt32(outbuff, 0x10);
            StringBuilder sb = new StringBuilder();

            for (int i = dwPresetIDOffset; 
                i < dwPresetIDOffset + dwPresetIDSize; i++)
            {
                sb.Append(String.Format("{0:X2}", outbuff[i]));
            }

            sb.Append("-");

            for (int i = dwPlatformIDOffset; 
                i < dwPlatformIDOffset + dwPlatformIDSize; i ++ )  
            {
                sb.Append( String.Format("{0:X2}", outbuff[i]));
            }
            return sb.ToString();
        }

        private void DeviceID_Load(object sender, System.EventArgs e)
        {
            try 
            {
                // Show the device ID.
                string strDeviceID = GetDeviceID();
                MessageBox.Show("Device ID: " + strDeviceID);

                // Show the device name.
                string deviceName = System.Net.Dns.GetHostName();
                MessageBox.Show("Device Name: " + deviceName);
            }

            catch (Exception ex)
            {
                MessageBox.Show(ex.Message.ToString());
            }
        }
    }
}

編譯程式碼

這個範例需要參考下列命名空間:

穩固程式設計

下表列出原生 KernelIoControl 函式參數,全部皆為 32 位元。

參數

Win32 型別

Managed 型別

一般值

dwIoControlCode

DWORD

Int32

IOCTL_HAL_GET_DEVICEID

lpInBuf

LPVOID

IntPtr

IntPtr.Zero (不需要輸入資料)

nInBufSize

DWORD

Int32

0 (不需要輸入資料)

lpOutBuf

LPVOID*

Int32

20 個元素的 Byte 陣列 (20 個位元組為 DEVICE_ID 結構的大小)

nOutBufSize

DWORD

Int32

20

lpBytesReturned

LPWORD

refInt32

0

lpOutBuf 參數的結構如下:

Structure DEVICE_ID
    Private dwSize As Integer
    Private dwPresetIDOffset As Integer
    Private dwPresetIDBytes As Integer
    Private dwPlatformIDOffset As Integer
    Private dwPlatformIDBytes As Integer
End Structure
struct DEVICE_ID
{
    int dwSize;
    int dwPresetIDOffset;
    int dwPresetIDBytes;
    int dwPlatformIDOffset;
    int dwPlatformIDBytes;
}

傳回值和錯誤處理

如果裝置 ID 已經複製到輸出緩衝區,KernelIoControl 函式會傳回 true,否則會傳回 false。如果 KernelIoControl 失敗,請呼叫 Managed GetLastWin32Error 方法,以取得 Win32 錯誤碼。錯誤碼可能為下列其中一個:

  • ERROR_NOT_SUPPORTED:表示裝置不會實作 IOCTL_HAL_GET_DEVICEID 控制碼。

  • ERROR_INSUFFICIENT_BUFFER:表示輸出緩衝區不夠大,無法存放裝置 ID。需要的位元組數由 DEVICE_ID 結構中的 dwSize 指定,會回傳在輸出緩衝區的前 4 個位元組中。如果發生這個錯誤,請依照 dwSize 指定的大小重新配置緩衝區,並再次呼叫 KernelIoControl

請參閱

工作

HOW TO:取得裝置記憶體

其他資源

.NET Compact Framework 中的互通性