⌘BABAKOV.NET Let's code!
100% [dark] [ru]

Determining the Platform in .NET CF on a Mobile Device C#

I needed to automatically determine which version of Win CE is installed on a device, and I was surprised by the end result, but I'll get to that later. First, I'll describe the method for obtaining platform information… We'll get the information using the API function SystemParametersInfo, nothing special, I'll skip to the example right away.

Here's the complete listing of the program:

using System;
using System.Linq;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;

namespace MobileVerOS
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        [DllImport("coredll.dll")]
        private static extern bool SystemParametersInfo(
            int uiAction,
            int uiParam,
            StringBuilder pvParam,
            int fWinIni);

        private static int SPI_GETPLATFORMTYPE = 257;

        //Определяем платформу, вообще ли это мобильное устройство?
        public static Platform GetPlatform()
        {
            Platform plat = Platform.Unknown;
            switch (System.Environment.OSVersion.Platform)
            {
                case PlatformID.Win32NT:
                    plat = Platform.Win32NT;
                    break;
                case PlatformID.Win32S:
                    plat = Platform.Win32S;
                    break;
                case PlatformID.Win32Windows:
                    plat = Platform.Win32Windows;
                    break;
                case PlatformID.WinCE:
                    plat = CheckWinCEPlatform();
                    break;
            }

            return plat;
        }

        //Определяем смартфон или нет?
        static Platform CheckWinCEPlatform()
        {
            Platform plat = Platform.WindowsCE;
            StringBuilder strbuild = new StringBuilder(200);
            SystemParametersInfo(SPI_GETPLATFORMTYPE, 200, strbuild, 0);
            string str = strbuild.ToString();
            switch (str)
            {
                case "PocketPC":
                    plat = Platform.PocketPC;
                    break;
                case "SmartPhone":
                    // PInvoke возвращает "SmartPhone" с заглавной P
                    // однако правильно "Smartphone" с маленькой p.
                    plat = Platform.Smartphone;
                    break;
            }
            return plat;
        }

        private void menuItem1_Click(object sender, EventArgs e)
        {
            this.Dispose();
            Application.Exit();
        }

        private void menuItem2_Click(object sender, EventArgs e)
        {
            try
            {
                label1.Text = "Платформа: " + GetPlatform();
            }
            catch (Exception ex)
            {
                label1.Text = ex.Message.ToString();
            }
        }
    }
    
    public enum Platform
        {
         PocketPC, WindowsCE, Smartphone, Win32NT, Win32S, Win32Windows, Unknown
        }


}



And now the most interesting part, I compile and run it on my HTC HD2!!!
And I see this:
 

That's it! Doesn't recognize the program on my HTC HD2 smartphone!!! :)

Download the example:

mobileveros.rar · 22 downloads 

Files

Comments (0)

No comments yet.

Leave a comment
Comment
Images (up to 5 MB)
Your comment will appear after moderation.