I am trying to create a shadow copy of the C drive using wmivss. When I use the command line with wmic shadowcopy call create Volume='c:'
, it successfully creates the shadow copy and returns the following value:
Executing (Win32_ShadowCopy)->create()
Method execution successful.
Out Parameters:
instance of __PARAMETERS
{
ReturnValue = 0;
ShadowID = "{85CF0935-84D8-46A4-98DB-43753CA3AD46}";
};
However, when I try to achieve this using C++ code, I encounter an error during the execution of ExecMethod
which states ExecMethod failed: WBEM_E_INITIALIZATION_FAILURE(80041014)
. I am not sure what went wrong in my implementation. I have tried starting my Volume Shadow Copy
service, but it didn’t help. Here is my code.
#include <iostream>
#include <comdef.h>
#include <Wbemidl.h>
#include <windows.h>
#pragma comment(lib, "wbemuuid.lib")
void Cleanup(IWbemServices* pSvc, IWbemLocator* pLoc) {
if (pSvc) {
pSvc->Release();
}
if (pLoc) {
pLoc->Release();
}
CoUninitialize();
}
HRESULT CreateShadowCopy(IWbemServices* pSvc, BSTR* pShadowID) {
HRESULT hres;
IWbemClassObject* pClass = nullptr;
IWbemClassObject* pInParamsDefinition = nullptr;
IWbemClassObject* pOutParams = nullptr;
IWbemClassObject* pClassInstance = nullptr;
// Get the class object for Win32_ShadowCopy
hres = pSvc->GetObject(L"Win32_ShadowCopy", 0, nullptr, &pClass, nullptr);
if (FAILED(hres)) {
std::wcerr << L"GetObject failed: " << std::hex << hres << std::endl;
return hres;
}
// Get the method's input parameters
hres = pClass->GetMethod(L"Create", 0, &pInParamsDefinition, nullptr);
if (FAILED(hres)) {
std::wcerr << L"GetMethod failed: " << std::hex << hres << std::endl;
pClass->Release();
return hres;
}
// Create an instance of the input parameters class
hres = pInParamsDefinition->SpawnInstance(0, &pClassInstance);
if (FAILED(hres)) {
std::wcerr << L"SpawnInstance failed: " << std::hex << hres << std::endl;
pInParamsDefinition->Release();
pClass->Release();
return hres;
}
// Set the input parameters
VARIANT vtVolume;
VariantInit(&vtVolume);
vtVolume.vt = VT_BSTR;
vtVolume.bstrVal = SysAllocString(L"C:\");
hres = pClassInstance->Put(L"Volume", 0, &vtVolume, 0);
VariantClear(&vtVolume);
if (FAILED(hres)) {
std::wcerr << L"Put failed: " << std::hex << hres << std::endl;
pClassInstance->Release();
pInParamsDefinition->Release();
pClass->Release();
return hres;
}
// Execute the method
hres = pSvc->ExecMethod(L"Win32_ShadowCopy", L"Create", 0, nullptr, pClassInstance, &pOutParams, nullptr);
if (FAILED(hres)) {
std::wcerr << L"ExecMethod failed: " << std::hex << hres << std::endl;
pClassInstance->Release();
pInParamsDefinition->Release();
pClass->Release();
return hres;
}
// Get the output parameters
VARIANT vtShadowID;
hres = pOutParams->Get(L"ShadowID", 0, &vtShadowID, nullptr, nullptr);
if (FAILED(hres)) {
std::wcerr << L"Get ShadowID failed: " << std::hex << hres << std::endl;
pOutParams->Release();
pClassInstance->Release();
pInParamsDefinition->Release();
pClass->Release();
return hres;
}
*pShadowID = SysAllocString(vtShadowID.bstrVal);
VariantClear(&vtShadowID);
pOutParams->Release();
pClassInstance->Release();
pInParamsDefinition->Release();
pClass->Release();
return S_OK;
}
int wmain() {
HRESULT hres;
IWbemLocator* pLoc = nullptr;
IWbemServices* pSvc = nullptr;
BSTR shadowID = nullptr;
// Step 1: Initialize COM.
hres = CoInitializeEx(0, COINIT_MULTITHREADED);
if (FAILED(hres)) {
std::wcerr << L"CoInitializeEx failed: " << std::hex << hres << std::endl;
return 1;
}
// Step 2: Set general COM security levels.
hres = CoInitializeSecurity(
nullptr,
-1, // COM authentication
nullptr, // Authentication services
nullptr, // Reserved
RPC_C_AUTHN_LEVEL_DEFAULT, // Default authentication
RPC_C_IMP_LEVEL_IMPERSONATE,// Default Impersonation
nullptr, // Authentication info
EOAC_NONE, // Additional capabilities
nullptr // Reserved
);
if (FAILED(hres)) {
std::wcerr << L"CoInitializeSecurity failed: " << std::hex << hres << std::endl;
CoUninitialize();
return 1;
}
// Step 3: Obtain the initial locator to WMI.
hres = CoCreateInstance(
CLSID_WbemLocator,
0,
CLSCTX_INPROC_SERVER,
IID_IWbemLocator,
(LPVOID*)&pLoc);
if (FAILED(hres)) {
std::wcerr << L"CoCreateInstance failed: " << std::hex << hres << std::endl;
CoUninitialize();
return 1;
}
// Step 4: Connect to WMI through the IWbemLocator::ConnectServer method.
hres = pLoc->ConnectServer(
_bstr_t(L"ROOT\CIMV2"),
nullptr,
nullptr,
0,
NULL,
0,
0,
&pSvc);
if (FAILED(hres)) {
std::wcerr << L"ConnectServer failed: " << std::hex << hres << std::endl;
pLoc->Release();
CoUninitialize();
return 1;
}
// Step 5: Set security levels on the proxy.
hres = CoSetProxyBlanket(
pSvc,
RPC_C_AUTHN_WINNT,
RPC_C_AUTHZ_NONE,
nullptr,
RPC_C_AUTHN_LEVEL_CALL,
RPC_C_IMP_LEVEL_IMPERSONATE,
nullptr,
EOAC_NONE);
if (FAILED(hres)) {
std::wcerr << L"CoSetProxyBlanket failed: " << std::hex << hres << std::endl;
Cleanup(pSvc, pLoc);
return 1;
}
// Step 6: Create a shadow copy.
hres = CreateShadowCopy(pSvc, &shadowID);
if (FAILED(hres)) {
std::wcerr << L"CreateShadowCopy failed: " << std::hex << hres << std::endl;
Cleanup(pSvc, pLoc);
return 1;
}
// Step 7: Print the shadow copy ID.
std::wcout << L"Shadow Copy ID: " << shadowID << std::endl;
// Step 8: Cleanup.
Cleanup(pSvc, pLoc);
return 0;
}
I also tried using CreateProcess to create a process to execute wmic shadowcopy call create Volume='c:'
. It produced the same error:
Executing (Win32_ShadowCopy)->create()
ERROR:
Description = Initialization failure
I have no idea why this is happening.
K.Aguilar is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.