Platform detection I: How to detect that your app is running in the emulator


When you develop your Windows CE or Windows Mobile application in .NET Compact Framework, you probably do a lot of testing on the Microsoft Device Emulators for Smartphone and Pocket PC.  Here I describe how to detect whether your program is running on an emulator or a physical device.


Microsoft’s Device Emulator gives itself away through a WinCE API called SystemParametersInfo when you pass in the argument SPI_GETOEMINFO.  We’ll use this to check for the emulator.  When we detect something other than the Microsoft value, we must be running on a physical device. 


I use partial classes because in later posts in the Platform Detection series I’ll add more to these classes.

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


This is the first post in a series of three on platform detection.  Coming up next: discerning between Smartphones and Pocket PCs.

Comments (27)

  1. Nino.Mobile says:

    Software / Hardware Engadget gives us a few of HTC’s Q4 lineup . Here’s a link to their European press

  2. I now have three blogs that I post to: this one, JMPInline , and NetCFTeam . Which posts will I put where?

  3. 黎波 says:

    是否曾经想过要设计一个.NETCF应用程序,它能够同时在PocketPC和Smartphone平台上使用,并根据不同的平台加载不同的功能模块,那如何检测你的应用程序当前运行在Pock…

  4. 作者:马宁 在开发Windows CE的应用程序时,经常需要检测平台类型,了解我们的应用程序运行在Pocket PC、Smartphone还是Windows CE上。在这篇文章里,我们介绍如何编写一个应用程序来检测当前运行的平台类型。

  5. 马宁 says:

    作者:马宁在开发WindowsCE的应用程序时,经常需要检测平台类型,了解我们的应用程序运行在PocketPC、Smartphone还是WindowsCE上。在这篇文章里,我们介绍如何编写一个…

  6. 作者:马宁 在开发Windows CE的应用程序时,经常需要检测平台类型,了解我们的应用程序运行在Pocket PC、Smartphone还是Windows CE上。在这篇文章里,我们介绍如何编写一个应用程序来检测当前运行的平台类型。