获取设备 ID 和名称
获取设备 ID 和名称

更新:2007 年 11 月
要获取设备的名称,请使用 Dns.GetHostName 属性。通常情况下,默认名称为“PocketPC”。
本示例在加载窗体时在消息框中显示设备的 ID 和名称。
要获取设备 ID 或序列号,您必须使用平台调用来访问本机 Windows CE KernelIoControl 函数。
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Diagnostics;
using System.Windows.Forms;
using System.Runtime.InteropServices;
using System.Text; namespace DeviceID
{
/// <summary>
/// Summary description for DeviceID.
/// </summary>
public class DeviceID : System.Windows.Forms.Form
{ public DeviceID()
{
//
// Required for Windows Form Designer support
//
InitializeComponent(); //
// TODO: Add any constructor code after InitializeComponent call
//
} /// <summary>
/// Clean up any resources being used.
/// </summary>
protected override void Dispose( bool disposing )
{
base.Dispose( disposing );
} #region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
//
// DeviceID
//
this.Text = "DeviceID";
this.Load += new System.EventHandler(this.DeviceID_Load); }
static void Main()
{
Application.Run(new DeviceID());
}
#endregion private static Int32 METHOD_BUFFERED = 0;
private static Int32 FILE_ANY_ACCESS = 0;
private static Int32 FILE_DEVICE_HAL = 0x00000101; private const Int32 ERROR_NOT_SUPPORTED = 0x32;
private const Int32 ERROR_INSUFFICIENT_BUFFER = 0x7A; private static Int32 IOCTL_HAL_GET_DEVICEID =
((FILE_DEVICE_HAL) << 16) | ((FILE_ANY_ACCESS) << 14)
| ((21) << 2) | (METHOD_BUFFERED); [DllImport("coredll.dll", SetLastError=true)]
private static extern bool KernelIoControl(Int32 dwIoControlCode,
IntPtr lpInBuf, Int32 nInBufSize, byte[] lpOutBuf,
Int32 nOutBufSize, ref Int32 lpBytesReturned); private static string GetDeviceID()
{
// Initialize the output buffer to the size of a
// Win32 DEVICE_ID structure.
byte[] outbuff = new byte[20];
Int32 dwOutBytes;
bool done = false; Int32 nBuffSize = outbuff.Length; // Set DEVICEID.dwSize to size of buffer. Some platforms look at
// this field rather than the nOutBufSize param of KernelIoControl
// when determining if the buffer is large enough.
BitConverter.GetBytes(nBuffSize).CopyTo(outbuff, 0);
dwOutBytes = 0; // Loop until the device ID is retrieved or an error occurs.
while (! done)
{
if (KernelIoControl(IOCTL_HAL_GET_DEVICEID, IntPtr.Zero,
0, outbuff, nBuffSize, ref dwOutBytes))
{
done = true;
}
else
{
int error = Marshal.GetLastWin32Error();
switch (error)
{
case ERROR_NOT_SUPPORTED:
throw new NotSupportedException(
"IOCTL_HAL_GET_DEVICEID is not supported on this device",
new Win32Exception(error)); case ERROR_INSUFFICIENT_BUFFER: // The buffer is not big enough for the data. The
// required size is in the first 4 bytes of the output
// buffer (DEVICE_ID.dwSize).
nBuffSize = BitConverter.ToInt32(outbuff, 0);
outbuff = new byte[nBuffSize]; // Set DEVICEID.dwSize to size of buffer. Some
// platforms look at this field rather than the
// nOutBufSize param of KernelIoControl when
// determining if the buffer is large enough.
BitConverter.GetBytes(nBuffSize).CopyTo(outbuff, 0);
break; default:
throw new Win32Exception(error, "Unexpected error");
}
}
} // Copy the elements of the DEVICE_ID structure.
Int32 dwPresetIDOffset = BitConverter.ToInt32(outbuff, 0x4);
Int32 dwPresetIDSize = BitConverter.ToInt32(outbuff, 0x8);
Int32 dwPlatformIDOffset = BitConverter.ToInt32(outbuff, 0xc);
Int32 dwPlatformIDSize = BitConverter.ToInt32(outbuff, 0x10);
StringBuilder sb = new StringBuilder(); for (int i = dwPresetIDOffset;
i < dwPresetIDOffset + dwPresetIDSize; i++)
{
sb.Append(String.Format("{0:X2}", outbuff[i]));
} sb.Append("-"); for (int i = dwPlatformIDOffset;
i < dwPlatformIDOffset + dwPlatformIDSize; i ++ )
{
sb.Append( String.Format("{0:X2}", outbuff[i]));
}
return sb.ToString();
} private void DeviceID_Load(object sender, System.EventArgs e)
{
try
{
// Show the device ID.
string strDeviceID = GetDeviceID();
MessageBox.Show("Device ID: " + strDeviceID); // Show the device name.
string deviceName = System.Net.Dns.GetHostName();
MessageBox.Show("Device Name: " + deviceName);
} catch (Exception ex)
{
MessageBox.Show(ex.Message.ToString());
}
}
}
}
下表列出了本机 KernelIoControl 函数参数。所有参数均是 32 位。
lpOutBuf 参数具有以下结构:
struct DEVICE_ID
{
int dwSize;
int dwPresetIDOffset;
int dwPresetIDBytes;
int dwPlatformIDOffset;
int dwPlatformIDBytes;
}
返回值和错误处理
如果将设备 ID 复制到了输出缓冲区,KernelIoControl 函数返回 true;否则,它会返回 false。如果 KernelIoControl 失败,请调用托管GetLastWin32Error 方法以获取 Win32 错误代码。错误代码可能是下列代码中的任意一种:
ERROR_NOT_SUPPORTED - 指示设备不执行 IOCTL_HAL_GET_DEVICEID 控制代码。
ERROR_INSUFFICIENT_BUFFER - 指示输出缓冲区不够大,无法容纳设备 ID。由 DEVICE_ID 结构中的 dwSize 指定的所需字节数在输出缓冲区的前四个字节中返回。如果发生此错误,请将输出缓冲区重新分配为 dwSize 指定的大小,然后再次调用 KernelIoControl。
获取设备 ID 和名称的更多相关文章
- ionic获取ios唯一设备id的解决方案
经常有朋友来问这个问题. 每次都去解释这个问题也浪费不少时间, 所以还是开一篇文章, 把这个问题说清楚吧. 先纠正一个误区吧: 有同学可以通过ionic natvie的device插件获取. 我们在文 ...
- ios获取设备信息总结
1.获取设备的信息 UIDevice *device = [[UIDevice alloc] int]; NSString *name = device.name; //获取设备所有者的名 ...
- Android 开发 获取设备信息与App信息
设备信息 设备ID(DeviceId) 获取办法 android.telephony.TelephonyManager tm = (android.telephony.TelephonyManager ...
- ios 获取设备相关的信息
.获取设备的信息 UIDevice *device = [[UIDevice alloc] int]; NSString *name = device.name; //获取设备所有者的名称 NSStr ...
- 测试成长记录:python调adb无法获取设备信息bug记录
背景介绍: 一直在负责公司Android自动化的编写工作,采用的是uiautomator2,需要获取设备id来连接设备,就是 adb devices 问题描述: 之前一直用 subprocess.ch ...
- iOS 获取设备信息,mac地址,IP地址,设备名称
#import "DeviceInfoUtil.h" #import "GlobleData.h" #import "sys/utsname.h&qu ...
- 获取设备IMEI ,手机名称,系统SDK版本号,系统版本号
1.获取设备IMEI TelephonyManager tm = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE); Str ...
- 黄聪:wordpress如何获取当前分类页面的ID、名称、别名(slug)
<? global $wp_query; $cat_ID = get_query_var('cat'); $category = get_category($cat_ID); echo $cat ...
- 【转】获取android设备 id
关于本文档 Android的开发者在一些特定情况下都需要知道手机中的唯一设备ID.例如,跟踪应用程序的安装,生成用于复制保护的DRM时需要使用设备的唯一ID.在本文档结尾处提供了作为参考的示例代码片段 ...
随机推荐
- 金中半日baoling游-----stoi
蒟蒻又来水博客了,写个游记啦啦啦啦,好像是第一篇游记咯. 温馨提示:愚人节写的博客看了后会变棒棒哦!(麻麻再也不用担心我被骗) 进入正题 3月31日早6:30左右起床了,然后就是....(此处可省略) ...
- Java视频教程等百度云资源分享——更新ing
韩顺平javase(87讲)密码:hsp789 链接:https://pan.baidu.com/s/1eNCyvFcVHsd7P4gdvrFqtw密码:el1y 韩顺平javaee(66讲)密码:h ...
- Codeforces Round #447 (Div. 2) C 构造
现在有一个长度为n的数列 n不超过4000 求出它的gcd生成set 生成方式是对<i,j> insert进去(a[i] ^ a[i+1] ... ^a[j]) i<=j 然而现在给 ...
- JumpGame,JumpGame2
JumpGame:给定一个非负整数数组,开始在第一个位置,每个位置上的数字代表最大可以跳跃的步数,判断能不能跳到最后一个位置. 例如:A=[2,3,1,1,4],在位置0处,可以跳一步到位置1,位置1 ...
- mysql desc esc 基本命令总结
asc 按升序排列desc 按降序排列 下列语句部分是Mssql语句,不可以在access中使用. SQL分类:DDL—数据定义语言(CREATE,ALTER,DROP,DECLARE)DML—数据操 ...
- LeetCode第[11]题(Java):Container With Most Water (数组容器盛水)——Medium
题目难度:Medium Given n non-negative integers a1, a2, ..., an, where each represents a point at coordina ...
- bower 安装
安装Yeomannpm install --global yo搭建一个web应用脚手架,你将需要安装generator-webapp生成器:npm install -g generator-webap ...
- 图片与路径(Path)的应用
图片的应用:软盘样式的保存按钮,笔记本样式的编辑按钮:只能用图片 路径(Path)的应用:异形轮廓(各种气泡框,普通控件无法描述):异形线条(普通控件无法描述):图片(不建议,因为展现效果不好,比如: ...
- 为Visual Studio添加快捷工具
添加额外工具: Tools -> External Tools... 1. 添加Git Console Title: Git Console Command: C:\Program Files\ ...
- 【lightoj-1002】Country Roads(dijkstra变形)
light1002:传送门 [题目大意] n个点m条边,给一个源点,找出源点到其他点的‘最短路’ 定义:找出每条通路中最大的cost,这些最大的cost中找出一个最小的即为‘最短路’,dijkstra ...