CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5

Thread: Variables & WMI

Hybrid View

  1. #1
    Join Date
    Aug 2012
    Posts
    4

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

  2. #2
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,396

    Re: Variables & WMI

    Quote Originally Posted by Simoninman View Post
    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

  3. #3
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,635

    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.

  4. #4
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,396

    Re: Variables & WMI

    Quote Originally Posted by GCDEF View Post
    ...
    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

  5. #5
    Join Date
    Aug 2012
    Posts
    4

    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
  •  





Click Here to Expand Forum to Full Width

Featured