Determining the type of a device at runtime II: A single binary solution

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();
        case “smartphone”:
            devType = DeviceType.Smartphone;

        case “pocketpc”:
            devType = DeviceType.PocketPC;

            devType = DeviceType.WindowsCE;

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

— DK

This posting is provided “AS IS” with no warranties, and confers no rights.

Comments (2)

  1. James says:


    Interesting article.  I was wondering if one is able to determine the brand of the device programmatically as well?  Like iPaq model 122, etc…

  2. David Kline says:

    I started this series last year and thought I would continue the tradition with my best of 2006 collection.