7

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

谢谢多米尼克

4

2 回答 2

7

本文间接地告诉你如何。它展示了如何创建一个实用的方法IsEmulator来解决这个问题。如果您通常关心平台检测,您可能也对后续感兴趣。

来自文章:

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"));
        }
    }
}
于 2010-06-25T14:35:33.463 回答
4

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

于 2014-02-09T07:43:09.723 回答