.NET紧凑框架 - 检测是否在模拟器下?

时间:2010-06-25 14:31:13

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

有没有办法从.NET CF代码中检测我们是在仿真器还是真实设备上运行?

由于 多米尼克

2 个答案:

答案 0 :(得分:7)

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"));
        }
    }
}

答案 1 :(得分:4)

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