NETCF运行平台检测(转)
2009-03-17 10:22
323 查看
该文章参考了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 ()
源码如下:
测试过程中发现文章中提供的代码存在兼容性问题,这源于一次偶然性的测试,当时用的是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; } } }
相关文章推荐
- NETCF运行平台检测
- visual studio.net已检测到指定的web服务器运行的不是asp.net1.1版。无法运行asp.net web应用程序 (转载)
- visual studio.net已检测到指定的web服务器运行的不是asp.net 1.1 版错误
- “Visual Studio.net已检测到指定的Web服务器运行的不是Asp.net1.1版。您将无法运行Asp.net Web应用程序或服务”问题的解决方案
- Visual Studio .NET已检测到指定的Web服务器运行的不是ASP.NET 1.1 版
- Visual Studio .NET已检测到指定的WEB服务运行的不是ASP.NET 1.1版
- “Visual Studio .NET 已检测到指定的 Web 服务器运行的不是 ASP.NET 1.1 版...”问题的解决办法
- 第3章 软件安装、配置、运行方法--统一项目管理平台(UMPlatForm.NET)
- Visual Studio.NET 已检测到指定的Web服务器运行的不是ASP.NET1.1版的问题。
- 您的 .NET CF 程序到底运行在什么平台上?
- .NET跨平台:在Ubuntu上用自己编译的dnx运行ASP.NET 5示例程序
- 错误:Visual Studio .NET 已检测到指定的 Web 服务器运行的不是 ASP.NET 1.1 版
- 用dos批处理程序检测是否安装.netframework,并自动安装后运行指定程序(.net自启动光盘的制做)
- 解决"Visual Studio .NET 已检测到指定的 Web 服务器运行的不是 ASP.NET 1.1 版。您将无法运行 ASP.NET Web应用程序或服务"的方法
- vs.net已经检测到制定的WEB服务器运行的不是ASP.NET1.1版,你无法运行ASP.NET WEB应用程序或服务
- visual stadio.net已检测到web服务器运行的不是asp.net1.1版"故障的排除
- visual studio.net已检测到指定的Web服务器运行的不是ASP.net 1.1版。
- visual studio.net已检测到web服务器运行的不是asp.net1.1版"故障的排除
- AgileEAS.NET SOA 中间件平台5.2版本下载、配置学习(一):下载平台并基于直连环境运行
- 【华磊原创】安装程序自动检测安装.Net Framework运行环境(使用InnoSetup)