WMI(Windows Management Instrumentation,Windows 管理规范)是一项核心的Windows管理技术,WMI作为一种规范和基础结构,通过它可以访问、配置、管理和监视几乎所有的Windows资源,比如用户可以在远程计算机器上启动一个进程;设定一个在特定日期和时间运行的进程;远程启动计算机;获得本地或远程计算机的已安装程序列表;查询本地或远程计算机的Windows事件日志等等。
VC下的示例代码如下(复制可用):
#define _WIN32_DCOM
#include <iostream>
#include <comdef.h>
#include <conio.h>
#include "wbemidl.h"
#pragma comment(lib,"WbemUuid.lib")
using namespace std;
static IWbemLocator *pLoc = NULL;
static IWbemServices *pSvc = NULL;
static IEnumWbemClassObject* pEnumerator = NULL;
static IWbemClassObject* pclsObj = NULL;
int main(int argc, char* argv[])
{
HRESULT hres;
cout << "Step 1: --------------------------------------------------" << endl;
cout << "Initialize COM. ------------------------------------------" << endl;
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.
}
cout << "Step 2: --------------------------------------------------" << endl;
cout << "Set general COM security levels --------------------------" << endl;
cout << "Note: If you are using Windows 2000, you need to specify -" << endl;
cout << "the default authentication credentials for a user by using" << endl;
cout << "a SOLE_AUTHENTICATION_LIST structure in the pAuthList ----" << endl;
cout << "parameter of CoInitializeSecurity ------------------------" << endl;
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.
}
cout << "Step 3: ---------------------------------------------------" << endl;
cout << "Obtain the initial locator to WMI -------------------------" << endl;
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.
}
cout << "Step 4: -----------------------------------------------------" << endl;
cout << "Connect to WMI through the IWbemLocator::ConnectServer method" << endl;
IWbemServices *pSvc = NULL;
cout << "Connect to the root\\cimv2 namespace with" << endl;
cout << "the current user and obtain pointer pSvc" << endl;
cout << "to make IWbemServices calls." << endl;
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;
cout << "Step 5: --------------------------------------------------" << endl;
cout << "Set security levels on the proxy -------------------------" << endl;
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.
}
cout << "Step 6: --------------------------------------------------" << endl;
cout << "Use the IWbemServices pointer to make requests of WMI ----" << endl;
// For example, get the name of the operating system
IEnumWbemClassObject* pEnumerator = NULL;
hres = pSvc->ExecQuery(
bstr_t("WQL"),
//
bstr_t("SELECT * FROM Win32_SoundDevice"),
// 音频设备
//
bstr_t("SELECT * FROM Win32_Product"),
//
bstr_t("SELECT * FROM Win32_Registry"),
// 注册表
//
bstr_t("SELECT * FROM Win32_OnBoardDevice"),
// 主板上的嵌入设备
//
bstr_t("SELECT * FROM Win32_MotherboardDevice"),
// 母板
//
bstr_t("SELECT * FROM Win32_BaseBoard"),
// 主板
//
bstr_t("SELECT * FROM Win32_DMAChannel"),
// DMA通道
//
bstr_t("SELECT * FROM Win32_Bus"),
// 总线
//
bstr_t("SELECT * FROM Win32_BIOS"),
// BIOS系统
//
bstr_t("SELECT * FROM Win32_SystemBIOS"),
//
bstr_t("SELECT * FROM Win32_Processor"),
// 处理器
//
bstr_t("SELECT * FROM Win32_SystemProcesses"),
//
//
bstr_t("SELECT * FROM Win32_Process"),
// 系统进程
//
bstr_t("SELECT * FROM Win32_Thread"),
// 系统线程
//
bstr_t("SELECT * FROM Win32_AssociatedProcessorMemory"),
// CACHE
//
bstr_t("SELECT * FROM Win32_CacheMemory"),
// 二级缓存内存
//
bstr_t("SELECT * FROM Win32_PhysicalMedia"),
// 物理媒体信息 (硬盘)
//
bstr_t("SELECT * FROM Win32_LogicalDisk "),
// 逻辑驱动器
//
bstr_t("SELECT * FROM Win32_DiskDrive"),
// 磁盘驱动器
//
bstr_t("SELECT * FROM Win32_MemoryDevice"),
// 内存设备
//
bstr_t("SELECT * FROM Win32_PhysicalMemoryArray"),
// 物理内存数组
//
bstr_t("SELECT * FROM Win32_PhysicalMemoryLocation"),
// 物理内存位置
//
bstr_t("SELECT * FROM CIM_NumericSensor"),
// 数字传感器
//
bstr_t("SELECT * FROM Win32_VoltageProbe"),
// 数字传感器
//
bstr_t("SELECT * FROM Win32_TemperatureProbe"),
// 温度传感器
//
bstr_t("SELECT * FROM Win32_CurrentProbe"),
//
bstr_t("SELECT * FROM Win32_OperatingSystem"),
// 操作系统
//
bstr_t("SELECT * FROM Win32_UserAccount"),
// 用户账号
//
bstr_t("SELECT * FROM Win32_SerialPort"),
// 串行接口
//
bstr_t("SELECT * FROM Win32_ParallelPort"),
// 并行接口
//
bstr_t("SELECT * FROM Win32_SCSIController"),
// 小型计算机系统接口
//
bstr_t("SELECT * FROM Win32_PortResource"),
// I/O 端口
//
bstr_t("SELECT * FROM Win32_PNPDevice"),
// 即插即用设备
//
bstr_t("SELECT * FROM Win32_NetworkAdapter"),
// 网络适配器
bstr_t("SELECT * FROM Win32_NetworkAdapterConfiguration"),
//
bstr_t("SELECT * FROM Win32_NetworkAdapterSetting"),
//
bstr_t("SELECT * FROM Win32_AssociatedBattery"),
//
bstr_t("SELECT * FROM Win32_Battery"),
// 内部电池
//
bstr_t("SELECT * FROM Win32_PortableBattery"),
//
bstr_t("SELECT * FROM Win32_PowerManagementEvent"),
//
bstr_t("SELECT * FROM Win32_UninterruptiblePowerSupply"),
//
bstr_t("SELECT * FROM Win32_DriverForDevice"),
//
bstr_t("SELECT * FROM Win32_Printer"),
// 打印机
//
bstr_t("SELECT * FROM Win32_TCPIPPrinterPort"),
//
bstr_t("SELECT * FROM Win32_POTSModem"),
//
bstr_t("SELECT * FROM Win32_DesktopMonitor"),
// 显示器
//bstr_t("SELECT * FROM Win32_VideoController"),
// 显卡
//
bstr_t("SELECT * FROM Win32_CDROMDrive"),
//
bstr_t("SELECT * FROM Win32_Keyboard"),
// 键盘
//
bstr_t("SELECT * FROM Win32_AutochkSetting"),
//
bstr_t("SELECT * FROM Win32_PointingDevice"),
// 点击设备:鼠标、触摸板
//
bstr_t("SELECT * FROM Win32_Fan"),
// 风扇
//
bstr_t("SELECT * FROM Win32_WMISetting"),
//
bstr_t("SELECT * FROM Win32_TimeZone"),
//
bstr_t("SELECT * FROM Win32_Environment"),
// 环境路径
//
bstr_t("SELECT * FROM Win32_QuotaSetting"),
//
bstr_t("SELECT * FROM Win32_NetworkProtocol"),
// 己安装的网络协议
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.
}
cout << "Step 7: -------------------------------------------------" << endl;
cout << "Get the data from the query in step 6 -------------------" << endl;
IWbemClassObject* pclsObj = NULL;
ULONG uReturn = 0;
while (pEnumerator)
{
cout << "pEnumerator==>" << pEnumerator << endl;
HRESULT hr = pEnumerator->Next(WBEM_INFINITE, 1, &pclsObj, &uReturn);
cout << "pclsObj==>" << pclsObj << " NO. object found" << endl;
if ((0 == uReturn) || (0 == pclsObj))
{
break;
}
VARIANT vtProp;
// Get the value of the Name property
VariantInit(&vtProp);
vtProp.bstrVal = 0;// NULL
// hr = pclsObj->Get(L"MACAddress", 0, &vtProp, 0, 0);
hr = pclsObj->GetObjectText(0, &vtProp.bstrVal);
if (vtProp.bstrVal != 0)
{
wcout.imbue(locale(locale(""),"",LC_CTYPE));
wcout << L"硬件信息: " << vtProp.bstrVal << endl;
}
else
{
cout << "硬件信息 " << "vtProp.bstrVal == NULL" << endl;
}
VariantClear(&vtProp);
}
cout << "Cleanup" << endl;
// ========
pSvc->Release();
pLoc->Release();
if (pEnumerator)
{
pEnumerator->Release();
}
if (pclsObj)
{
pclsObj->Release();
}
CoUninitialize();
cout << "end of program" << endl;
return 0; // Program successfully completed.
}