PC Review Forums Newsgroups Windows XP Windows XP WMI VC++ security problem using WMI

Reply

VC++ security problem using WMI

 
Thread Tools Rate Thread
Old 22-06-2007, 01:38 PM   #1
Newsgroups
Guest
 
Posts: n/a
Default VC++ security problem using WMI


Hi,
I found some code from codeproject site using WMI in VC++.

I have created a method having the following code in it. It works fine when
I am running into "Admin" accound in Windows XP Pro while in "Restricted
user account", its not able to run. It's giving error messsage "Could not
enumerate" so it seems "pEnumObject" seems null or empty.
Please help,

Thanks in advance,

Code:
CoInitialize(NULL);
//Security needs to be initialized in XP first and this was the major
problem
//why it was not working in XP.
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_PhysicalMedia");

BSTR strQuery = (L"Select * from Win32_PhysicalMedia");

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"SerialNumber");

//Product

BSTR strClassProp = SysAllocString(L"SerialNumber");

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))

{

VariantClear( &v );

pIWbemLocator->Release();

pWbemServices->Release();

pEnumObject->Release();

pClassObject->Release();

CoUninitialize();

return strPath;

}

else

{

MessageBox("Error in getting object");

return;

}

VariantClear( &v );

pIWbemLocator->Release();

pWbemServices->Release();

pEnumObject->Release();

pClassObject->Release();

CoUninitialize();

return;





  Reply With Quote
Old 23-06-2007, 06:03 PM   #2
Gerry Hickman
Guest
 
Posts: n/a
Default Re: VC++ security problem using WMI

Hi,

In general, all calls related to "physical" devices require Admin rights
on Windows. You can usually get "logical" devices as a normal user but
not physical.

Newsgroups wrote:
> Hi,
> I found some code from codeproject site using WMI in VC++.
>
> I have created a method having the following code in it. It works fine when
> I am running into "Admin" accound in Windows XP Pro while in "Restricted
> user account", its not able to run. It's giving error messsage "Could not
> enumerate" so it seems "pEnumObject" seems null or empty.
> Please help,
>
> Thanks in advance,
>
> Code:
> CoInitialize(NULL);
> //Security needs to be initialized in XP first and this was the major
> problem
> //why it was not working in XP.
> 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_PhysicalMedia");
>
> BSTR strQuery = (L"Select * from Win32_PhysicalMedia");
>
> 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"SerialNumber");
>
> //Product
>
> BSTR strClassProp = SysAllocString(L"SerialNumber");
>
> 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))
>
> {
>
> VariantClear( &v );
>
> pIWbemLocator->Release();
>
> pWbemServices->Release();
>
> pEnumObject->Release();
>
> pClassObject->Release();
>
> CoUninitialize();
>
> return strPath;
>
> }
>
> else
>
> {
>
> MessageBox("Error in getting object");
>
> return;
>
> }
>
> VariantClear( &v );
>
> pIWbemLocator->Release();
>
> pWbemServices->Release();
>
> pEnumObject->Release();
>
> pClassObject->Release();
>
> CoUninitialize();
>
> return;
>
>
>
>
>



--
Gerry Hickman (London UK)
  Reply With Quote
Reply



Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off