vc使用wmi例子
来源:互联网 发布:淘宝客服搞笑欢迎语 编辑:程序博客网 时间:2024/05/20 07:59
#define _WIN32_DCOM#define _WIN32_WINNT 0X0500 #include <iostream>#include <comdef.h>#include <conio.h>#include "wbemidl.h"#include "wmi.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;// http://www.pudn.com/downloads44/sourcecode/windows/system/detail148813.html// http://www.qingfengju.com/article.asp?id=62// http://blog.csdn.net/shiwei0124/article/details/4985790// http://topic.csdn.net/u/20100623/15/ad7c592d-2548-4aae-8ca2-18065baf6566.html// http://msdn.microsoft.com/en-us/library/aa389273(VS.85).aspxint DisplayALL( ) { 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. } int main(int argc, char **argv) {//DisplayALL(); return 0;int p = Step1()?1:Step2()?1:Step3()?1:Step4()?1:Step5();// 下面可能错误,编译器优化,先算其中某一步为0,不再算其它//int p = Step1() & Step2() & Step3() & Step4() & Step5(); if (p) goto ExitProgram;// 网卡p = Step6("SELECT * FROM Win32_NetworkAdapter");if (p) goto ExitProgram;{unsigned short MACAddress[MAX_PATH] = {0};p = Step7(L"NetConnectionID", L"本地连接", L"MACAddress", MACAddress);if (MACAddress[0] != 0) { wcout.imbue(locale(locale(""),"",LC_CTYPE));wcout << L"网卡地址: " << MACAddress << endl; } }// CPUp = Step6("SELECT * FROM Win32_Processor");if (p) goto ExitProgram;{unsigned short ProcessorId[MAX_PATH] = {0};p = Step7(L"DeviceID", L"CPU0", L"ProcessorId", ProcessorId);if (ProcessorId[0] != 0) { wcout.imbue(locale(locale(""),"",LC_CTYPE));wcout << L"处理器ID: " << ProcessorId << endl; } }// 硬盘p = Step6("SELECT * FROM Win32_PhysicalMedia");if (p) goto ExitProgram;{unsigned short SerialNumber[MAX_PATH] = {0};p = Step7(L"SerialNumber", SerialNumber);if (SerialNumber[0] != 0) { wcout.imbue(locale(locale(""),"",LC_CTYPE));wcout << L"硬盘序列号: " << SerialNumber << endl; } }// 显卡p = Step6("SELECT * FROM Win32_VideoController");if (p) goto ExitProgram;{unsigned short PNPDeviceID[MAX_PATH] = {0};p = Step7(L"PNPDeviceID", PNPDeviceID);if (PNPDeviceID[0] != 0) { wcout.imbue(locale(locale(""),"",LC_CTYPE));wcout << L"显卡序列号: " << PNPDeviceID << endl; } }// 操作系统p = Step6("SELECT * FROM Win32_OperatingSystem");if (p) goto ExitProgram;{unsigned short SerialNumber[MAX_PATH] = {0};p = Step7(L"SerialNumber", SerialNumber);if (SerialNumber[0] != 0) { wcout.imbue(locale(locale(""),"",LC_CTYPE));wcout << L"操作系统ID: " << SerialNumber << endl; } } pSvc->Release(); pLoc->Release(); ExitProgram:Step8();getch();return 0;}int Step1(){ 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. } return 0;}int Step2(){ //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; HRESULT hres; 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. } return 0;}int Step3(){ //cout << "Step 3: ---------------------------------------------------" << endl; // cout << "Obtain the initial locator to WMI -------------------------" << endl; // IWbemLocator *pLoc = NULL; HRESULT hres; 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. } return 0;}int Step4(){ //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; HRESULT hres; 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; return 0;}int Step5(){ //cout << "Step 5: --------------------------------------------------" << endl; // cout << "Set security levels on the proxy -------------------------" << endl; HRESULT hres; 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. } return 0;}int Step6(char *CmdString){ //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; HRESULT hres; hres = pSvc->ExecQuery( bstr_t("WQL"),bstr_t(CmdString),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. } return 0;}int Step7(unsigned short *Item1, unsigned short *content1, unsigned short *Item2, unsigned short *content2){ //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;// NULLhr = pclsObj->Get(Item1, 0, &vtProp, 0, 0);if(hr != WBEM_S_NO_ERROR || vtProp.bstrVal == 0 || wcscmp(vtProp.bstrVal, content1) != 0) {VariantClear(&vtProp);continue;}VariantClear(&vtProp);VariantInit(&vtProp); vtProp.bstrVal = 0;// NULL hr = pclsObj->Get(Item2, 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; wcscpy(content2, vtProp.bstrVal); } else { cout << "硬件信息 " << "vtProp.bstrVal == NULL" << endl; } VariantClear(&vtProp); } return 0;}int Step7(unsigned short *Item, unsigned short *content){ //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(Item, 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; wcscpy(content, vtProp.bstrVal); } else { cout << "硬件信息 " << "vtProp.bstrVal == NULL" << endl; } VariantClear(&vtProp); } return 0;}int Step8(){// cout << "Cleanup" << endl; // ======== if (pEnumerator) { pEnumerator->Release(); } if (pclsObj) { pclsObj->Release(); } CoUninitialize(); // cout << "end of program" << endl;return 0;}
例子:http://download.csdn.net/detail/jiftlixu/4236349
- vc使用wmi例子
- WMI编程之一:在VC中使用WMI
- WMI 例子
- VC WMI
- vc settimer 使用例子
- 在VC中使用WMI(英文原版)作者:BugSearcher
- WMI在VC中的使用 -- 设置DHCP和DNS
- VC使用WMI获取活动的网卡列表
- python 操作wmi例子
- 使用WMI
- VC WMI 2
- VC调用WMI框架
- VC++ unique_ptr的使用例子
- WMI技术介绍和应用——使用VC编写一个半同步查询WMI服务的类
- WMI技术介绍和应用——使用VC编写一个半同步查询WMI服务的类
- WMI技术介绍和应用——使用VC编写一个半同步查询WMI服务的类
- python 操作wmi例子
- VC下WMI示例代码
- 快速排序经典实现
- 通过依赖属性来实现自定义控件的属性
- suse Linux设置操作系统安装盘的iso文件为安装源安装mysql服务
- 升级vp8
- delphi下文件查找findfirst
- vc使用wmi例子
- NS3 -- 使用trace
- Linux usb驱动程序全注释
- Sql server c#扩展存储过程支持并行多线程运算
- Java编程中“为了性能”尽量要做到的一些地方
- dwz框架下刷新页面和表单
- 财务软件是企业信息化的基础
- ORA-15061 reported while doing a file operation with 11.1 or 11.2 ASM after PSU applied in database
- LINQ to Object(集运算符与JOIN)