Click here to Skip to main content
15,881,424 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
the code is the following :
C++
BSTR MethodName = SysAllocString(L"DefragAnalysis");
BSTR ClassName = SysAllocString(L"Win32_Volume");

IWbemClassObject* pClass = NULL; 
hres = pSvc-> GetObject (ClassName, 0, 0, &pClass, 0); 

IEnumWbemClassObject* pEnumerator = NULL; 
hres = pSvc-> ExecQuery ( 
    bstr_t ("WQL"),  
    bstr_t ("SELECT * FROM Win32_Volume"), 
    WBEM_FLAG_FORWARD_ONLY | WBEM_FLAG_RETURN_IMMEDIATELY,  
    NULL, 
    &pEnumerator); 

IWbemClassObject *pclsObj; 
ULONG uReturn = 0; 

while (pEnumerator) 
{ 
    HRESULT hr = pEnumerator-> Next (WBEM_INFINITE, 1,  
        &pclsObj, &uReturn); 

    if (0 == uReturn) 
    { 
        break; 
    }
    VARIANT vtProp;

            if(FAILED(pclsObj->Get(L"__Path", 0, &vtProp, NULL, NULL)))  
            {  
             cout << "Object has no __Path!" << endl;  
             pclsObj->Release();  
             continue;  
             }  
            int test =0;
            //Execute Method 
            IWbemClassObject* pOutParams = NULL; 
            VARIANT varReturnValue;
            hres = pSvc-> ExecMethod (vtProp.bstrVal, MethodName, 0,     
                           //vtProp.bstrVal
            NULL, NULL, &pOutParams, NULL); 
            hres = pOutParams->Get(L"ReturnValue", 0, 
                            &***varReturnValue***, 
                            NULL, 0);
            VariantClear(&varReturnValue);

}

but i get a vaule of 11 of ReturnValue witch means Unknown error could someone help,i 've been looking for a solution for days

i dont know if the problem is in the list of Parameters of ExecMethod or anything else?
Posted
Updated 19-May-14 11:47am
v2

1 solution

VB
hres = pSvc-> ExecMethod (vtProp.bstrVal, MethodName, 0,
                           //vtProp.bstrVal
            NULL, NULL, &pOutParams, NULL);



The first argument should be a ClassName.
Not too familiar with that kind of code, but I believe that the pOutparams should point on a structure to get the return value of the Defrag call.
 
Share this answer
 
Comments
ala deli 20-May-14 7:19am    
thanks for your reply but when i put the first argument as ClassName hres gets negative value(witch means an error )
Rage 20-May-14 7:30am    
I think you will need to get _both_ problems fixed before getting a proper return value (first the classname, then the spawn of the output parameters)
ala deli 20-May-14 7:39am    
<pre lang="c++">
BSTR MethodName = SysAllocString(L"DefragAnalysis");
BSTR ClassName = SysAllocString(L"Win32_Volume");

IWbemClassObject* pClass = NULL;
hres = pSvc-> GetObject (ClassName, 0, 0, &pClass, 0);

IWbemClassObject* pOutParamsDefinition = NULL;
hres = pClass->GetMethod(MethodName, 0, NULL, &pOutParamsDefinition);

IWbemClassObject* pClassInstance = NULL;
hres = pOutParamsDefinition->SpawnInstance(0, &pClassInstance);

IEnumWbemClassObject* pEnumerator = NULL;
hres = pSvc-> ExecQuery (
bstr_t ("WQL"),
bstr_t ("SELECT * FROM Win32_Volume WHERE DriveType = 3 AND DriveLetter IS NOT NULL"),
WBEM_FLAG_FORWARD_ONLY | WBEM_FLAG_RETURN_IMMEDIATELY,
NULL,
&pEnumerator);

IWbemClassObject *pclsObj;
IWbemClassObject* pDefragAnalysis = NULL;
ULONG uReturn = 0;

while (pEnumerator)
{
HRESULT hr = pEnumerator-> Next (WBEM_INFINITE, 1,
&pclsObj, &uReturn);

if (0 == uReturn)
{
break;
}
VARIANT vtProp;

if(FAILED(pclsObj->Get(L"__Path", 0, &vtProp, NULL, NULL)))
{
cout << "Object has no __Path!" << endl;
pclsObj->Release();
continue;
}
//int test =0;
//Execute Method
//IWbemClassObject* pOutParams = NULL;
VARIANT varReturnValue;
hres = pSvc-> ExecMethod (ClassName, MethodName, 0, //vtProp.bstrVal
NULL, NULL, &pClassInstance, NULL);
hres = pClassInstance->Get(L"DefragAnalysis", 0, &varReturnValue, NULL, 0);
if (!FAILED(hres))
{
if (varReturnValue.vt == VT_UNKNOWN)
{
int test=0;
}
}
VariantClear(&varReturnValue);
hres = pClassInstance->Get(L"ReturnValue", 0, &varReturnValue, NULL, 0);
VariantClear(&varReturnValue);

}
</pre>
i made this but i still have the same eroor

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900