用C++语言怎么获取CPU的序列号
- 提问者网友:沦陷
- 2021-02-25 20:42
- 五星知识达人网友:迟山
- 2021-02-25 21:32
- 1楼网友:我住北渡口
- 2021-02-25 22:40
#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;
本文来自csdn博客,转载请标明出处: http://blog.csdn.net/honkerhero/archive/2007/01/04/1473589.aspx