前言
好久不玩WMI, 已经不知道怎么写了。
找到msdn的一个例子。
实验
// WMI_test.cpp : 此文件包含 "main" 函数。程序执行将在此处开始并结束。
//
#include "pch.h"
#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 --------------------------
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 (for example, 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 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 1; // Program has failed.
}
// Step 6: --------------------------------------------------
// Use the IWbemServices pointer to make requests of WMI ----
// For example, get the name of the operating system
// 在这就可以访问WMI中的命名空间的名称了. e.g. Win32_OperatingSystem
IEnumWbemClassObject* pEnumerator = NULL;
hres = pSvc->ExecQuery(
bstr_t("WQL"),
bstr_t("SELECT * FROM Win32_OperatingSystem"),
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.
}
// Step 7: -------------------------------------------------
// Get the data from the query in step 6 -------------------
IWbemClassObject *pclsObj = NULL;
ULONG uReturn = 0;
while (pEnumerator)
{
HRESULT hr = pEnumerator->Next(WBEM_INFINITE, 1,
&pclsObj, &uReturn);
if (0 == uReturn)
{
break;
}
VARIANT vtProp;
// win32-operatingsystem 的属性定义
// https://docs.microsoft.com/en-us/windows/desktop/cimwin32prov/win32-operatingsystem
// 取属性内容, 试试各种属性的类型(string, int, bool, etc..)
// Data type: string
hr = pclsObj->Get(L"BootDevice", 0, &vtProp, 0, 0);
wprintf(L"BootDevice : %s\n", vtProp.bstrVal);
VariantClear(&vtProp);
// Data type: sint16
hr = pclsObj->Get(L"CurrentTimeZone", 0, &vtProp, 0, 0);
wprintf(L"CurrentTimeZone : %d\n", vtProp.uiVal);
VariantClear(&vtProp);
// Data type: boolean
hr = pclsObj->Get(L"DataExecutionPrevention_32BitApplications", 0, &vtProp, 0, 0);
wprintf(L"DataExecutionPrevention_32BitApplications : %s\n", vtProp.boolVal ? L"true" : L"false");
VariantClear(&vtProp);
// Data type: datetime
hr = pclsObj->Get(L"InstallDate", 0, &vtProp, 0, 0);
// + bstrVal 0x012e4924 L"20180804002635.000000+480" wchar_t *
// 不知道怎么将返回的时间值专程时间字符串
// C#的实现msdn上有, c++的没有
wprintf(L"InstallDate : %s\n", vtProp.bstrVal);
VariantClear(&vtProp);
// 不玩了, 返回值所在的正确的成员可以在vs2017调试器的监视窗口中看到
// 然后展开vtProp的节点,看哪个成员和vtProp顶层节点的值相同,就用哪个值
/**
- vtProp BSTR = 0x00b7424c L"20180804002635.000000+480" tagVARIANT
+ BSTR 0x00b7424c L"20180804002635.000000+480" wchar_t *
vt 0x0008 unsigned short
+ [原始视图] {vt=0x0008 wReserved1=0x0000 wReserved2=0x0000 ...} tagVARIANT
*/
pclsObj->Release();
}
// Cleanup
// ========
pSvc->Release();
pLoc->Release();
pEnumerator->Release();
CoUninitialize();
return 0; // Program successfully completed.
}