原创 NETCF运行平台检测

2008-5-10 13:24 2048 5 5 分类: 软件与OS


该文章参考了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 ()

源码如下:


None.gifusing System;
None.gif
using System.IO;
None.gif
using Microsoft.Win32;
None.gif
using System.Runtime.InteropServices;
None.gif
using System.Text;
None.gif
None.gif
namespace PlatformDetection
ExpandedBlockStart.gifContractedBlock.gif
dot.gif{
InBlock.gif    
internal class PInvoke
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        [DllImport(
"Coredll.dll", EntryPoint = "SystemParametersInfoW", CharSet = CharSet.Unicode)]
InBlock.gif        
static extern int SystemParametersInfo4Strings(uint uiAction, uint uiParam, StringBuilder pvParam, uint fWinIni);
InBlock.gif
InBlock.gif        
const int MAX_PATH = 260;
InBlock.gif        [DllImport(
"Coredll.dll")]
InBlock.gif        
static extern int SHGetSpecialFolderPath(IntPtr hwndOwner, StringBuilder lpszPath, int nFolder, int fCreate);
InBlock.gif
InBlock.gif        
public enum SystemParametersInfoActions : uint
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            SPI_GETPLATFORMTYPE 
= 257// this is used elsewhere for Smartphone/PocketPC detection
InBlock.gif
            SPI_GETOEMINFO = 258,
ExpandedSubBlockEnd.gif        }

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

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

InBlock.gif
InBlock.gif        
public enum SpecialFolders : int
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            CSIDL_WINDOWS 
= 0x0024,
ExpandedSubBlockEnd.gif        }

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

ExpandedSubBlockEnd.gif    }

InBlock.gif
InBlock.gif    
public class PlatformDetection
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
private const string MicrosoftEmulatorOemValue = "Microsoft DeviceEmulator";
InBlock.gif
InBlock.gif        
//10/26/2006 wangyun To effect compatibility between PC and PDA devices
InBlock.gif
        public static bool IsWinCE()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
return System.Environment.OSVersion.Platform == PlatformID.WinCE;
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
public static bool IsEmulator()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
if (IsWinCE())
InBlock.gif                
return PInvoke.GetOemInfo() == MicrosoftEmulatorOemValue;
InBlock.gif            
else
InBlock.gif                
return false;
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
public static bool IsSmartphone()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
if (IsWinCE())
InBlock.gif                
return PInvoke.GetPlatformType() == "SmartPhone";
InBlock.gif            
else
InBlock.gif                
return false;
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
public static bool IsPocketPC()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
if (IsWinCE())
InBlock.gif                
return PInvoke.GetPlatformType() == "PocketPC";
InBlock.gif            
else
InBlock.gif                
return false;
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
public static bool IsTouchScreen()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
if (IsWinCE())
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
string driverFileName = Registry.GetValue(@"HKEY_LOCAL_MACHINE\Hardware\DeviceMap\Touch",
InBlock.gif                    
"DriverName""touch.dll").ToString();
InBlock.gif                
string windowsFolder = PInvoke.GetSpecialFolder(PInvoke.SpecialFolders.CSIDL_WINDOWS);
InBlock.gif                
string driverPath = Path.Combine(windowsFolder, driverFileName);
InBlock.gif                
bool driverExists = File.Exists(driverPath);
InBlock.gif
InBlock.gif                
return
InBlock.gif                    driverExists 
&&
InBlock.gif                    
// Windows Mobile 5.0 Smartphone emulator and earlier has a driver, but no touch screen.
InBlock.gif
                    !(IsSmartphone() && IsEmulator() && Environment.OSVersion.Version.Major < 6);
ExpandedSubBlockEnd.gif            }

InBlock.gif            
else
InBlock.gif                
return false;
ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif    }

ExpandedBlockEnd.gif}
PARTNER CONTENT

文章评论0条评论)

登录后参与讨论
我要评论
0
5
关闭 站长推荐上一条 /3 下一条