Thursday, August 5, 2010

Windows Mobile - C# - Get Device ID


/*********************************************************************
Description: C# implementation of Windows Mobile to get DEVICE ID of the Smart Phone.
*********************************************************************/



using System.Runtime.InteropServices;

[DllImport("coredll.dll")]
private extern static int GetDeviceUniqueID(
    [In, Out] byte[] applicationdata,
    UInt32 cbApplictionData,
    UInt32 dwDeviceIDVersion,
    [In, Out] byte[] deviceIDOuput,
    [In, Out] uint pcbDeviceIDOutput);

// refer http://msdn.microsoft.com/en-us/library/aa908781.aspx for more details

private string GetDeviceID(string applicationName)
{
    byte[] applicationData = new byte[applicationName.Length];

    for (int i = 0; i < applicationName.Length; i++)
        applicationData[i] = (byte)applicationName[i];

    byte[] deviceIDOut = new byte[20]; // should be minimum of 20
    uint sizeID = 20; // should be minimum of 20

        GetDeviceUniqueID(applicationData, (UInt32)applicationData.Length, 1, deviceIDOut, sizeID);

        return BitConverter.ToString(deviceIDOut); // use this to return in the form of xx-xx-xx....
        // return BitConverter.ToString(deviceIDOut).Replace("-", ""); // use this to return in the form of xxxxxx....
}

// usage
string strDevID = GetDeviceID("Your Application Name");



1 comment: