Thursday, March 26, 2009

Getting Windows Mobile Device ID

I got email today asking me how to get the device ID from a pocket pc with vb

Imports System.Text
Public Class Form1
    <System.Runtime.InteropServices.DllImport("coredll.dll")> _
Private Shared Function GetDeviceUniqueID(ByVal appdata As Byte(), ByVal cbApplictionData As Integer, ByVal dwDeviceIDVersion As Integer, ByVal deviceIDOuput As Byte(), ByRef pcbDeviceIDOutput As Integer) As Integer
    End Function
    Private Function GetDeviceId(ByVal appData As String) As Byte()
        Dim appDataBytes As Byte() = System.Text.Encoding.ASCII.GetBytes(appData)
        Dim outputSize As Integer = 20
        Dim output(19) As Byte
        Dim result As Integer = GetDeviceUniqueID(appDataBytes, appDataBytes.Length, 1, output, outputSize)
        Return output
    End Function
    Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Dim sbId As New StringBuilder
        Dim bID() As Byte = GetDeviceId("MyAppName")
        For Each b In bID
            sbId.Append(String.Format("{0:x2}", b))
    End Sub
End Class