【问题标题】:Get the Device ID on the Windows Mobile device in compact framework?在紧凑框架中获取 Windows Mobile 设备上的设备 ID?
【发布时间】:2012-07-28 21:13:16
【问题描述】:

当我使用 RAPI API 从 PC 连接到 Windows 移动设备时,我可以像这样获取它的设备 ID:

using (RemoteDeviceManager r = new RemoteDeviceManager())
{
     using (RemoteDevice dev = r.Devices.FirstConnectedDevice)
     {
         //dev.DeviceId; <--the id
     }
}

如何在紧凑的框架中在设备本身上获得相同的 'DeviceId' 值?

【问题讨论】:

    标签: c# windows-mobile compact-framework rapi


    【解决方案1】:

    完整代码为:

    public static class Device 
    {
        //http://msdn.microsoft.com/en-us/library/aa446562.aspx
        //These values and the definition of IOCTL_HAL_GET_DEVICEID were taken from the C++ header file, 
        // winioctl.h.
        private static Int32 FILE_DEVICE_HAL = 0x00000101;
        private static Int32 FILE_ANY_ACCESS = 0x0;
        private static Int32 METHOD_BUFFERED = 0x0;
    
        private static Int32 IOCTL_HAL_GET_DEVICEID =
            ((FILE_DEVICE_HAL) << 16) | ((FILE_ANY_ACCESS) << 14)
             | ((21) << 2) | (METHOD_BUFFERED);
    
        [DllImport("coredll.dll")]
        private static extern bool KernelIoControl(Int32 IoControlCode, IntPtr InputBuffer, Int32 InputBufferSize, byte[] OutputBuffer, Int32 OutputBufferSize, ref Int32 BytesReturned);
        public static string GetDeviceID()
        {
            byte[] data = new byte[256];
            Int32 OutputBufferSize, BytesReturned;
            OutputBufferSize = data.Length;
            BytesReturned = 0;
            // Call KernelIoControl passing the previously defined IOCTL_HAL_GET_DEVICEID parameter
            // We don’t need to pass any input buffers to this call
            // so InputBuffer and InputBufferSize are set to their null values
            bool retVal = KernelIoControl(IOCTL_HAL_GET_DEVICEID, IntPtr.Zero, 0, data, OutputBufferSize, ref BytesReturned);
            // If the request failed, exit the method now
            if (retVal)
            {
                // Examine the OutputBuffer byte array to find the start of the 
                // Preset ID and Platform ID, as well as the size of the PlatformID. 
                // PresetIDOffset – The number of bytes the preset ID is offset from the beginning of the structure
                // PlatformIDOffset - The number of bytes the platform ID is offset from the beginning of the structure
                // PlatformIDSize - The number of bytes used to store the platform ID
                // Use BitConverter.ToInt32() to convert from byte[] to int
                Int32 PresetIDOffset = BitConverter.ToInt32(data, 4);
                Int32 PlatformIDOffset = BitConverter.ToInt32(data, 0xc);
                Int32 PlatformIDSize = BitConverter.ToInt32(data, 0x10);
    
                // Convert the Preset ID segments into a string so they can be 
                // displayed easily.
                StringBuilder sb = new StringBuilder();
                sb.Append(String.Format("{0:X8}-{1:X4}-{2:X4}-{3:X4}-",
                     BitConverter.ToInt32(data, PresetIDOffset),
                     BitConverter.ToInt16(data, PresetIDOffset + 4),
                     BitConverter.ToInt16(data, PresetIDOffset + 6),
                     BitConverter.ToInt16(data, PresetIDOffset + 8)));
    
                // Break the Platform ID down into 2-digit hexadecimal numbers
                // and append them to the Preset ID. This will result in a 
                // string-formatted Device ID
                for (int i = PlatformIDOffset; i < PlatformIDOffset + PlatformIDSize; i++)
                {
                    sb.Append(String.Format("{0:X2}", data[i]));
                }
                // return the Device ID string
                return sb.ToString();
            }
            // if null call 
            // "If the OEM has provided support for the IOCTL_HAL_GET_DEVICEID IOCTL, KernelIoControl will return true.
            // If the OEM has not provided support for IOCTL_HAL_GET_DEVICEID, or the request fails, then false is returned. 
            // If false is returned, call Marshal.GetLastWin32Error for details. An error code of 122 indicates that
            // the allocated DEVICE_ID buffer is too small to store all the information. In this situation,
            // the first 4 bytes of OutputBuffer will contain the required buffer size. 
            //Simply reallocate the size of OutputBuffer and request the information again."
            return null;
        }
    }
    

    那么 例如:

    var deviceId = Device.GetDeviceID();
    Debug.WriteLine(string.Format("Device Id {0}", deviceId));
    

    谢谢 jp2code。

    我刚刚完成了这些点。在 Windows Compact 7 中为我工作

    编辑: 参考:https://msdn.microsoft.com/en-us/library/ee478480.aspx 2014 年 10 月 16 日 此 I/O 控制 (IOCTL_HAL_GET_DEVICEID) 已弃用。其功能已集成到 IOCTL_HAL_GET_DEVICE_INFO。

    安东尼奥

    【讨论】:

    • 这应该是标记的答案。谢谢你把它放在一起。在带有 Windows CE 6 的摩托罗拉 MC3190 上工作
    【解决方案2】:

    Microsoft 早在 2003 年就在 Retrieving the Windows CE Device ID with the Microsoft .NET Compact Framework 上发布了一篇我一直成功使用的库文章。

    [DllImport("coredll.dll")]
    private static extern bool KernelIoControl(Int32 IoControlCode, IntPtr InputBuffer, Int32 InputBufferSize, byte[] OutputBuffer, Int32 OutputBufferSize, ref Int32 BytesReturned);
    
    private static string GetDeviceID() {
      // Reference: http://msdn.microsoft.com/en-us/library/aa446562.aspx
      byte[] data = new byte[256];
      Int32 OutputBufferSize, BytesReturned;
      OutputBufferSize = data.Length;
      BytesReturned = 0;
      // Call KernelIoControl passing the previously defined IOCTL_HAL_GET_DEVICEID parameter
      // We don’t need to pass any input buffers to this call
      // so InputBuffer and InputBufferSize are set to their null values
      bool retVal = KernelIoControl(IOCTL_HAL_GET_DEVICEID, IntPtr.Zero, 0, data, OutputBufferSize, ref BytesReturned);
      // If the request failed, exit the method now
      if (retVal) {
        // Examine the OutputBuffer byte array to find the start of the 
        // Preset ID and Platform ID, as well as the size of the PlatformID. 
        // PresetIDOffset – The number of bytes the preset ID is offset from the beginning of the structure
        // PlatformIDOffset - The number of bytes the platform ID is offset from the beginning of the structure
        // PlatformIDSize - The number of bytes used to store the platform ID
        // Use BitConverter.ToInt32() to convert from byte[] to int
        Int32 PresetIDOffset = BitConverter.ToInt32(data, 4);
        Int32 PlatformIDOffset = BitConverter.ToInt32(data, 0xc);
        Int32 PlatformIDSize = BitConverter.ToInt32(data, 0x10);
    
        // Convert the Preset ID segments into a string so they can be 
        // displayed easily.
        StringBuilder sb = new StringBuilder();
        sb.Append(String.Format("{0:X8}-{1:X4}-{2:X4}-{3:X4}-",
             BitConverter.ToInt32(data, PresetIDOffset),
             BitConverter.ToInt16(data, PresetIDOffset + 4),
             BitConverter.ToInt16(data, PresetIDOffset + 6),
             BitConverter.ToInt16(data, PresetIDOffset + 8)));
    
        // Break the Platform ID down into 2-digit hexadecimal numbers
        // and append them to the Preset ID. This will result in a 
        // string-formatted Device ID
        for (int i = PlatformIDOffset; i < PlatformIDOffset + PlatformIDSize; i++) {
          sb.Append(String.Format("{0:X2}", data[i]));
        }
        // return the Device ID string
        return sb.ToString();
      }
      return null;
    }
    

    【讨论】:

      猜你喜欢
      • 2011-08-17
      • 2010-12-12
      • 2011-02-20
      • 2011-02-08
      • 2012-04-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多