Pages

Hello geeks !!

Welcome.

This blog gives you several interesting information related to application development for smartphones such as Windows Mobile, iPhone, Android.

You may find pieces of code that might be of help to understand the concept that is being explained.

Please feel free to post back your questions, comments and suggestions.



Friday, December 4, 2009

Identify the Windows Platform Version

Its not as tough as it would seem. It's quite simple.

The coredll.dll exposes this function.
SystemParametersInfo(uint uiAction, uint uiParam, StringBuilder pvParam, uint fWiniIni)


The first parameter specifies what System Parameter you want to access.

With respect to the Platform, there are two parameters that can be sent.

SPI_GETPLATFORMTYPE = 257;
SPI_GETOEMINFO = 258;



You need to pass an empty StringBuilder object which would be your 'out' parameter.

Environment.OSVersion.Platform property gives a value which identifies the platform as either 'WinCE' or 'Desktop'.

Check this value.

If its WinCE, then, if 'PlatformType' returns 'SmartPhone' then its a Windows Mobile Standard device.

If its WinCE, and 'PlatformType' returns 'PocketPC', then,
      If "Windows\Phone.dll" exists, then its a WM Professional device.
      Else, its a WM Classic device.

Also, passing 'OEMInfo' to the function, returns whether the platform is an emulator or not.

Here's a sample piece of code with comment blocks.



#region Platform Invokes
    private static class NativeMethods
    {
      [DllImport("coredll.dll")]
      private static extern int SystemParametersInfo(uint uiAction, uint uiParam, StringBuilder pvParam, uint fWiniIni);


      private const uint SPI_GETPLATFORMTYPE = 257;
      private const uint SPI_GETOEMINFO = 258;


      private static string GetSystemParameter(uint uiParam)
      {
        StringBuilder sb = new StringBuilder(128);
        if (SystemParametersInfo(uiParam, (uint)sb.Capacity, sb, 0) == 0)
          throw new ApplicationException("Failed to get system parameter");
        return sb.ToString();
      }


      public static string GetPlatformType()
      {
        return GetSystemParameter(SPI_GETPLATFORMTYPE);
      }


      public static string GetOEMInfo()
      {
        return GetSystemParameter(SPI_GETOEMINFO);
      }
      
      // Returns true if the device has a cellular radio
      public static bool GetHasRadio()
      {
        return File.Exists(@"\Windows\Phone.dll");
      }
    }
    #endregion


    // Returns true if the application is running on a
    // desktop version of the Windows operating system
    public static bool IsDesktopWindows
    {
      get { return Environment.OSVersion.Platform != PlatformID.WinCE;  }
    }


    // Returns true if the application is running on a
    // device powered by some form of WIndows CE based
    // operating system
    public static bool IsWindowsCE
    {
      get { return Environment.OSVersion.Platform == PlatformID.WinCE; }
    }


    // Returns true if the application is running on a
    // Windows Mobile Standard (i.e. Smartphone) device
    public static bool IsWindowsMobileStandard
    {
      get { return IsWindowsCE && (NativeMethods.GetPlatformType() == "SmartPhone"); }
    }


    // Returns true if the application is running on a
    // Windows Mobile Classic (i.e. Pocket PC without
    // cellphone) device.
    public static bool IsWindowsMobileClassic
    {
      get { return IsWindowsCE && !NativeMethods.GetHasRadio() && (NativeMethods.GetPlatformType() == "PocketPC"); }
    }


    // Returns true if the application is running on a
    // Windows Mobile Professional (i.e. Pocket PC with
    // cellphone) device.
    public static bool IsWindowsMobileProfessional
    {
      get { return IsWindowsCE && NativeMethods.GetHasRadio() && (NativeMethods.GetPlatformType() == "PocketPC"); }
    }


    // Returns true if the application is running
    // within the Device Emulator on a desktop machine
    public static bool IsEmulator
    {
      get { return IsWindowsCE && (NativeMethods.GetOEMInfo() == "Microsoft DeviceEmulator"); }
    }

No comments:

Post a Comment