无法使用WMI类c ++获取CPU的UniqueId

时间:2012-10-08 09:23:19

标签: c++ visual-c++ mfc wmi wmi-query

在应用程序中,它需要检索系统特定的UniqueId。所以我试图检索CPU的UniqueId。我正在使用WMI类属性来检索它。 但是在尝试检索win32_processor类的“UniqueId”属性时,它会在期望输出的变量中返回VT_NULL。但同时它为其他属性提供了有效的输出,如deciveId,processorId等,但这些并不是唯一的,不能满足我的目的。

任何人都知道为什么会这样吗?请帮助我......

下面是我使用的代码...请看看,并说明是否有任何问题.... 我可以做些什么修改才能让它发挥作用...

   if(CoInitializeSecurity( NULL,
-1,
NULL,
NULL,
RPC_C_AUTHN_LEVEL_PKT,
RPC_C_IMP_LEVEL_IMPERSONATE,
NULL,
EOAC_NONE,
0
) != S_OK)
    return;

IWbemLocator * pIWbemLocator = NULL;
IWbemServices * pWbemServices = NULL;
IEnumWbemClassObject * pEnumObject  = NULL;

BSTR bstrNamespace = (L"root\\cimv2");


if(CoCreateInstance (
        CLSID_WbemAdministrativeLocator,
        NULL ,
        CLSCTX_INPROC_SERVER | CLSCTX_LOCAL_SERVER , 
        IID_IUnknown ,
        ( void ** ) & pIWbemLocator
        ) != S_OK)
            return;

if(pIWbemLocator->ConnectServer(
            bstrNamespace,  // Namespace
            NULL,          // Userid
            NULL,           // PW
            NULL,           // Locale
            0,              // flags
            NULL,           // Authority
            NULL,           // Context
            &pWbemServices
            ) != S_OK)
            return;



HRESULT hRes;
BSTR strQuery = (L"Select * from Win32_Processor");
BSTR strQL = (L"WQL");
hRes = pWbemServices->ExecQuery(strQL, strQuery,WBEM_FLAG_RETURN_IMMEDIATELY,NULL,&pEnumObject);

if(hRes != S_OK)
{
    MessageBox("Could not execute Query");
    return;
}

ULONG uCount = 1, uReturned;
IWbemClassObject * pClassObject = NULL;


hRes = pEnumObject->Reset();

if(hRes != S_OK)
{
    MessageBox("Could not Enumerate");
    return;
}

hRes = pEnumObject->Next(WBEM_INFINITE,uCount, &pClassObject, &uReturned);
if(hRes != S_OK)
{
    MessageBox("Could not Enumerate");
    return;
}

VARIANT v;


BSTR strClassProp = SysAllocString(L"UniqueId");

hRes = pClassObject->Get(strClassProp, 0, &v, 0, 0);// here the v is VT_NULL but works if the vlaue of strClassProp is processerId, deviceId
if(hRes != S_OK)
{
    MessageBox("Could not Get Value");
    return;
}

SysFreeString(strClassProp);

_bstr_t bstrPath = &v;  //it causes exception if the v is VT_NULL in other cases its working

char* strPath = new char [1024];
_stprintf(strPath, ("%s"),(char*)bstrPath);


if (SUCCEEDED(hRes))
    MessageBox(strPath);

期待专家建议和帮助...... 在此先感谢....

0 个答案:

没有答案