MSDN Library

How to: Get the Device ID and Name

[This documentation is for preview only, and is subject to change in later releases. Blank topics are included as placeholders.]

To get the name of the device, use the Dns.GetHostName property. A typical default name is "PocketPC".

This example displays the ID and name of the device in messages boxes when the form loads.

To get the device ID, or serial number, you must use platform invoke to access the native Windows CE KernelIoControl function.

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)
    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
                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


        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
            ' 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
        End Try
    End Sub
End Class

The following table lists the native KernelIoControl function parameters. All are 32 bits.


Win32 type

Managed type

Typical value








IntPtr.Zero (no input data required)




0 (no input data required)




A 20-element Byte array (20 bytes is the size of the DEVICE_ID structure)







ref Int32


The lpOutBuf parameter has the following structure:

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

Return Values and Error Handling

The KernelIoControl function returns true if the device ID was copied to the output buffer; otherwise, it returns false. If KernelIoControl fails, call the managed GetLastWin32Error method to get the Win32 error code. The error code is likely to be either of the following:

  • ERROR_NOT_SUPPORTED - Indicates that the device does not implement the IOCTL_HAL_GET_DEVICEID control code.

  • ERROR_INSUFFICIENT_BUFFER - Indicates that the output buffer was not large enough to hold the device ID. The required number of bytes, specified by dwSize in the DEVICE_ID structure, is returned in the first four bytes of the output buffer. If this error occurs, reallocate the output buffer to the size specified by dwSize and call KernelIoControl again.

© 2016 Microsoft