Platform detection II: Is your app running on Smartphone or Pocket PC?

While both Smartphones and Pocket PCs are based on Windows Mobile, there are some very important differences for developers who are targeting both platforms.  Not the least of which are lack of LinkButtons and other clickable elements on Smartphones (since they have no touch screen).  In this post I show how to detect whether your app is running on a Smartphone or a Pocket PC style Windows Mobile device.

It is a simple call to SystemParametersInfo to find whether you are running on a Smartphone or a Pocket PC.  You need a few constants defined and the P/Invoke marshaling code to make it work from managed code however.

This post builds on the first post in the Platform Detection series.  You'll need to build on the code from that post for this post to compile and run.  Like the last post, this code uses partial classes, so you can copy and paste the previous post's code into one file and this code into another (removing the first post's Main method) and your project will compile and run.

 using System;
using System.IO;
using System.Windows.Forms;
using Microsoft.Win32;
using System.Runtime.InteropServices;
using System.Text;

namespace PlatformDetection
{
    internal partial class PInvoke
    {
        public static string GetPlatformType()
        {
            StringBuilder platformType = new StringBuilder(50);
            if (SystemParametersInfo4Strings((uint)SystemParametersInfoActions.SPI_GETPLATFORMTYPE,
                (uint)platformType.Capacity, platformType, 0) == 0)
                throw new Exception("Error getting platform type.");
            return platformType.ToString();
        }
    }
    internal partial class PlatformDetection
    {
        public static bool IsSmartphone()
        {
            return PInvoke.GetPlatformType() == "SmartPhone";
        }
        public static bool IsPocketPC()
        {
            return PInvoke.GetPlatformType() == "PocketPC";
        }
    }
    class PlatformProgram
    {
        static void Main(string[] args)
        {
            string platform;
            if (PlatformDetection.IsSmartphone())
                platform = "Smartphone";
            else if (PlatformDetection.IsPocketPC())
                platform = "Pocket PC";
            else
                platform = "Other WinCE";
            MessageBox.Show("Platform: " + platform);
        }
    }

}

This is the second post in a series of three on platform detection.  Coming up next: detecting the presence of a touch screen.

Comments