Hello,
I am trying to use WMI components in VC++. Code is working for bstrNamespace passed to ConnectServer
if it is “root/cmiv2”. However for the real purpose of gettting information from remote machine like
“//ps2636/root/cmiv2” API failed with error as 0x80041008 An invalid parameter was specified.
If someone has know workaround for this issue, let me know.
Thanks in advance.
~Rajesh
Below is code for the same:
CoInitializeEx(NULL, COINIT_MULTITHREADED);
try
{
HRESULT hRes;
IWbemLocator * pIWbemLocator = NULL;
IWbemServices * pWbemServices ;
IEnumWbemClassObject * pEnumObject = NULL;
_bstr_t bstrNamespace = (L"//ps2636/root/CIMV2");
// BSTR bstrNamespace = (L"root/CIMV2");
if(CoInitializeSecurity( NULL,
-1,
NULL,
NULL,
RPC_C_AUTHN_LEVEL_PKT,
RPC_C_IMP_LEVEL_IMPERSONATE,
NULL,
EOAC_NONE,
0
) != S_OK)
return;
if(CoCreateInstance (
__uuidof (WbemAdministrativeLocator),
NULL ,
CLSCTX_INPROC_SERVER, //| CLSCTX_LOCAL_SERVER ,
IID_IUnknown ,
( void ** ) & pIWbemLocator
) != S_OK)
return;
/////
// Here is culprit
////
hRes = pIWbemLocator->ConnectServer(
bstrNamespace, // Namespace
NULL, // Userid
NULL, // PW
NULL, // Locale
0, // flags
NULL, // Authority
NULL, // Context
&pWbemServices
) ;
if(hRes != S_OK)
{
// API Fails with error 0x80041008 An invalid parameter was specified, or the namespace could not be parsed.
return;
}
// BSTR strQuery = (L"Select * from win32_Processor");
BSTR strQuery = (L"Select * from win32_OperatingSystem");
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 ;
// BSTR strClassProp = SysAllocString(L"LoadPercentage");
// strClassProp = SysAllocString(L"Caption");
// strClassProp = SysAllocString(L"CSName");
strClassProp = SysAllocString(L"Version");
hRes = pClassObject->Get(strClassProp, 0, &v, 0, 0);
if(hRes != S_OK)
{
MessageBox(“Could not Get Value”);
return;
}
SysFreeString(strClassProp);
_bstr_t bstrPath = &v; //Just to convert BSTR to ANSI
char* strPath=(char*)bstrPath;
if (SUCCEEDED(hRes))
MessageBox(strPath);
else
MessageBox(“Error in getting object”);
VariantClear( &v );
strClassProp = SysAllocString(L"ServicePackMajorVersion");
hRes = pClassObject->Get(strClassProp, 0, &v, 0, 0);
if(hRes != S_OK)
{
MessageBox(“Could not Get Value”);
return;
}
SysFreeString(strClassProp);
int nServicePackMajorVersion= v.iVal; //Just to convert BSTR to ANSI
VariantClear( &v );
pIWbemLocator->Release();
pWbemServices->Release();
pEnumObject->Release();
pClassObject->Release();
}
catch (_com_error err)
{
MessageBox (err.ErrorMessage(), 0, 0) ;
}
CoUninitialize();