第一种,依托WMI

#define _WIN32_DCOM
#include <iostream>
using namespace std;
#include <comdef.h>
#include <Wbemidl.h> # pragma comment(lib, "wbemuuid.lib") int main(int argc, char **argv)
{
HRESULT hres;
// Step 1: --------------------------------------------------
// Initialize COM. ------------------------------------------
hres = CoInitializeEx(0, COINIT_MULTITHREADED);
if (FAILED(hres))
{
cout << "Failed to initialize COM library. Error code = 0x"
<< hex << hres << endl;
return 1; // Program has failed.
} // Step 2: --------------------------------------------------
// Set general COM security levels --------------------------
// Note: If you are using Windows 2000, you need to specify -
// the default authentication credentials for a user by using
// a SOLE_AUTHENTICATION_LIST structure in the pAuthList ----
// parameter of CoInitializeSecurity ------------------------
hres = CoInitializeSecurity(
NULL,
-1, // COM authentication
NULL, // Authentication services
NULL, // Reserved
RPC_C_AUTHN_LEVEL_DEFAULT, // Default authentication
RPC_C_IMP_LEVEL_IMPERSONATE, // Default Impersonation
NULL, // Authentication info
EOAC_NONE, // Additional capabilities
NULL // Reserved
); if (FAILED(hres))
{
cout << "Failed to initialize security. Error code = 0x"
<< hex << hres << endl;
CoUninitialize();
return 1; // Program has failed.
}
// Step 3: ---------------------------------------------------
// Obtain the initial locator to WMI ------------------------- IWbemLocator *pLoc = NULL; hres = CoCreateInstance(
CLSID_WbemLocator,
0,
CLSCTX_INPROC_SERVER,
IID_IWbemLocator, (LPVOID *)&pLoc); if (FAILED(hres))
{
cout << "Failed to create IWbemLocator object."
<< " Err code = 0x"
<< hex << hres << endl;
CoUninitialize();
return 1; // Program has failed.
} // Step 4: ----------------------------------------------------- // Connect to WMI through the IWbemLocator::ConnectServer method
IWbemServices *pSvc = NULL;
// Connect to the root\cimv2 namespace with
// the current user and obtain pointer pSvc
// to make IWbemServices calls. hres = pLoc->ConnectServer(
_bstr_t(L"ROOT\\CIMV2"), // Object path of WMI namespace
NULL, // User name. NULL = current user
NULL, // User password. NULL = current
0, // Locale. NULL indicates current
NULL, // Security flags.
0, // Authority (e.g. Kerberos)
0, // Context object
&pSvc // pointer to IWbemServices proxy
); if (FAILED(hres))
{
cout << "Could not connect. Error code = 0x"
<< hex << hres << endl;
pLoc->Release();
CoUninitialize();
return 1; // Program has failed.
}
cout << "Connected to ROOT\\CIMV2 WMI namespace" << endl;
// Step 5: --------------------------------------------------
// Set security levels on the proxy -------------------------
hres = CoSetProxyBlanket(
pSvc, // Indicates the proxy to se
RPC_C_AUTHN_WINNT, // RPC_C_AUTHN_xxx
RPC_C_AUTHZ_NONE, // RPC_C_AUTHZ_xxx
NULL, // Server principal name
RPC_C_AUTHN_LEVEL_CALL, // RPC_C_AUTHN_LEVEL_xxx
RPC_C_IMP_LEVEL_IMPERSONATE, // RPC_C_IMP_LEVEL_xxx
NULL, // client identity
EOAC_NONE // proxy capabilities
);
if (FAILED(hres))
{
cout << "Could not set proxy blanket. Error code = 0x"
<< hex << hres << endl;
pSvc->Release();
pLoc->Release();
CoUninitialize();
return 1; // Program has failed.
}
// Step 6: --------------------------------------------------
// Use the IWbemServices pointer to make requests of WMI ----
// For example, get the name of the operating system
IEnumWbemClassObject* pEnumerator = NULL;
hres = pSvc->ExecQuery(
bstr_t("WQL"),
bstr_t("SELECT * FROM Win32_ComputerSystemProduct"),
WBEM_FLAG_FORWARD_ONLY | WBEM_FLAG_RETURN_IMMEDIATELY,
NULL,
&pEnumerator);
if (FAILED(hres))
{
cout << "Query for operating system name failed."
<< " Error code = 0x"
<< hex << hres << endl;
pSvc->Release();
pLoc->Release();
CoUninitialize();
return 1; // Program has failed.
} IWbemClassObject *pclsObj; ULONG uReturn = 0;
string test;
while (pEnumerator)
{
HRESULT hr = pEnumerator->Next(WBEM_INFINITE, 1,
&pclsObj, &uReturn);
if (0 == uReturn)
{
break;
}
VARIANT vtProp;
// Get the value of the Name property
hr = pclsObj->Get(L"UUID", 0, &vtProp, 0, 0);
wcout << " OS Name : " << vtProp.bstrVal << endl;
VariantClear(&vtProp);
//pclsObj->Release();
}
//pSvc->Release();
//pLoc->Release();
//pEnumerator->Release();
//pclsObj->Release();
//CoUninitialize();
system("PAUSE");
return 0; // Program successfully completed.
}

第二种,依托GetSystemFirmwareTable

#include <Windows.h>
#include <string>
#include <tchar.h> 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 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;
} 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; i < Smbios->Length; 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]));
}
p += h->length;
while ((*(WORD *)p) != 0) p++;
p += 2;
} getchar();
return 0; }

相关链接:http://forum.eepw.com.cn/thread/291638/1

C++中两种获取UUID的方法(编程)的更多相关文章

  1. AppCan中两种获取信息的方法

    <div id="newsInfo">正在加载...</div> 1.JSON格式: [{'R': '1','NOTI_ID': '9','NOTI_TIT ...

  2. Hibernate中两种获取Session的方式

    转自:https://www.jb51.net/article/130309.htm Session:是应用程序与数据库之间的一个会话,是hibernate运作的中心,持久层操作的基础.对象的生命周期 ...

  3. ajax请求中 两种csrftoken的发送方法

    通过ajax的方式发送两个数据进行加法运算 html页面 <body> <h3>index页面 </h3> <input type="text&qu ...

  4. objective-C 中两种实现动画的方法

    第一种方法: [UIView beginAnimations:@"Curl"context:nil];//动画开始 [UIView setAnimationDuration:1.2 ...

  5. objective-C 中两种实现动画的方法(转)

     转发自:http://wayne173.iteye.com/blog/1250232 第一种方法: [UIView beginAnimations:@"Curl"context: ...

  6. [TestNG] Eclipse/STS中两种安装TestNG的方法

    Two Ways To Install TestNG in Eclipse/STS Today I install the newest Sprint Tool Suite and want to i ...

  7. Python 两种获取文件大小的方法

    import os r=os.path.getsize("/root/catbird1.stl") f=open("/root/catbird1.stl",&q ...

  8. php 两种获取分类树的方法

    php 两种获取分类树的方法 1. /** * 获取分类树 * @param array $array 数据源 * @param int $pid 父级ID * @param int $level 分 ...

  9. JAVA 中两种判断输入的是否是数字的方法__正则化_

    JAVA 中两种判断输入的是否是数字的方法 package t0806; import java.io.*; import java.util.regex.*; public class zhengz ...

  10. Cesium 中两种添加 model 方法的区别

    概述 Cesium 中包含两种添加 model 的方法,分别为: 通过 viewer.entities.add() 函数添加 通过 viewer.scene.primitives.add() 函数添加 ...

随机推荐

  1. MySQL调优学习-快速获取占用CPU较高的SQL语句

    MySQL调优学习-快速获取占用CPU较高的SQL语句 背景 早上突然发现一个MySQL数据库的CPU使用率居高 因为是一个混布的环境上面还有一个redis 怕影响业务就上去像查看一下具体是何种原因导 ...

  2. [转帖]构建 TiFlash 副本

    https://docs.pingcap.com/zh/tidb/stable/create-tiflash-replicas#%E6%8C%89%E8%A1%A8%E6%9E%84%E5%BB%BA ...

  3. [转帖]tidb集群部署

    http://blog.itpub.net/29785807/viewspace-2789852/ 一.安装规划 1 2 3 4 5 6 使用15台服务器 5台tidb服务器:每台3个tidb实例+1 ...

  4. 不同linux发行版 FIO测试结果总结

    不同linux发行版 FIO测试结果总结 背景 机器来源 配置: 2路28核心Golden 6330 2.0Ghz 512G内存 硬盘 24块 960G SSD (22块 Raid5 + 2块 hot ...

  5. 关于JVM指针压缩性能的研究

    关于JVM指针压缩性能的研究 摘要 JVM的内存对消最小是 8bytes 所以32G内存的情况下可以使用 32位的指针就可以了. 32位就是4G 在乘以最小的内存extent 8 bytes 的出来可 ...

  6. Python学习之十一_Windows获取硬件信息

    Python学习之十一_Windows获取硬件信息 简介 网上找了一些方法简单整理了下,可以快速获取部分信息 包含机器名称等. 以及序列号相关 部分学习来源: https://blog.51cto.c ...

  7. 申威CPU的简单知识梳理

    摘要 最近有客户要用申威服务器了 自己很早之前简单测试过相关的CPU的服务器 但是感觉很多东西都不是很系统. 今天简单收集一下资料 希望对以后的工作有所帮助 申威CPU的创始 申威是解放军总参谋部第五 ...

  8. ESXi规避ESXiArgs勒索软件的简单方法

    摘要 今天查看深信服科技的公众号 发现有一个ESXiArgs 的勒索软件. 感觉对公司存在一定的风险.但是感觉操作手册有点简单. 这里想着写全面一点. 作为操作手册使用. 并且深信服仅是解决了在运行, ...

  9. 大数据面试题集锦-Hadoop面试题(三)-MapReduce

    你准备好面试了吗?这里有一些面试中可能会问到的问题以及相对应的答案.如果你需要更多的面试经验和面试题,关注一下"张飞的猪大数据分享"吧,公众号会不定时的分享相关的知识和资料. 目录 ...

  10. ABP-VNext 用户权限管理系统实战02---用户权限表的创建与迁移

    一.表实体建立 1.菜单表 [Comment("菜单表")] [Table("t_identity_menu")] public class Menu : Au ...