一般步骤:
1. 通过调用CoInitialzeEx来初始化COM参数.
2. 通过调用CoInitializeSecurity来初始化COM过程安全.
3. 通过调用CoCreateInstance来实例化。
4. 通过调用IWbemLocator::ConnectServer来获取一个本机root\cimv2命名空间的IWbemServices的指针。(注意在字符串中要写“root\\cimv2”,两个\\)
5. 设置IWbemServices代理安全,WMI service可以通过调用CoSetProxyBlanket来模拟客户端。
6. 用IWbmeServices指针来查询WMI. 本例子通过调用IWbemServies::ExecQuery 来查询操作系统名称.
SELEC * FROM Win32_OperatingSystem
7. 获取和现实WQL查询的数据.
WMI 可以访问的信息类型有:
Win32_1394Controller
Win32_BaseBoard
Win32_Battery
Win32_BIOS
Win32_Bus
Win32_CacheMemory
Win32_CDROMDrive
Win32_CurrentProbe
Win32_DesktopMonitor
Win32_DeviceMemoryAddress
Win32_DiskDrive
Win32_DisplayConfiguration
Win32_DisplayControllerConfiguration
Win32_DMAChannel
Win32_Fan
Win32_FloppyController
Win32_FloppyDrive
Win32_HeatPipe
Win32_IDEController
Win32_InfraredDevice
Win32_IRQResource
Win32_Keyboard
Win32_MemoryArray
Win32_MemoryDevice
Win32_MotherboardDevice
Win32_NetworkAdapter
Win32_NetworkAdapterConfiguration
Win32_OnBoardDevice
Win32_ParallelPort
Win32_PCMCIAController
Win32_PhysicalMemory
Win32_PhysicalMemoryArray
Win32_PnPEntity
Win32_PointingDevice
Win32_PortableBattery
Win32_PortConnector
Win32_PortResource
Win32_POTSModem
Win32_PowerManagementEvent
Win32_Printer
Win32_PrinterConfiguration
Win32_PrintJob
Win32_Processor
Win32_Refrigeration
Win32_SerialPort
Win32_SerialPortConfiguration
Win32_SMBIOSMemory
Win32_SoundDevice
Win32_SystemEnclosure
Win32_SystemMemoryResource
Win32_SystemSlot
Win32_TapeDrive
Win32_TemperatureProbe
Win32_UninterruptiblePowerSupply
Win32_USBController
Win32_VideoConfiguration
Win32_VideoController
Win32_VoltageProbe
.h文件
点击(此处)折叠或打开
- /*
- 通过WMI,得到product name
- */
- #pragma once
- #include "stdafx.h"
- #include <comdef.h>
- #include <Wbemidl.h>
- #include <Wbemcli.h>
- #include <conio.h>
-
- class GetProductName
- {
- public:
- GetProductName();
- ~GetProductName();
- //正确返回0,错误返回非0
- //nName:out 得到的product name
- int Get(CString & nName);
-
- private:
- HRESULT hres;
- IEnumWbemClassObject* pEnumerator;
- IWbemLocator *pLoc;
- IWbemServices *pSvc;
-
- // Step 1:
- // Initialize COM.
- BOOL Setp1(void);
-
- // Step 2: --------------------------------------------------
- // Set general COM security levels --------------------------
- // 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 ------------------------
- BOOL Setp2(void);
-
- // Step 3: ---------------------------------------------------
- // Obtain the initial locator to WMI -------------------------
- BOOL Setp3(void);
-
- // Step 4: -----------------------------------------------------
- // Connect to WMI through the IWbemLocator::ConnectServer method
- // Connect to the root/cimv2 namespace with
- // the current user and obtain pointer pSvc
- // to make IWbemServices calls.
- BOOL Setp4(void);
-
- // Step 5: --------------------------------------------------
- // Set security levels on the proxy -------------------------
- BOOL Setp5(void);
-
- // Step 6: --------------------------------------------------
- // Use the IWbemServices pointer to make requests of WMI ----
- // get the product name of the baseboard
- BOOL Setp6(void);
-
- // Step 7: -------------------------------------------------
- // Get the data from the query in step 6 -------------------
- BOOL Setp7(CString & nName);
- };
.cpp
点击(此处)折叠或打开
- #include "stdafx.h"
- #include "GetProductName.h"
-
- #pragma comment(lib, "wbemuuid.lib")
-
- using namespace std;
-
- GetProductName::GetProductName()
- {
- hres = NULL;
- pEnumerator = NULL;
- pLoc = NULL;
- pSvc = NULL;
- }
- GetProductName::~GetProductName()
- {
- }
- BOOL GetProductName::Setp1(void)
- {
- // Step 1: --------------------------------------------------
- // Initialize COM. ------------------------------------------
-
- hres = CoInitializeEx(0, COINIT_MULTITHREADED);
- if (FAILED(hres))
- {
- return FALSE; // Program has failed.
- }
- return TRUE;
- }
- BOOL GetProductName::Setp2(void)
- {
- // Step 2: --------------------------------------------------
- // Set general COM security levels --------------------------
- // 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))
- {
- CoUninitialize();
- return FALSE; // Program has failed.
- }
- return TRUE;
- }
- BOOL GetProductName::Setp3(void)
- {
- // Step 3: ---------------------------------------------------
- // Obtain the initial locator to WMI -------------------------
-
- hres = CoCreateInstance(
- CLSID_WbemLocator,
- 0,
- CLSCTX_INPROC_SERVER,
- IID_IWbemLocator, (LPVOID *) &pLoc);
-
- if (FAILED(hres))
- {
- CoUninitialize();
- return FALSE; // Program has failed.
- }
- return TRUE;
- }
- BOOL GetProductName::Setp4(void)
- {
- // Step 4: -----------------------------------------------------
- // Connect to WMI through the IWbemLocator::ConnectServer method
-
- // 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 (e.g. Kerberos)
- 0, // Context object
- &pSvc // pointer to IWbemServices proxy
- );
-
- if (FAILED(hres))
- {
- pLoc->Release();
- CoUninitialize();
- return FALSE; // Program has failed.
- }
- return TRUE;
- }
- BOOL GetProductName::Setp5(void)
- {
- // 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))
- {
- pSvc->Release();
- pLoc->Release();
- CoUninitialize();
- return FALSE; // Program has failed.
- }
- return TRUE;
- }
- BOOL GetProductName::Setp6(void)
- {
- // Step 6: --------------------------------------------------
- // Use the IWbemServices pointer to make requests of WMI ----
- // get the product name of the baseboard
-
- hres = pSvc->ExecQuery(bstr_t("WQL"), bstr_t("SELECT * FROM Win32_BaseBoard"),
- WBEM_FLAG_FORWARD_ONLY | WBEM_FLAG_RETURN_IMMEDIATELY, NULL, &pEnumerator);
- if (FAILED(hres))
- {
- pSvc->Release();
- return FALSE; // Program has failed.
- }
- return TRUE;
- }
- BOOL GetProductName::Setp7(CString & nName)
- {
- // Step 7: -------------------------------------------------
- // Get the data from the query in step 6 -------------------
- IWbemClassObject *pclsObj = NULL;
-
- ULONG ulReturn = 0;
-
- do
- {
- HRESULT hr = pEnumerator->Next(WBEM_INFINITE, 1, &pclsObj, &ulReturn);
- if (!SUCCEEDED(hr))
- {
- break;
- return FALSE;
- }
-
- VARIANT vtProp;
- VariantInit(&vtProp);
-
- hr = pclsObj->Get(L"product", 0, &vtProp, 0, 0);
-
- nName = vtProp.bstrVal;
-
- VariantClear(&vtProp);
- } while(FALSE);
-
- pEnumerator->Release();
- pclsObj->Release();
- return TRUE;
- }
- int GetProductName::Get(CString & nName)
- {
- int lreturn = 0;
- do
- {
- if (Setp1() == FALSE)
- {
- lreturn = 1;
- break;
- }
- if (Setp2() == FALSE)
- {
- lreturn = 2;
- break;
- }
- if (Setp3() == FALSE)
- {
- lreturn = 3;
- break;
- }
- if (Setp4() == FALSE)
- {
- lreturn = 4;
- break;
- }
- if (Setp5() == FALSE)
- {
- lreturn = 5;
- break;
- }
- if (Setp6() == FALSE)
- {
- lreturn = 6;
- break;
- }
- if (Setp7(nName) == FALSE)
- {
- lreturn = 7;
- break;
- }
- } while (FALSE);
-
- // Cleanup
- pSvc->Release();
- pLoc->Release();
- CoUninitialize();
- return lreturn;
- }