NETCF运行平台检测
生活随笔
收集整理的這篇文章主要介紹了
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的方法,具體參見源代碼。 最終版本提供的檢測功能:
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()
????????{
????????????//10/26/2006?wangyun?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()
????????{
????????????//10/26/2006?wangyun?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";
????????//10/26/2006?wangyun?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;
????????}
????}
}
- IsWinCE ()
- IsEmulator ()
- IsSmartPhone ()
- IsPockPC ()
- IsTouchScreen ()
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()
????????{
????????????//10/26/2006?wangyun?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()
????????{
????????????//10/26/2006?wangyun?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";
????????//10/26/2006?wangyun?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;
????????}
????}
}
轉載于:https://www.cnblogs.com/swnuwangyun/archive/2006/11/10/556838.html
總結
以上是生活随笔為你收集整理的NETCF运行平台检测的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 【下载中心】如何撰写分卷资源的标题、简介
- 下一篇: Java 知识总结大汇总!看完哪个都变大