代码框架,你所要做的仅是修改WQL语句,具体可看MSDN。以下代码为简介起见,一次返回所有的CPUID或硬盘厂商系列号
-
- #include "StdAfx.h"
- #include ".\puiddisk.h"
- #define _WIN32_DCOM
- #include <iostream>
- using namespace std;
- #include <comdef.h>
- #include <Wbemidl.h>
- # pragma comment(lib, "wbemuuid.lib")
- CpuIDDisk::CpuIDDisk(void)
- {
- GetInfomation();
- }
- CpuIDDisk::~CpuIDDisk(void)
- {
- }
- int CpuIDDisk::GetInfomation(void)
- {
- HRESULT hres;
- //步骤1:不是必须的,COM只须也只能初始化一次
- hres = CoInitializeEx(0, COINIT_MULTITHREADED );
- if (FAILED(hres))
- {
- return 1; //初始化COM异常:注意,COM只须也只能初始化一次
- }
- //步骤2:不是必须的,COM只须也只能设置一次
- //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))
- {
- CoUninitialize();
- return 1; // Program has failed.
- }
-
- //以上不是必须的,若已有“::COMInit();”,则要跳过
- //步骤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))
- {
- CoUninitialize();
- return 1;//Failed to create IWbemLocator object
- }
- //步骤4:Connect to WMI through the IWbemLocator::ConnectServer method
- IWbemServices *pSvc = NULL;
- 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 1; // Program has failed.
- }
- // 步骤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 1;
- }
- // 步骤6:Use the IWbemServices pointer to make requests of WMI ----
- IEnumWbemClassObject* pEnumerator = NULL;
- //计算CPUID
- hres = pSvc->ExecQuery(
- bstr_t("WQL"),
- bstr_t("SELECT * FROM Win32_Processor"),//Win32_OperatingSystem
- WBEM_FLAG_FORWARD_ONLY | WBEM_FLAG_RETURN_IMMEDIATELY,
- NULL,
- &pEnumerator);
-
- if (FAILED(hres))
- {
- pSvc->Release();
- pLoc->Release();
- CoUninitialize();
- return 1;
- }
- // 步骤7:Get the data from the query
- IWbemClassObject *pclsObj;
- ULONG uReturn = 0;
- while (pEnumerator)
- {
- HRESULT hr = pEnumerator->Next(WBEM_INFINITE, 1,
- &pclsObj, &uReturn);
- if(0 == uReturn)
- {
- break;
- }
- VARIANT vtProp;
- VariantInit(&vtProp);
- hr = pclsObj->Get(L"ProcessorId", 0, &vtProp, 0, 0);
- strProcessID=_com_util::ConvertBSTRToString(vtProp.bstrVal);//strProcessID:类级变量
- }
- //计算硬盘系列号
- hres = pSvc->ExecQuery(
- bstr_t("WQL"),
- bstr_t("SELECT * FROM Win32_DiskDrive"),
- WBEM_FLAG_FORWARD_ONLY | WBEM_FLAG_RETURN_IMMEDIATELY,
- NULL,
- &pEnumerator);
- if (FAILED(hres))
- {
- pSvc->Release();
- pLoc->Release();
- CoUninitialize();
- return 1;
- }
- while (pEnumerator)
- {
- HRESULT hr = pEnumerator->Next(WBEM_INFINITE, 1,
- &pclsObj, &uReturn);
- if(0 == uReturn)
- {
- break;
- }
- VARIANT vtProp;
- VariantInit(&vtProp);
- hr = pclsObj->Get(L"PNPDeviceID", 0, &vtProp, 0, 0);
- strDisk=_com_util::ConvertBSTRToString(vtProp.bstrVal);
- }
- pSvc->Release();
- pLoc->Release();
- pEnumerator->Release();
- pclsObj->Release();
- CoUninitialize();
- return 0;
- }
复制代码 |
|