-
September 5th, 2012, 09:55 AM
#1
Variables & WMI
Hi everybody I am currently trying to create a C++ program that collects data from WMI through a function and then stores them into seperate variables so as it can be used later. However I am running into a snag, I think it might have something to do with the way I am assigning variables but unsure, i can get the results to my queries fine, but then when I try to display the variables later they do not make sense...
This is my function:
Code:
WCHAR* WMIQuery(char* QueryName, char* ClassName, char* PropertyName, char* QueryArgument)
{
WCHAR* QueryResult;
// Display Current Objective
printf("Looking for %s...\n", QueryName);
// Create BSTR variable containing query
CComBSTR querystr(OLESTR("SELECT * FROM "));
querystr.Append(ClassName);
querystr.Append(QueryArgument);
BSTR WQLQuery = querystr.Detach();
// Create BSTR variable containt propertyname
CComBSTR propertystr;
propertystr.Append(PropertyName);
BSTR NameQuery = propertystr.Detach();
// Use the IWbemServices pointer to make requests of WMI
printf("Query %s Class...\n", ClassName);
IEnumWbemClassObject* pEnumerator = NULL;
hr = pSvc->ExecQuery(
BSTR(L"WQL"),
BSTR(WQLQuery),
WBEM_FLAG_FORWARD_ONLY | WBEM_FLAG_RETURN_IMMEDIATELY,
NULL,
&pEnumerator);
if (FAILED(hr)) // Program has failed.
{
cout << "Query failed. " << "Error code = 0x" << hex << hr << endl;
pSvc->Release();
pLoc->Release();
CoUninitialize();
}
else
{
printf("Getting Value of %s property from class...",PropertyName);
IWbemClassObject *pclsObj;
ULONG uReturn = 0;
while (pEnumerator)
{
hr = pEnumerator->Next(WBEM_INFINITE, 1, &pclsObj, &uReturn);
if(0 == uReturn)
{
break;
}
VARIANT vtProp;
// Get the value of the Name property from class
hr = pclsObj->Get(NameQuery, 0, &vtProp, 0, 0);
// Display Resuly
printf("Result: %ls\n",vtProp.bstrVal);
// Copy result into a usable variable
QueryResult = vtProp.bstrVal;
// Clear vtprop
VariantClear(&vtProp);
}
}
return QueryResult;
}
This is my call:
Code:
WCHAR* WS_Name = WMIQuery("Computer Name", "Win32_ComputerSystem", "Name", ""); // Computer Name
WCHAR* WS_Domain = WMIQuery("Domain Name", "Win32_ComputerSystem", "Domain", ""); // Domain Name
printf("Display ALL Results...\n");
printf("Machine Name: %ls\n", WS_Name);
printf("Domain Name: %ls\n", WS_Domain);
but my results look like this:
Code:
Looking for Computer Name...
Query Win32_ComputerSystem Class...
Getting Value of Name property from class...Result: Home-11
Result: Home-11
Looking for Domain Name...
Query Win32_ComputerSystem Class...
Getting Value of Domain property from class...Result: home.local
Result: home.local
Display ALL Results...
Machine Name: Domain
Domain Name: home.local
Sorry its long, but any help would be greatly appreciated, I know i'm overlooking something stupid :-(
-
September 5th, 2012, 11:15 AM
#2
Re: Variables & WMI
Originally Posted by Simoninman
This is my function:
Code:
WCHAR* WMIQuery(char* QueryName, char* ClassName, char* PropertyName, char* QueryArgument)
{
WCHAR* QueryResult;
...
return QueryResult;
}
You return a pointer to WCHAR that is defined locally within the WMIQuery() function.
But it goes out of scope just before this function returns, so the return value points to some garbage!
Either return CString (or std::wstring) objects or add additional parameter (WCHAR* QueryResult) in the argument list of the function.
Victor Nijegorodov
-
September 5th, 2012, 11:21 AM
#3
Re: Variables & WMI
I'm not really up on the code he's using, but it seems that the code here probably contributes.
VARIANT vtProp;
...
QueryResult = vtProp.bstrVal;
...
VariantClear(&vtProp);
...
return QueryResult;
Looks more like he's returning a pointer to a variant that's getting cleared then going out of scope.
Last edited by GCDEF; September 5th, 2012 at 11:26 AM.
-
September 5th, 2012, 11:35 AM
#4
Re: Variables & WMI
Originally Posted by GCDEF
...
Looks more like he's returning a pointer to a variant that's getting cleared then going out of scope.
You are probably right.
And sorry, I didn't have enough time to analyze the function body. I only looked at the how return value was defined.
Victor Nijegorodov
-
September 7th, 2012, 05:27 AM
#5
Re: Variables & WMI
Thank you for your help, Still new to all the variable types, so didn't see that :-s
Thank you again
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|