C++ Windows 下读取DMI(硬件)信息的方法

在Windows下读取DMI信息的方法:

使用Windows自带的函数完成:

GetSystemFirmwareTable():点击这里->  msdn函数详情

系统要求在: Windows Vista, Windows XP Professional x64 Edition以上

// wdmi.cpp : Defines the entry point for the console application.
// Open source.... you can refer to below code....
 
#include "stdafx.h"
#include 
#include 
#include 
 
typedef struct _dmi_header
{
	BYTE type;
	BYTE length;
	WORD handle;
}dmi_header;
 
typedef struct _RawSMBIOSData
{
    BYTE    Used20CallingMethod;
    BYTE    SMBIOSMajorVersion;
    BYTE    SMBIOSMinorVersion;
    BYTE    DmiRevision;
    DWORD   Length;
    BYTE    SMBIOSTableData[];
}RawSMBIOSData;
 
static const char *dmi_chassis_type(BYTE code)
{
	/* 7.4.1 */
	static const char *type[] = {
		"Other", /* 0x01 */
		"Unknown",
		"Desktop",
		"Low Profile Desktop",
		"Pizza Box",
		"Mini Tower",
		"Tower",
		"Portable",
		"Laptop",
		"Notebook",
		"Hand Held",
		"Docking Station",
		"All In One",
		"Sub Notebook",
		"Space-saving",
		"Lunch Box",
		"Main Server Chassis", /* CIM_Chassis.ChassisPackageType says "Main System Chassis" */
		"Expansion Chassis",
		"Sub Chassis",
		"Bus Expansion Chassis",
		"Peripheral Chassis",
		"RAID Chassis",
		"Rack Mount Chassis",
		"Sealed-case PC",
		"Multi-system",
		"CompactPCI",
		"AdvancedTCA",
		"Blade",
		"Blade Enclosing" /* 0x1D */
	};
 
	code &= 0x7F; /* bits 6:0 are chassis type, 7th bit is the lock bit */
 
	if (code >= 0x01 && code <= 0x1D)
		return type[code - 0x01];
	return "unknown";
}
 
static void dmi_system_uuid(const BYTE *p, short ver)
{
	int only0xFF = 1, only0x00 = 1;
	int i;
 
	for (i = 0; i < 16 && (only0x00 || only0xFF); i++)
	{
		if (p[i] != 0x00) only0x00 = 0;
		if (p[i] != 0xFF) only0xFF = 0;
	}
 
	if (only0xFF)
	{
		printf("Not Present");
		return;
	}
	if (only0x00)
	{
		printf("Not Settable");
		return;
	}
 
	/*
	 * As of version 2.6 of the SMBIOS specification, the first 3
	 * fields of the UUID are supposed to be encoded on little-endian.
	 * The specification says that this is the defacto standard,
	 * however I've seen systems following RFC 4122 instead and use
	 * network byte order, so I am reluctant to apply the byte-swapping
	 * for older versions.
	 */
	if (ver >= 0x0206)
		printf("%02X%02X%02X%02X-%02X%02X-%02X%02X-%02X%02X-%02X%02X%02X%02X%02X%02X\n",
			p[3], p[2], p[1], p[0], p[5], p[4], p[7], p[6],
			p[8], p[9], p[10], p[11], p[12], p[13], p[14], p[15]);
	else
		printf("-%02X%02X%02X%02X-%02X%02X-%02X%02X-%02X%02X-%02X%02X%02X%02X%02X%02X\n",
			p[0], p[1], p[2], p[3], p[4], p[5], p[6], p[7],
			p[8], p[9], p[10], p[11], p[12], p[13], p[14], p[15]);
}
 
const char *dmi_string(const dmi_header *dm, BYTE s)
{
	char *bp = (char *)dm;
	size_t i, len;
 
	if (s == 0)
		return "Not Specified";
 
	bp += dm->length;
	while (s > 1 && *bp)
	{
		bp += strlen(bp);
		bp++;
		s--;
	}
 
	if (!*bp)
		return "BAD_INDEX";
 
	/* ASCII filtering */
	len = strlen(bp);
	for (i = 0; i < len; i++)
		if (bp[i] < 32 || bp[i] == 127)
			bp[i] = '.';
 
	return bp;
}
 
int _tmain(int argc, _TCHAR* argv[])
{
	DWORD bufsize = 0;
	BYTE buf[65536] = {0};
	int ret = 0;
	RawSMBIOSData *Smbios;
	dmi_header *h = NULL;
	int flag = 1;
	
	ret = GetSystemFirmwareTable('RSMB', 0, 0, 0);
	if (!ret)
	{
		printf("Function failed!\n");
		return 1;
	}
 
	printf("get buffer size is %d\n", ret);
	bufsize = ret;
 
	ret = GetSystemFirmwareTable('RSMB', 0, buf, bufsize);
	if (!ret)
	{
		printf("Function failed!\n");
		return 1;
	}
 
	Smbios = (RawSMBIOSData *)buf;
 
	BYTE *p = Smbios->SMBIOSTableData;
 
	if (Smbios->Length != bufsize -8 )
	{
		printf("Smbios length error\n");
		return 1;
	}
 
	for(int i =0; iLength; i++){
		h = (dmi_header *)p;
 
		if (h->type == 0 && flag) {
			printf("\nType %02d - [BIOS]\n", h->type);
			printf("\tBIOS Vendor : %s\n", dmi_string(h, p[0x4]));
			printf("\tBIOS Version: %s\n", dmi_string(h, p[0x5]));
			printf("\tRelease Date: %s\n", dmi_string(h, p[0x8]));
			if(p[0x16]!=0xff && p[0x17]!=0xff)
				printf("\tEC version: %d.%d\n", p[0x16], p[0x17]);
 
			flag = 0;
		}
 
		else if (h->type == 1) {
			printf("\nType %02d - [System Information]\n", h->type);
			printf("\tManufacturer: %s\n", dmi_string(h, p[0x4]));
			printf("\tProduct Name: %s\n", dmi_string(h, p[0x5]));
			printf("\tVersion: %s\n", dmi_string(h, p[0x6]));
			printf("\tSerial Number: %s\n", dmi_string(h, p[0x7]));
			printf("\tUUID: "); dmi_system_uuid(p+0x8, Smbios->SMBIOSMajorVersion*0x100+Smbios->SMBIOSMinorVersion);
			printf("\tSKU Number: %s\n", dmi_string(h, p[0x19]));
			printf("\tFamily: %s\n", dmi_string(h, p[0x1a]));
		}
 
		else if (h->type == 2) {
            printf("\nType %02d - [System Information]\n", h->type);
			printf("\tManufacturer: %s\n", dmi_string(h, p[0x4]));
			printf("\tProduct: %s\n", dmi_string(h, p[0x5]));
			printf("\tVersion: %s\n", dmi_string(h, p[0x6]));
			printf("\tSerial Number: %s\n", dmi_string(h, p[0x7]));
			printf("\tAsset Tag: %s\n", dmi_string(h, p[0x8]));
			printf("\tLocation in Chassis: %s\n", dmi_string(h, p[0x0a]));
		}
 
		else if (h->type == 3) {
			printf("\nType %02d - [System Enclosure or Chassis]\n", h->type);
			printf("\tManufacturer: %s\n", dmi_string(h, p[0x04]));
			printf("\tType: %s\n", dmi_chassis_type(p[0x05]));
			printf("\tVersion: %s\n", dmi_string(h, p[0x06]));
			printf("\tSerial Number: %s\n", dmi_string(h, p[0x07]));
			printf("\tAsset Tag Number: %s\n", dmi_string(h, p[0x08]));
			printf("\tVersion: %s\n", dmi_string(h, p[0x10]));
		}
 
		else if(h->type == 4){
			printf("\nType %02d - [Processor Information]\n", h->type);
			printf("\tSocket Designation: %s\n", dmi_string(h, p[0x04]));
			printf("\tProcessor Manufacturer: %s\n", dmi_string(h, p[0x07]));
			printf("\tProcessor Version: %s\n", dmi_string(h, p[0x10]));
			printf("\tVoltage: %d (Bit0 - 5v, Bit1 - 3.3v, Bit2 - 2.9v)\n", p[0x11]&0x7);
 
			printf("\tExternal Clock: %d MHz\n", p[0x12]+p[0x13]*0x100);
			printf("\tMax Speed: %d MHz\n", p[0x14]+p[0x15]*0x100);
			printf("\tCurrent Speed: %d MHz\n", p[0x16]+p[0x17]*0x100);
			printf("\tSerial Number: %s\n", dmi_string(h, p[0x20]));
			printf("\tAsset Tag: %s\n", dmi_string(h, p[0x21]));
			printf("\tPart Number: %s\n", dmi_string(h, p[0x22]));
		}
 
		else if (h->type == 17) {
			if (p[0xc]+p[0xd]*0x100 == 0)
				continue;
			printf("\nType %02d - [Memory]\n", h->type);
			printf("\tTotal Width: %d\n", p[0x8]);
			printf("\tData Width:%d\n",   p[0xa]);
			printf("\tSize: %d MB\n",   p[0xc]+p[0xd]*0x100);
			printf("\tSpeed: %dMHz\n",	p[0x15]+p[0x16]*0x100);
			printf("\tBank Locator: %s\n", dmi_string(h, p[0x11]));
			printf("\tManufacturer: %s\n", dmi_string(h, p[0x17]));
			printf("\tSerial Number: %s\n", dmi_string(h, p[0x18]));
			printf("\tAsset Tag: %s\n", dmi_string(h, p[0x19]));
			printf("\tPart Number: %s\n", dmi_string(h, p[0x1A]));
		}
		
		p += h->length;
		while((*(WORD *)p)!=0) p++;
		p += 2;
	}
 
	getchar();
	return 0;
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
一般获取主板UID都是cmd命令 执行"wmic path Win32_ComputerSystemProduct get uuid" 这里说的是使用API获取,找了半天没找到,最后发现只能通过 GetSystemFirmwareTable 获取 SMBIOS信息 ,在这个结构体里存储的相当复杂,看的有点头晕 首先调用 GetSystemFirmwareTable  先让他返回结构体大小,然后我们申请一块内存,再调用一次,就会把  SMBIOS信息 存储到 我们申请的内存里了. SMBIOS信息 前8个字节就不看了,可以百度慢慢看,从第9个字节开始是TYPE结构,第一个字节是type类型,第二个字节就是这个type类型的大小,从type起始地址计算,后面的就是格式化区域,不同type类型格式化区域代表的信息也不同,格式化区域完后,就会跟一段u字符串,我们知道ascii字符串是0结尾,u字符串是00 结尾,字符串结尾后就是下一个type结构的开始了. 这里就只看下type1类型结构  从资料里看,在第8个字节后的16位就是uid了.   其他类型大家就自己查一下了. 里面的模块命令 就用了下 精益的十六进制转换命令,这个大家应该都有. 代码有点丑,见笑了. 系统信息 (Type 1) : SMBIOS 实现只关联一个单一的系统实例,并且包含且只包含一个系统信息结构。 位置 名称 长度 描述 00h TYPE 号 1BYTE 结构的TYPE 号,此处是1 01h 长度 1BYTE 格式区域总长度,2.0 版为08h ,2.1-2.3.4 版为19h,从2.4 版开始为1Bh 02h 句柄 2BYTE 指向本结构的句柄 04h 电脑制造商 1BYTE 一般为01h ,表示在字符串区域中的编号 05h 产品名称 1BYTE [url=]在字符串区域中的编号[/url] 06h 版本号 1BYTE 在字符串区域中的编号 07h 序列号 1BYTE 在字符串区域中的编号 09h UUID 16BYTE 通用唯一标识符 18h 唤醒类型 BYTE 用来标识导致系统开电启动的事件 19h SKU 号 BYTE 在字符串区域中的编号,SKU 号通常为产品ID 或采购订单号 1Ah 产品家族 1BYTE 在字符串区域中的编号

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值