I need help with CopyEx function used in WMI. I try to write code which
copies whole directory recursively. I found on net, that it is possible with
WMI. I tried it but I get error and I don't know where is the problem. I
found some examples on this topic on net, but only for C#, nothing for C++.
So here is my code:
HRESULT CopyDirectory(LPCTSTR SourcePath, LPCTSTR DestPath)
{
HRESULT hres;
// Obtain the initial locator to WMI
CComPtr<IWbemLocator> pLoc = NULL;
hres = CoCreateInstance(
CLSID_WbemLocator,
0,
CLSCTX_INPROC_SERVER,
IID_IWbemLocator, (LPVOID *) &pLoc);
if (FAILED(hres))
{
return hres;
}
// Connect to WMI through the IWbemLocator::ConnectServer method
CComPtr<IWbemServices> pSvc = NULL;
// Connect to the root\cimv2 namespace with
// the current user and obtain pointer pSvc
// to make IWbemServices calls.
hres = pLoc->ConnectServer(
_bstr_t(_T("\\\\.\\ROOT\\CIMV2")), // Object path of WMI namespace
NULL, // User name. NULL = current user
NULL, // User password. NULL = current
0, // Locale. NULL indicates current
NULL, // Security flags.
0, // Authority (e.g. Kerberos)
0, // Context object
&pSvc // pointer to IWbemServices proxy
);
if (FAILED(hres))
{
return hres;
}
// Use the IWbemServices pointer to make requests of WMI ----
// set up to call the Win32_Directory::CopyEx method
_bstr_t MethodName(_T("CopyEx"));
_bstr_t ClassName(_T("Win32_Directory"));
// retrieves wbem class - Win32_Directory
CComPtr<IWbemClassObject> pClass = NULL;
hres = pSvc->GetObject(ClassName, 0, NULL, &pClass, NULL);
if(FAILED(hres)) return hres;
// create instance of class Win32_Directory
CComPtr<IWbemClassObject> pClassInstance = NULL;
hres = pClass->SpawnInstance(0, &pClassInstance);
if(FAILED(hres)) return hres;
// set source path -> Name property
VARIANT varVariable;
varVariable.vt = VT_BSTR;
varVariable.bstrVal = _bstr_t(SourcePath);
// Store the value for the Name property
hres = pClassInstance->Put(_T("Name"), 0, &varVariable, 0);
VariantClear(&varVariable);
if(FAILED(hres)) return hres;
// retrieves wbem class for InputParams -> uint32 CopyEx(string
FileName, string StopFileName, string StartFileName, boolean Recursive);
CComPtr<IWbemClassObject> pInParamsDefinition = NULL;
hres = pClass->GetMethod(MethodName, 0,
&pInParamsDefinition, NULL);
if(FAILED(hres)) return hres;
// create instance of method's InputParams
CComPtr<IWbemClassObject> pInParamsClassInstance = NULL;
hres = pInParamsDefinition->SpawnInstance(0, &pInParamsClassInstance);
if(FAILED(hres)) return hres;
// Create the values for the in parameters -> FileName param
varVariable.vt = VT_BSTR;
varVariable.bstrVal = _bstr_t(DestPath);
// Store the value for the in parameters
hres = pInParamsClassInstance->Put(_T("FileName"), 0, &varVariable, 0);
VariantClear(&varVariable);
if(FAILED(hres)) return hres;
// Create the values for the in parameters -> Recursive param
varVariable.vt = VT_BOOL;
varVariable.boolVal = TRUE;
// Store the value for the in parameters
hres = pInParamsClassInstance->Put(_T("Recursive"), 0, &varVariable, 0);
VariantClear(&varVariable);
if(FAILED(hres)) return hres;
// Execute Method CopyEx
IWbemClassObject* pOutParams = NULL;
hres = pSvc->ExecMethod(ClassName, MethodName, 0, NULL,
pInParamsClassInstance, &pOutParams, NULL);
// here I get error: WBEM_E_INVALID_METHOD_PARAMETERS
if (FAILED(hres)) return hres;
// if you want to see what the method returned,
// use the following code. The return value will
// be in &varReturnValue
VARIANT varReturnValue;
hres = pOutParams->Get(_bstr_t(_T("ReturnValue")), 0, &varReturnValue,
NULL, 0);
if (FAILED(hres)) return hres;
VariantClear(&varReturnValue);
return ERROR_SUCCESS;
}
Please can someone help me? I get error as soon as I call ExecMethod
function which should execute CopyEx. I created instance of Win32_Directory
class, set Name property to source directory, then I created instance of
class for Input Parameters, where I set destination directory to Filename
property. Finally I call ExecMethod() where I get error. Its explanation in
MSDN is:
This error results from one of two causes:
The supplied pInParams object was determined by the provider to be invalid.
The strObjPath parameter specifies a class without the [static] qualifier on
the method.
But what did I wrong?
Thanks.
Michal