转载:https://www.cnblogs.com/tlduck/p/5132738.html

 #define _WIN32_DCOM

 #include<iostream>
#include<fstream>
#include<string>
#include "direct.h"
#include <tchar.h>
#include <time.h>
#include <comdef.h>
#include <Wbemidl.h>
#include <conio.h>
#include "atlstr.h"
#include "atlbase.h"
//#include "TcpCtl.h"
//#include "winsock2.h"
//#include "InitDll.h"
using namespace std; # pragma comment(lib, "wbemuuid.lib")
# pragma comment(lib, "ws2_32.lib") //通过WMI获取主板号
BOOL ManageWMIBord(char bord[])
{
HRESULT hres;
// Step 1: 初始化COM
//hres = CoInitializeEx(0, COINIT_MULTITHREADED); //网上的代码都是使用这行语句进行初始化,但是我在实际使用中,发现也可以采用下面的语句进行初始化
hres = CoInitialize(); //网上的代码是没有注释下面这个判断的,但是实际使用中发现,如果之前已经初始化成功了,在第二次初始化的时候,下面的代码就会导致返回false,所以,实际使用中我就注释掉了
//if (FAILED(hres))
//{
// cout << "Failed to initialize COM library. Error code = 0x"
// << hex << hres << endl;
// return false; // Program has failed.
//} // Step 2: 设置COM的安全认证级别 //在实际使用过程中,我发现如果这一步不注释掉的话,程序总是返回false,注释掉之后程序反而可以正常运行,原因未知 // 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 false; // Program has failed.
////} // Step 3: 获得WMI连接COM接口
IWbemLocator *pLoc = NULL;
hres = CoCreateInstance(
CLSID_WbemLocator,
,
CLSCTX_INPROC_SERVER,
IID_IWbemLocator, (LPVOID *)&pLoc); if (FAILED(hres))
{
cout << "Failed to create IWbemLocator object."
<< " Err code = 0x"
<< hex << hres << endl;
CoUninitialize();
return false; // Program has failed.
} // Step 4: 通过连接接口连接WMI的内核对象名"ROOT//CIMV2"
IWbemServices *pSvc = NULL; hres = pLoc->ConnectServer( _bstr_t(L"ROOT\\CIMV2"), // Object path of WMI namespace
NULL, // User name. NULL = current user
NULL, // User password. NULL = current
, // Locale. NULL indicates current
NULL, // Security flags.
, // Authority (e.g. Kerberos)
, // Context object
&pSvc // pointer to IWbemServices proxy
); if (FAILED(hres))
{
cout << "Could not connect. Error code = 0x"
<< hex << hres << endl;
pLoc->Release();
CoUninitialize();
return false; // Program has failed.
}
//cout << "Connected to ROOT\\CIMV2 WMI namespace" << endl; // Step 5: 设置请求代理的安全级别
hres = CoSetProxyBlanket(
pSvc, // Indicates the proxy to set
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 false; // Program has failed.
}
// Step 6: 通过请求代理来向WMI发送请求----
// For example, get the name of the operating system
IEnumWbemClassObject* pEnumerator = NULL;
hres = pSvc->ExecQuery(
bstr_t("WQL"),
//bstr_t("SELECT * FROM Win32_NetworkAdapterConfiguration WHERE IPEnabled = 'TRUE'"),
bstr_t("SELECT * FROM Win32_BaseBoard"),//只需要通过修改这里的查询语句,就可以实现对MAC地址等其他信息的查询
WBEM_FLAG_FORWARD_ONLY | WBEM_FLAG_RETURN_IMMEDIATELY,
NULL,
&pEnumerator); if (FAILED(hres))
{
cout << "Query for Network Adapter Configuration failed."
<< " Error code = 0x"
<< hex << hres << endl;
pSvc->Release();
pLoc->Release();
CoUninitialize();
return false; // Program has failed.
} // Step 7: 循环枚举所有的结果对象 IWbemClassObject *pclsObj;
pclsObj = NULL;
ULONG uReturn = ; while (pEnumerator)
{
HRESULT hr = pEnumerator->Next(WBEM_INFINITE, ,
&pclsObj, &uReturn);
if ( == uReturn)
{
break;
}
VARIANT vtProp;
VariantInit(&vtProp); hr = pclsObj->Get(L"SerialNumber", , &vtProp, , );//查询不同的硬件信息,除了修改上面的查询语句,这里的字段也要修改 if (!FAILED(hr))
{
CW2A tmpstr1(vtProp.bstrVal);
strcpy_s(bord, , tmpstr1);//这里的200是可调的,自己根据实际情况设置,但是肯定不能大于bord的长度
//cout << "BordSN:" << sn << endl; } VariantClear(&vtProp);
}//end while // 释放资源
pSvc->Release();
pLoc->Release();
pEnumerator->Release();
pclsObj->Release();
CoUninitialize();
return true; // Program successfully completed. } int main()
{
char bord[];
ManageWMIBord(bord);
cout << bord << "\n";
system("pause");
return ;
}

运行结果:

附:

// WQL查询语句 
const T_WQL_QUERY szWQLQuery[] = { 
// 网卡原生MAC地址 
"SELECT * FROM Win32_NetworkAdapter WHERE (MACAddress IS NOT NULL) AND (NOT (PNPDeviceID LIKE 'ROOT%'))", 
L"PNPDeviceID",

// 硬盘序列号 
"SELECT * FROM Win32_DiskDrive WHERE (SerialNumber IS NOT NULL) AND (MediaType LIKE 'Fixed hard disk%')", 
L"SerialNumber",

// 主板序列号 
"SELECT * FROM Win32_BaseBoard WHERE (SerialNumber IS NOT NULL)", 
L"SerialNumber",

// 处理器ID 
"SELECT * FROM Win32_Processor WHERE (ProcessorId IS NOT NULL)", 
L"ProcessorId",

// BIOS序列号 
"SELECT * FROM Win32_BIOS WHERE (SerialNumber IS NOT NULL)", 
L"SerialNumber",

// 主板型号 
"SELECT * FROM Win32_BaseBoard WHERE (Product IS NOT NULL)", 
L"Product",

// 网卡当前MAC地址 
"SELECT * FROM Win32_NetworkAdapter WHERE (MACAddress IS NOT NULL) AND (NOT (PNPDeviceID LIKE 'ROOT%'))", 
L"MACAddress",

//当前机器的型号和厂商

"SELECT * FROM Win32_computersystem",

L"Manufacturer",L"Model"

}

通过WMI获取网卡MAC地址、硬盘序列号、主板序列号、CPU ID、BIOS序列号的更多相关文章

  1. 转: 通过WMI获取网卡MAC地址、硬盘序列号、主板序列号、CPU ID、BIOS序列号

    最近由于项目的需要,需要在程序中获取机器的硬盘序列号和MAC地址等信息,在C#下,可以很容易的获得这些信息,但是在C++程序中感觉比较麻烦.经过百度,发现很多大虾都是通过WMI来获取这些硬件信息的,网 ...

  2. (转)通过WMI获取网卡MAC地址、硬盘序列号、主板序列号、CPU ID、BIOS序列号

    最近由于项目的需要,需要在程序中获取机器的硬盘序列号和MAC地址等信息,在C#下,可以很容易的获得这些信息,但是在C++程序中感觉比较麻烦.经过百度,发现很多大虾都是通过WMI来获取这些硬件信息的,网 ...

  3. 获取网卡MAC、硬盘序列号、CPU_ID、BIOS编号

    抄来的 获取网卡MAC.硬盘序列号.CPU ID.BIOS编号 本文中所有原理及思想均取自网络,有修改.其中获取硬盘序列号.获取CPU编号.获取BIOS编号的原始代码的著作权归各自作者所有. 以下代码 ...

  4. Python 获取 网卡 MAC 地址

    /*********************************************************************** * Python 获取 网卡 MAC 地址 * 说明: ...

  5. windows平台下获取网卡MAC地址、硬盘序列号、主板序列号、CPU ID、BIOS序列号

    转自http://blog.csdn.net/jhqin/article/details/5548656,如有侵权,请联系本人删除,谢谢!! 头文件:WMI_DeviceQuery.h /* ---- ...

  6. VC++获取网卡MAC、硬盘序列号、CPU ID、BIOS编号

    以下代码可以取得系统特征码(网卡MAC.硬盘序列号.CPU ID.BIOS编号) BYTE szSystemInfo[4096]; // 在程序执行完毕后,此处存储取得的系统特征码 UINT uSys ...

  7. 使用WinPcap获取网卡MAC地址

    关键字:WinPcap 网卡 MAC地址 作者:txw1958 在WpdPack_4_1_2\WpdPack\Examples-remote\PacketDriver\GetMacAddress\目录 ...

  8. php获取网卡MAC地址源码

    <?php /** 获取网卡的MAC地址原码:目前支持WIN/LINUX系统 获取机器网卡的物理(MAC)地址 **/ class GetMacAddr{ var $return_array = ...

  9. matlab 获取网卡MAC地址

    输入命令 [sta,MACres] =  dos('getmac'); 其中MACres 存储的信息即为网卡的 相关信息. 如果想判断读取的网卡信息是否有指定信息可以如下输入 USER1 = strf ...

随机推荐

  1. SSM-整合简单配置

    首先说明Spring和Mybatis的版本: Spring:3.2.4 Mybatis:3.3.0 使用了C3P0连接池和Log4J日志,下面是jar包总览: 然后是项目目录总览: 为了能够让项目跑一 ...

  2. C变量常量

    变量是指其值可以变化的量.计算机中,指令代码.数据都存储于内存中.变量也需要存储在内存中.在计算机中,每个变量都被分配了一块内存空间,在这些空间里存储的就是变量的值.变量之所以可以变化,就是这个存储空 ...

  3. 工具系列 | git checkout 可替换命令 git switch 和 git restore

    前言 git checkout 这个命令承担了太多职责,既被用来切换分支,又被用来恢复工作区文件,对用户造成了很大的认知负担. Git社区发布了Git的新版本2.23.在该版本中,有一个特性非常引人瞩 ...

  4. Java-POJ1003-Hangover

    题目大意: 给出一个浮点数a,求出使得 不等式 1/2 + 1/3 + ... + 1/(n+1) ≥ a 成立的最小值 大水题,由于数据范围小,给出了确认上界5.20,满足二分答案 但是我懒啊,直接 ...

  5. Codeforces Round #598 (Div. 3) B Minimize the Permutation

    B. Minimize the Permutation You are given a permutation of length nn. Recall that the permutation is ...

  6. 常见css属性

    div {            width: 100px;            height: 100px;            /* 表示行高 */            line-heigh ...

  7. jquery获取select多选框选中的文本值

    $("#select option:selected").text();

  8. nmon help文档zh-han

    nmon版本16g的提示 完整的帮助信息:nmon -h 屏幕统计:nmon 数据收集:nmon -f [-s <秒>] [-c <计数>] [-t | -T] 容量计划:nm ...

  9. rabbitmq快速安装(实测有效)(新版)

    rabbitMq的快速安装和使用在第二部分,第一部分就看个热闹,第二部分直接可以完成环境的搭建 如果需要资料的话可以直接来 这里 进行下载 第一部分 http://www.cnerlang.com/r ...

  10. [LeetCode]1.Two Sum 两数之和&&第一次刷题感想

    ---恢复内容开始--- 参考博客: https://www.cnblogs.com/grandyang/p/4130379.html https://blog.csdn.net/weixin_387 ...