一、首先是查询的表。
硬件代号为深色的,属性代号是浅色的例如(0,0)是CPU的名字 | ||
//【0】处理器ID | ||
0 | L"Name", | |
1 | L"ProcessorId", | |
2 | L"MaxClockSpeed", | //最快速度 |
3 | L"CurrentClockSpeed", | //当前速度 |
4 | L"L2CacheSize", | //二级缓存(Byte) |
5 | L"L3CacheSize", | //三级缓存(Byte) |
6 | L"Manufacturer", | //制造厂家 |
7 | L"NumberOfCores", | //核数 |
8 | L"NumberOfLogicalProcessors", | //线程数,逻辑处理器数量 |
9 | L"SystemName", | //系统名字 |
//【1】 网卡原生MAC地址 | ||
10 | L"Name", | |
11 | L"AdapterType", | //适配器类型 |
12 | L"GUID", | |
13 | L"Manufacturer", | |
14 | L"PNPDeviceID", | |
15 | L"MACAddress", | // 网卡当前MAC地址 |
16 | L"NetConnectionID", | |
17 | L"Speed", | |
//【2】 硬盘序列号 | ||
18 | L"Name", | |
19 | L"SerialNumber", | |
20 | L"BytesPerSector", | |
21 | L"FirmwareRevision", | |
22 | L"InterfaceType", | |
23 | L"Manufacturer", | |
24 | L"Model", | |
25 | L"Partitions", | //分区个数 |
26 | L"PNPDeviceID", | |
27 | L"SerialNumber", | |
28 | L"Signature", | |
29 | L"Size", | |
30 | L"TotalCylinders", | |
31 | L"TotalHeads", | |
32 | L"TotalSectors", | |
33 | L"TotalTracks", | |
34 | L"TracksPerCylinder", | |
//【3】 主板序列号 | ||
35 | L"Manufacturer", | |
36 | L"Product", | // 主板型号 |
37 | L"SerialNumber", | |
//【4】BIOS | ||
38 | L"CurrentLanguage", | |
39 | L"ReleaseDate", | |
40 | L"SerialNumber", | |
41 | L"SMBIOSBIOSVersion", | |
42 | L"Version", | |
//【5】内存 | ||
43 | L"Capacity" | ,//容量 |
44 | L"DeviceLocator", | |
45 | L"FormFactor", | |
46 | L"Manufacturer", | |
47 | L"PartNumber", | |
48 | L"SerialNumber", | |
49 | L"Speed", | |
//【6】 键盘 | ||
50 | L"Description", | |
51 | L"DeviceID", | |
52 | L"PNPDeviceID", | |
//【7】 点输入设备,包括鼠标。 | ||
53 | L"Description", | |
54 | L"DeviceID", | |
55 | L"Manufacturer", | |
56 | L"PNPDeviceID", | |
//【8】 光盘驱动器(57-62) | ||
57 | L"Description", | |
58 | L"DeviceID", | |
59 | L"Drive", | |
60 | L"MediaType", | |
61 | L"Manufacturer", | |
62 | L"PNPDeviceID", | |
//【9】 串口(63-66) | ||
63 | L"Description", | |
64 | L"DeviceID", | |
65 | L"MaxBaudRate", | |
66 | L"PNPDeviceID", | |
// 【10】串口配置 (67-67) | ||
67 | L"Description", | |
// 【11】多媒体设置,一般指声卡(68-70) | ||
68 | L"Description", | |
69 | L"Manufacturer", | |
70 | L"PNPDeviceID", | |
//【12】 主板插槽 (ISA & PCI & AGP)(71-71) | ||
71 | L"SlotDesignation", | |
// 【13】USB 控制器 (72-74) | ||
72 | L"Description", | |
73 | L"Manufacturer", | |
74 | L"PNPDeviceID", | |
// 【14】显示器 (75-80) | ||
75 | L"Description", | |
76 | L"MonitorManufacturer", | |
77 | L"MonitorType", | |
78 | L"PNPDeviceID", | |
79 | L"ScreenHeight", | |
80 | L"ScreenWidth", | |
// 【15】显卡细节。 (81-94) | ||
81 | L"AdapterDACType", | |
82 | L"AdapterRAM", | |
83 | L"Availability", | |
84 | L"CurrentRefreshRate", | |
85 | L"CurrentScanMode", | |
86 | L"CurrentVerticalResolution", | |
87 | L"Description", | |
88 | L"DriverDate", | |
89 | L"DriverVersion", | |
90 | L"MaxRefreshRate", | |
91 | L"PNPDeviceID", | |
92 | L"VideoMemoryType", | |
93 | L"VideoModeDescription", | |
94 | L"VideoProcessor", | |
// 【16】显卡设置(95-102) | ||
95 | L"BitsPerPixel", | |
96 | L"ColorPlanes", | |
97 | L"Description", | |
98 | L"DeviceSpecificPens", | |
99 | L"HorizontalResolution", | |
100 | L"RefreshRate", | |
101 | L"VerticalResolution", | |
102 | L"VideoMode", |
#define _WIN32_DCOM
#include "stdafx.h"
#include <iostream>
using namespace std;
#include <comdef.h>
#include <Wbemidl.h>
# pragma comment(lib, "wbemuuid.lib")
int main(int argc, char **argv)
{
HRESULT hres;
// 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.
}
// Initialize
hres = CoInitializeSecurity(
NULL,
-1, // COM negotiates service
NULL, // Authentication services
NULL, // Reserved
RPC_C_AUTHN_LEVEL_DEFAULT, // authentication
RPC_C_IMP_LEVEL_IMPERSONATE, // 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.
}
// Obtain the initial locator to Windows Management
// on a particular host computer.
IWbemLocator *pLoc = 0;
hres = CoCreateInstance(
CLSID_WbemLocator,
0,
CLSCTX_INPROC_SERVER,
IID_IWbemLocator, (LPVOID *) &pLoc);
if (FAILED(hres))
{
cout << "Failed to create IWbemLocator object. "
<< "Error code = 0x"
<< hex << hres << endl;
CoUninitialize();
return 1; // Program has failed.
}
IWbemServices *pSvc = 0;
// 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"), // WMI namespace
NULL, // User name
NULL, // User password
0, // Locale
NULL, // Security flags
0, // Authority
0, // Context object
&pSvc // 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;
// Set the IWbemServices proxy so that impersonation
// of the user (client) occurs.
hres = CoSetProxyBlanket(
pSvc, // the proxy to set
RPC_C_AUTHN_WINNT, // authentication service
RPC_C_AUTHZ_NONE, // authorization service
NULL, // Server principal name
RPC_C_AUTHN_LEVEL_CALL, // authentication level
RPC_C_IMP_LEVEL_IMPERSONATE, // impersonation level
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.
}
// Use the IWbemServices pointer to make requests of WMI.
// Make requests here:
// For example, query for all the running processes
IEnumWbemClassObject* pEnumerator = NULL;
hres = pSvc->ExecQuery(
bstr_t("WQL"),
bstr_t("SELECT * FROM Win32_Process"),
WBEM_FLAG_FORWARD_ONLY | WBEM_FLAG_RETURN_IMMEDIATELY,
NULL,
&pEnumerator);
if (FAILED(hres))
{
cout << "Query for processes failed. "
<< "Error code = 0x"
<< hex << hres << endl;
pSvc->Release();
pLoc->Release();
CoUninitialize();
return 1; // Program has failed.
}
else
{
IWbemClassObject *pclsObj;
ULONG uReturn = 0;
while (pEnumerator)
{
hres = pEnumerator->Next(WBEM_INFINITE, 1,
&pclsObj, &uReturn);
if(0 == uReturn)
{
break;
}
VARIANT vtProp;
// Get the value of the Name property
hres = pclsObj->Get(L"Name", 0, &vtProp, 0, 0);
wcout << "Process Name : " << vtProp.bstrVal << endl;
VariantClear(&vtProp);
}
}
// Cleanup
// ========
pSvc->Release();
pLoc->Release();
CoUninitialize();
return 0; // Program successfully completed.
}
整理之后:
WMIQuery test0;
test0.Initial();
if(!test0.WQLquery(0,0,teststr))teststr="查询失败";
else cout<<"teststr";
这句得到的是CPU名字 根据上表查询。
下面给出下载地址:
整理好的类:http://download.youkuaiyun.com/detail/ssutlyh/6545023
MFC demon:http://pan.baidu.com/s/1y2j3s
附上非WQL获取CPU序列号硬盘序列号:http://pan.baidu.com/s/1rGn8C