【问题标题】:.NET compact framework - detecting if under emulator?.NET 紧凑框架 - 检测是否在模拟器下?
【发布时间】:2010-06-25 14:31:13
【问题描述】:

有没有办法通过 .NET CF 代码检测我们是在模拟器上运行还是在真实设备上运行?

谢谢 多米尼克

【问题讨论】:

    标签: c# .net compact-framework device-emulation


    【解决方案1】:

    This article 间接告诉你如何做。它展示了如何创建一个实用方法IsEmulator 来解决问题。如果您一般关心平台检测,您可能还对follow-up 感兴趣。

    来自文章:

    using System;
    using System.IO;
    using System.Windows.Forms;
    using Microsoft.Win32;
    using System.Runtime.InteropServices;
    using System.Text;
    
    namespace PlatformDetection
    {
        internal partial class PInvoke
        {
            [DllImport("Coredll.dll", EntryPoint = "SystemParametersInfoW", CharSet = CharSet.Unicode)]
            static extern int SystemParametersInfo4Strings(uint uiAction, uint uiParam, StringBuilder pvParam, uint fWinIni);
    
            public enum SystemParametersInfoActions : uint
            {
                SPI_GETPLATFORMTYPE = 257, // this is used elsewhere for Smartphone/PocketPC detection
                SPI_GETOEMINFO = 258,
            }
    
            public static string GetOemInfo()
            {
                StringBuilder oemInfo = new StringBuilder(50);
                if (SystemParametersInfo4Strings((uint)SystemParametersInfoActions.SPI_GETOEMINFO,
                    (uint)oemInfo.Capacity, oemInfo, 0) == 0)
                    throw new Exception("Error getting OEM info.");
                return oemInfo.ToString();
            }
    
        }
        internal partial class PlatformDetection
        {
            private const string MicrosoftEmulatorOemValue = "Microsoft DeviceEmulator";
            public static bool IsEmulator()
            {
                return PInvoke.GetOemInfo() == MicrosoftEmulatorOemValue;
            }
        }
        class EmulatorProgram
        {
            static void Main(string[] args)
            {
                MessageBox.Show("Emulator: " + (PlatformDetection.IsEmulator() ? "Yes" : "No"));
            }
        }
    }
    

    【讨论】:

      【解决方案2】:

      如果您使用的是OpenNETCF Smart Device Framework,您可以测试OpenNETCF.WindowsCE.DeviceManagement.OemInfo 属性以查看它是否等于“Microsoft DeviceEmulator”。这就是我检测到我在模拟器下运行并且不应该与条形码阅读器等特定硬件交互的方式。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2010-09-18
        • 2010-10-17
        • 2011-02-26
        • 1970-01-01
        相关资源
        最近更新 更多