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

Thread: WMI VARIANT read CimType = Object

  1. #1
    Join Date
    Mar 2019
    Posts
    9

    WMI VARIANT read CimType = Object

    Hi,

    i want to read from a VARIANT (CimType = Object) the properties

    namespace ROOT\CIMV2
    class Win32_Volume
    methode : DefragAnalysis

    Code:
    VARIANT varReturnValue_DefragAnalysis;
    CIMTYPE cimtype;
    pOutParams->Get(L"DefragAnalysis", 0, &varReturnValue_DefragAnalysis, &cimtype, 0);

    Thanks in advance.

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

    Re: WMI VARIANT read CimType = Object

    Quote Originally Posted by carre View Post
    Thanks in advance.
    You are welcome!

    But what is your problem?
    Victor Nijegorodov

  3. #3
    Join Date
    Mar 2019
    Posts
    9

    Re: WMI VARIANT read CimType = Object

    Hi Victor,

    my problem is, i know from (https://docs.microsoft.com/en-us/pre...s-win32-volume) the DefragAnalysis method returns '[out] object DefragAnalysis' and with get i can store the 'object' in a VARIANT and i know this object should have this parameters (https://docs.microsoft.com/en-us/pre...defraganalysis) but i have currently no idea what is necessary to do to receive this parameters out of the VARIANT.

    Thanks

  4. #4
    Arjay's Avatar
    Arjay is offline Moderator / MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,032

    Re: WMI VARIANT read CimType = Object

    It doesn't look like your are calling the method properly (it looks like you are calling it like a property). It's been years since I've done any WMI and that was in C# and even longer when I worked with it in C++.

    At any rate, take a look at the zipped up sample projects in my replies in this post http://forums.codeguru.com/showthrea...Code-Included).

    They might help. Specifically, see post #12.
    Last edited by Arjay; July 11th, 2019 at 11:07 PM.

  5. #5
    Join Date
    Mar 2019
    Posts
    9

    Re: WMI VARIANT read CimType = Object

    Hi Arjay,

    thanks for your tip, here is the example (mainly from MS sides + your recommendation from your post):

    Code:
    #define _WIN32_DCOM
    #include <iostream>
    #include <wbemidl.h>
    #include <comdef.h>
    #include <icrsint.h>
    using namespace std;
    #pragma comment(lib, "wbemuuid.lib")
    int main()
    {
    	auto hr = S_OK;
    	hr = CoInitializeEx(NULL, COINIT_MULTITHREADED);
    	if (SUCCEEDED(hr))
    	{
    		_COM_SMARTPTR_TYPEDEF(IWbemLocator, _uuidof(IWbemLocator));
    		IWbemLocatorPtr pLocator = NULL;
    		_COM_SMARTPTR_TYPEDEF(IWbemServices, _uuidof(IWbemServices));
    		IWbemServicesPtr pService = NULL;
    		_COM_SMARTPTR_TYPEDEF(IWbemClassObject, _uuidof(IWbemClassObject));
    		IWbemClassObjectPtr pClass = NULL;
    		IWbemClassObjectPtr pInParamsDefinition = NULL;
    		IWbemClassObjectPtr pClassInstance = NULL;
    		_COM_SMARTPTR_TYPEDEF(IEnumWbemClassObject, _uuidof(IEnumWbemClassObject));
    		IEnumWbemClassObjectPtr pEnumerator = NULL;
    
    		BSTR path = SysAllocString(L"ROOT\\CIMV2");
    		do
    		{
    			hr = CoInitializeSecurity(NULL, -1, NULL, NULL, RPC_C_AUTHN_LEVEL_DEFAULT, RPC_C_IMP_LEVEL_IMPERSONATE, NULL, EOAC_NONE, NULL);
    			if (FAILED(hr))    break;
    			hr = CoCreateInstance(CLSID_WbemLocator, NULL, CLSCTX_INPROC_SERVER, IID_IWbemLocator, reinterpret_cast<LPVOID*>(&pLocator));
    			if (FAILED(hr))    break;
    			hr = pLocator->ConnectServer(path, NULL, NULL, NULL, 0, NULL, NULL, &pService);
    			if (FAILED(hr))    break;
    			hr = CoSetProxyBlanket(pService, RPC_C_AUTHN_WINNT, RPC_C_AUTHZ_NONE, NULL, RPC_C_AUTHN_LEVEL_CALL, RPC_C_IMP_LEVEL_IMPERSONATE, NULL, EOAC_NONE);
    			if (FAILED(hr))    break;
    			BSTR bstrClassName = SysAllocString(L"Win32_Volume");
    			BSTR bstrMethod_defrag_analyse = SysAllocString(L"DefragAnalysis");
    			BSTR bstrMethod_defrag = SysAllocString(L"Defrag");
    			BSTR bstrParameter_force = SysAllocString(L"Force");
    			hr = pService->GetObject(bstrClassName, 0, NULL, &pClass, NULL);
    			if (FAILED(hr))    break;
    			hr = pClass->GetMethod(bstrMethod_defrag_analyse, 0, &pInParamsDefinition, NULL);
    			if (FAILED(hr))    break;
    			BSTR bstrWQL = SysAllocString(L"WQL");
    			BSTR bstrQuery = SysAllocString(L"SELECT * FROM Win32_Volume WHERE DriveLetter = 'M:'");
    			// execute QWL to query volume info
    			hr = pService->ExecQuery(bstrWQL, bstrQuery, WBEM_FLAG_FORWARD_ONLY | WBEM_FLAG_RETURN_IMMEDIATELY, NULL, &pEnumerator);
    			if (FAILED(hr))    break;
    			while (pEnumerator)
    			{
    				ULONG uReturn = 0;
    				IWbemClassObjectPtr pVolumeObj = nullptr;
    				hr = pEnumerator->Next(WBEM_INFINITE, 1, &pVolumeObj, &uReturn);
    				if (0 == uReturn)
    					break;
    				_variant_t var;
    				hr = pVolumeObj->Get(L"__PATH", 0, &var, NULL, NULL);
    				if (FAILED(hr))    break;
    				IWbemClassObjectPtr pOutParams = nullptr;
    				hr = pService->ExecMethod(var.bstrVal, bstrMethod_defrag_analyse, 0, NULL, NULL, &pOutParams, NULL);
    				if (FAILED(hr))    break;
    				_variant_t varRetValDefRec;
    				_variant_t varRetValDefAna;
    				CIMTYPE cimtype;
    				hr = pOutParams->Get(L"DefragRecommended", 0, &varRetValDefRec, NULL, 0);
    				if (FAILED(hr))    break;
    				hr = pOutParams->Get(L"DefragAnalysis", 0, &varRetValDefAna, &cimtype, 0);
    				if (FAILED(hr))    break;
    				bool defrag_recommended = varRetValDefRec.boolVal;
    				if (defrag_recommended == false)
    					hr = S_FALSE;
    			}
    			::SysFreeString(bstrClassName);
    			::SysFreeString(bstrMethod_defrag_analyse);
    			::SysFreeString(bstrMethod_defrag);
    			::SysFreeString(bstrParameter_force);
    			::SysFreeString(bstrWQL);
    			::SysFreeString(bstrQuery);
    		} while (0);
    	}
    	CoUninitialize();
    	return 0;
    }
    I have prepared a partition ('M:') for a test - and a check with powershell commands:

    Code:
    PS C:\WINDOWS\system32> $drive = Get-WmiObject win32_volume -filter "driveletter='M:'"
    PS C:\WINDOWS\system32> $report = $drive.DefragAnalysis()
    PS C:\WINDOWS\system32> $report.DefragRecommended
    True
    PS C:\WINDOWS\system32> $report.DefragAnalysis.FilePercentFragmentation
    99
    if i run the above program as administrator - i receive no abort - but:
    1.) i receive on defrag_recommended = false and
    2.) in which way can i receive FilePercentFragmentation from variant varRetValDefAna?

    Thanks in advance.

  6. #6
    Arjay's Avatar
    Arjay is offline Moderator / MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,032

    Re: WMI VARIANT read CimType = Object

    Take a look at the CimTool mentioned in the post and use it to look at the properties of the object
    Make sure the properties you are interested in are available (as not all WMI providers implement all propeties).

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width




On-Demand Webinars (sponsored)