Recently, I wrote about a mechanism to identify the type of device on which your application is running.  My original solution utilized a custom unmanaged function to provide a simplified, type safe way to call the SystemParametersInfo function.

Some of the folks here commented that, while my method worked and was valid, it was sub-optimal since your application would need to ship an unmanaged DLL.  Since this DLL would have to be built for every processor type, it was not that much easier than having a separate application for each device type.

I whole heartedly agreed with their feedback, so I am revisiting my example today.

In my new example, I am taking advatage of the DllImportAttribute's EntryPoint field to allow me to rename the function in my P/Invoke signature.  Since, as mentioned last time, SystemParametersInfo is not a type safe function, I have created a string specific version, as shown below.

[DllImport("coredll.dll", EntryPoint="SystemParametersInfo", SetLastError = true)]
private extern static Boolean GetSystemParameterString(SystemParameters sysParam,
                                                       UInt32 bufferSize,
                                                       StringBuilder stringBuffer,
                                                       Boolean updateWinIni);


I then write a simple, managed method to identify the device type.  This method, in turn, calls SystemParametersInfo via my GetSystemParameterString P/Invoke.

// call our wrapper function
public static DeviceType GetDeviceType()
{
    DeviceType devType;

    StringBuilder sb = new StringBuilder(128);

    // ask the operating system for the device type
    Boolean success = GetSystemParameterString(SystemParameters.GetPlatformType,
                                               // SystemParametersInfo expects the buffer size in bytes
                                               (UInt32)(sb.Capacity * sizeof(char)),
                                               sb, false);

    // compare strings as lowercase
    String str = sb.ToString().ToLower();
    switch(str)
    {
        case "smartphone":
            devType = DeviceType.Smartphone;
            break;

        case "pocketpc":
            devType = DeviceType.PocketPC;
            break;

        default:
            devType = DeviceType.WindowsCE;
            break;
    }

    return devType;
}


The GetDeviceType method described above uses the following enums to specify the system parameter and to report the type of device.  I have limited the enum values to clarify the example.

enum SystemParameters : uint
{
    GetPlatformType = 257

    // TODO: Add additional SPI_* values (from winuser.h) here
}


enum DeviceType : int
{
    WindowsCE = 0,
    PocketPC = 1,
    Smartphone = 2
}


My application's Main method can make a very simple call to determine the type of device.  I have made this method static to simplify this example.

static Main(String[] args)
{
    DeviceType devType = GetDeviceType();
    MessageBox.Show(devType.ToString());
}


Enjoy!
-- DK

Disclaimer(s):
This posting is provided "AS IS" with no warranties, and confers no rights.