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 :-(