Search code examples
c#.net-3.5windows-mobile

Trouble implementing GetDeviceUniqueID on Windows Mobile 6


I'm using code very similar to the following Stack Overflow question: Can't find an Entry Point 'GetDeviceUniqueID' in a PInvoke DLL 'coredll.dll'

(My code pasted here for posterity's sake):

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



public static string GetDeviceID()
{
    string appString = "MyApp";
    byte[] appData = new byte[appString.Length];
    for (int count = 0; count < appString.Length; count++)
    {
        appData[count] = (byte)appString[count];
    }

    int appDataSize = appData.Length;
    byte[] DeviceOutput = new byte[20];
    uint SizeOut = 20;
    int i_rc = GetDeviceUniqueID(appData, appDataSize, 1, DeviceOutput, out SizeOut);

    string idString = "";
    for (int i = 0; i < DeviceOutput.Length; i++)
    {
        if (i == 4 || i == 6 || i == 8 || i == 10)
            idString = String.Format("{0}-{1}", idString, DeviceOutput[i].ToString("x2"));
        else
            idString = String.Format("{0}{1}", idString, DeviceOutput[i].ToString("x2"));
    }
    return idString;


}

I have no issues compiling the program on my emulator and deploying to my physical device. However, this code always returns a value of: "00000000-0000-0000-0000-00000000000000000000".

(Upon introspection, i_rc has a value of 2147024809).

What is going wrong? Why does the function seem to return a default/safe value?


Solution

  • Turns out that the app_data variable should be at least 8 characters long.

    (As prescribed by - web.archive.org)

    Maddening, as this rather crucial set of requirements doesn't appear to be documented anywhere.

    Bonus - I'll list the other requirements here in case we lose the above site to the sands of time:

    HRESULT GetDeviceUniqueID(LPBYTE pbApplicationData, DWORD cbApplictionData,
                              DWORD dwDeviceIDVersion, LPBYTE pbDeviceIDOutput,
                              DWORD *pcbDeviceIDOutput);
    
    • pbApplicationData - must be 8 characters in length otherwise the API will fail
    • dwDeviceIDVersion - should be 1
    • pbDeviceIDOutput - an out parameter which will be filled with the device specific unique id
    • pcbDeviceIDOutput - the length of pbDeviceIDOutput