Welcome to MSDN Blogs Sign in | Join | Help

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.

Published Friday, September 15, 2006 4:59 PM by NetCFTeam

Comments

# The Mobile Minute 150

Monday, September 18, 2006 10:36 PM by Nino.Mobile
Software / Hardware Engadget gives us a few of HTC’s Q4 lineup . Here’s a link to their European press

# Identity crisis

Friday, June 15, 2007 1:54 PM by Andrew Arnott

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

# MSDN Blog Postings · Identity crisis

Friday, June 15, 2007 4:39 PM by MSDN Blog Postings · Identity crisis

# Voip WebLog » Blog Archive » Re: Alternative to virtual template function

# 您的 .NET CF 程序到底运行在什么平台上?

Saturday, May 10, 2008 9:48 AM by 黎波

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

# 如何修改Windows CE的平台类型

Saturday, May 09, 2009 11:14 PM by 马宁的Windows Embedded研究

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

# 如何修改Windows CE的平台类型

Saturday, May 09, 2009 11:17 PM by 马宁

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

# 如何修改Windows CE的平台类型

Saturday, May 09, 2009 11:18 PM by Windows Embedded CE 中国研发团队

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

# Anith » ????????????Windows CE???????????????

# Tune Up Your PC » Post Topic » ????????????Windows CE???????????????

# NET Compact Framework Team Platform detection I How to detect that | work from home

Anonymous comments are disabled
 
Page view tracker