NETCF运行平台检测

     该文章参考了https://blogs.msdn.com/netcfteam/archive/2006/09/15/756755.aspx,上面详细介绍了如何在程序中检测NETCF的各种运行平台,此处对于其中的代码片段进行了整理。

     测试过程中发现文章中提供的代码存在兼容性问题,这源于一次偶然性的测试,当时用的是HP和SIEMENS的PDA进行测试,发现SIEMENS中当获取OEM信息时抛出异常,尝试性的进行了修改,居然解决了问题。具体的注释在代码中有详细的说明。

     鉴于有些PDA程序被要求在PC上面兼容运行,这也是客户的一个需求。在此对文章中的代码进行了改进。文章的代码原本被设计为在NETCF中运行,且使用了PINVOKE调用PDA中的相关底层函数,此代码如果在PC中运行,将产生不可预料的结果。由于当在PC上运行时,实际上是由.NET Framework的完整版本在执行代码,已经失去了对目标平台检测的意义。所以为了防止不可预料的结果出现,对相关代码做了修改。这一功能的实现是通过一个新添加的方法IsWinCE来实现的。该方法也可以向调用方提供必要的信息,作为检测实际执行代码的Framework的方法,具体参见源代码。

最终版本提供的检测功能:

  • IsWinCE ()
  • IsEmulator ()
  • IsSmartPhone ()
  • IsPockPC ()
  • IsTouchScreen ()

源码如下:

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

namespace PlatformDetection
{
    internal class PInvoke
    {
        [DllImport("Coredll.dll", EntryPoint = "SystemParametersInfoW", CharSet = CharSet.Unicode)]
        static extern int SystemParametersInfo4Strings(uint uiAction, 
                                                       uint uiParam, 
                                                       StringBuilder pvParam, 
                                                       uint fWinIni);

        const int MAX_PATH = 260;
        [DllImport("Coredll.dll")]
        static extern int SHGetSpecialFolderPath(IntPtr hwndOwner, 
                                                 StringBuilder lpszPath, 
                                                 int nFolder, 
                                                 int fCreate);

        public enum SystemParametersInfoActions : uint
        {
            SPI_GETPLATFORMTYPE = 257, // this is used elsewhere for Smartphone/PocketPC detection 
            SPI_GETOEMINFO = 258,
        }

        public static string GetOemInfo()
        {
            //Change from 50 to 100, as it doesn't work on "FUJITSU SIEMENS COMPUTERS Pocket LOOX" 
            StringBuilder oemInfo = new StringBuilder(100);
            if (SystemParametersInfo4Strings((uint)SystemParametersInfoActions.SPI_GETOEMINFO,
                    (uint)oemInfo.Capacity, oemInfo, 0) == 0)
                throw new Exception("Error getting OEM info.");
            return oemInfo.ToString();
        }

        public static string GetPlatformType()
        {
            //Change from 50 to 100 to avoid the possible bug as above 
            StringBuilder platformType = new StringBuilder(100);
            if (SystemParametersInfo4Strings((uint)SystemParametersInfoActions.SPI_GETPLATFORMTYPE,
                (uint)platformType.Capacity, platformType, 0) == 0)
                throw new Exception("Error getting platform type.");
            return platformType.ToString();
        }

        public enum SpecialFolders : int
        {
            CSIDL_WINDOWS = 0x0024,
        }

        public static string GetSpecialFolder(SpecialFolders specialFolder)
        {
            StringBuilder path = new StringBuilder(MAX_PATH);
            if (SHGetSpecialFolderPath(IntPtr.Zero, path, (int)specialFolder, 0) == 0)
                throw new Exception("Error getting Windows path.");
            return path.ToString();
        }
    }

    public class PlatformDetection
    {
        private const string MicrosoftEmulatorOemValue = "Microsoft DeviceEmulator";

        //To effect compatibility between PC and PDA devices 
        public static bool IsWinCE()
        {
            return System.Environment.OSVersion.Platform == PlatformID.WinCE;
        }

        public static bool IsEmulator()
        {
            if (IsWinCE())
                return PInvoke.GetOemInfo() == MicrosoftEmulatorOemValue;
            else
                return false;
        }

        public static bool IsSmartphone()
        {
            if (IsWinCE())
                return PInvoke.GetPlatformType() == "SmartPhone";
            else
                return false;
        }

        public static bool IsPocketPC()
        {
            if (IsWinCE())
                return PInvoke.GetPlatformType() == "PocketPC";
            else
                return false;
        }

        public static bool IsTouchScreen()
        {
            if (IsWinCE())
            {
                string driverFileName = Registry.GetValue(@"HKEY_LOCAL_MACHINE\Hardware\DeviceMap\Touch",
                    "DriverName", "touch.dll").ToString();
                string windowsFolder = PInvoke.GetSpecialFolder(PInvoke.SpecialFolders.CSIDL_WINDOWS);
                string driverPath = Path.Combine(windowsFolder, driverFileName);
                bool driverExists = File.Exists(driverPath);

                return
                    driverExists &&
                    // Windows Mobile 5.0 Smartphone emulator and earlier has a driver, but no touch screen. 
                    !(IsSmartphone() && IsEmulator() && Environment.OSVersion.Version.Major < 6);
            }
            else
                return false;
        }
    }
}

你可能感兴趣的:(net)