开发者

Windows Mobile 6 - How to programmatically work out if you are using an emulator

How can I test 开发者_开发技巧to see if I am using an emulator or the actual windows mobile device?


You could try using the GetDeviceUniqueID to see if they differ. The emulator may return a different ID so you'd be able to distinguish between the two.

The link on GetDeviceUniqueID takes you to a blog explaining what it does and there is source code to use as well.


You will have to look it up but I remember seeing an entry several years ago whereby using reflection there was a difference in version numbers on the NETCF between emulator and real hardware. I don't remember what those values where, but "aa" was the real deal and "bb" was the emulator. Not sure if that technique would still be valid with NET CF 3.5 but it worked on v2.


After a whole lot of searching I eventually found the answer I was looking for - here. I found the link to this on another Stackoverflow question.


You could P/Invoke KernelIoControl to get the device model name and see if that tells you whether it's the emulator.

[DllImport("coredll", SetLastError = true)]
public static extern bool KernelIoControl([In] uint IoControlCode,
  [In] byte[] InputBuffer,
  [In] uint InputBufferSize,
  [In, Out] byte[] OutputBuffer,
  [In] uint OutputBufferSize,
  [Out] out uint BytesReturned);

public const int ERROR_INSUFFICIENT_BUFFER = 0x7A;
public const int SPI_GETOEMINFO = 258;
public const Int32 FILE_DEVICE_HAL = 0x00000101;
public const Int32 FILE_ANY_ACCESS = 0x0;
public const Int32 METHOD_BUFFERED = 0x0;

private static uint CTL_CODE(uint DeviceType, uint Function, uint Method, 
 uint Access)
{
  return ((DeviceType << 16) | (Access << 14) | (Function << 2) | Method);
}

public static uint IOCTL_HAL_GET_DEVICE_INFO = CTL_CODE(FILE_DEVICE_HAL, 1,
            METHOD_BUFFERED, FILE_ANY_ACCESS);

public static string GetDeviceModelName()
{
  byte[] inputBuffer = BitConverter.GetBytes(SPI_GETOEMINFO);
  byte[] outputBuffer = new byte[256];
  uint bytesReturned = 0;

  // call KernelIoControl with IOCTL_HAL_GET_DEVICE_INFO parameter
  bool retVal = false;
  try
  {
    retVal = KernelIoControl(IOCTL_HAL_GET_DEVICE_INFO,
               inputBuffer, (uint)inputBuffer.Length, outputBuffer,
               (uint)outputBuffer.Length, out bytesReturned);
  }
  catch
  {
  }

  // if not succeeded, then try the last step again, now with increased buffer size
  if (!retVal)
  {
    int error = Marshal.GetLastWin32Error();

    // if the buffer size is incorrect, adjust it and call function again.
    if (error == ERROR_INSUFFICIENT_BUFFER)
    {
      int buffSize = BitConverter.ToInt32(outputBuffer, 0);
      outputBuffer = new byte[buffSize];
      BitConverter.GetBytes(buffSize).CopyTo(outputBuffer, 0);

      retVal = KernelIoControl(IOCTL_HAL_GET_DEVICE_INFO,
                 inputBuffer, (uint)inputBuffer.Length, outputBuffer,
                 (uint)outputBuffer.Length, out bytesReturned);
    }
  }

  // extract the model name
  string modelNameStr = null;
  if (retVal)
  {
    modelNameStr = System.Text.Encoding.Unicode.GetString(outputBuffer, 0, 
                     outputBuffer.Length).Replace("\0","");
  }

  return modelNameStr;
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜