Problem with Format method of Win32_Volume Class.

EDN Admin

Well-known member
Joined
Aug 7, 2010
Messages
12,794
Location
In the Machine
I try to create console application which will be format every volumes in the system (of course, in the real application user will select the volume). For this task I try to use Format method of Win32_Volume Class, but pSvc->ExecMethod return 0x80041008 (Invalid parameter was specified.) for every volume. Next you can find source code of my simple console application which demonstrates this problem:<font color="#010001" size=2>

<font size=2>
</font><font color="#010001" size=2>
HRESULT</font> <font color="#010001" size=2>hres</font><font size=2>;
</font><font color="#008000" size=2>// Step 1: -------------------------------------------------- </font><font size=2>
</font><font color="#008000" size=2>// Initialize COM. ------------------------------------------ </font><font size=2>
</font><font color="#010001" size=2>hres</font><font size=2> = </font><font color="#010001" size=2>CoInitializeEx</font><font size=2>(0, </font><font color="#010001" size=2>COINIT_MULTITHREADED</font><font size=2>);
</font><font color="#0000ff" size=2>if</font><font size=2> (</font><font color="#010001" size=2>FAILED</font><font size=2>(</font><font color="#010001" size=2>hres</font><font size=2>))
{
</font><font color="#010001" size=2>    cout</font><font size=2> << </font><font color="#a31515" size=2>"Failed to initialize COM library. Error code = 0x"</font><font size=2>  << </font><font color="#010001" size=2>hex</font><font size=2> << </font><font color="#010001" size=2>hres</font><font size=2> << </font><font color="#010001" size=2>endl</font><font size=2>;
</font><font color="#0000ff" size=2>    return</font><font size=2> 1; </font><font color="#008000" size=2>// Program has failed. </font><font size=2>
}
</font><font color="#008000" size=2>// Step 2: -------------------------------------------------- </font><font size=2>
</font><font color="#008000" size=2>// Set general COM security levels -------------------------- </font><font size=2>
</font><font color="#008000" size=2>// Note: If you are using Windows 2000, you must specify - </font><font size=2>
</font><font color="#008000" size=2>// the default authentication credentials for a user by using </font><font size=2>
</font><font color="#008000" size=2>// a SOLE_AUTHENTICATION_LIST structure in the pAuthList ---- </font><font size=2>
</font><font color="#008000" size=2>// parameter of CoInitializeSecurity ------------------------ </font><font size=2>
</font><font color="#010001" size=2>hres</font><font size=2> = </font><font color="#010001" size=2>CoInitializeSecurity</font><font size=2>(
</font><font color="#010001" size=2>NULL</font><font size=2>,
-1, </font><font color="#008000" size=2>// COM negotiates service </font><font size=2>
</font><font color="#010001" size=2>NULL</font><font size=2>, </font><font color="#008000" size=2>// Authentication services </font><font size=2>
</font><font color="#010001" size=2>NULL</font><font size=2>, </font><font color="#008000" size=2>// Reserved </font><font size=2>
</font><font color="#010001" size=2>RPC_C_AUTHN_LEVEL_DEFAULT</font><font size=2>, </font><font color="#008000" size=2>// Default authentication </font><font size=2>
</font><font color="#010001" size=2>RPC_C_IMP_LEVEL_IMPERSONATE</font><font size=2>, </font><font color="#008000" size=2>// Default Impersonation </font><font size=2>
</font><font color="#010001" size=2>NULL</font><font size=2>, </font><font color="#008000" size=2>// Authentication info </font><font size=2>
</font><font color="#010001" size=2>EOAC_NONE</font><font size=2>, </font><font color="#008000" size=2>// Additional capabilities </font><font size=2>
</font><font color="#010001" size=2>NULL</font> <font color="#008000" size=2>// Reserved </font><font size=2>
);

</font><font color="#0000ff" size=2>if</font><font size=2> (</font><font color="#010001" size=2>FAILED</font><font size=2>(</font><font color="#010001" size=2>hres</font><font size=2>))
{
</font><font color="#010001" size=2>    cout</font><font size=2> << </font><font color="#a31515" size=2>"Failed to initialize security. Error code = 0x"</font><font size=2> << </font><font color="#010001" size=2>hex</font><font size=2> << </font><font color="#010001" size=2>hres</font><font size=2> << </font><font color="#010001" size=2>endl</font><font size=2>;
</font><font color="#010001" size=2>    CoUninitialize</font><font size=2>();
</font><font color="#0000ff" size=2>    return</font><font size=2> 1; </font><font color="#008000" size=2>// Program has failed. </font><font size=2>
}
</font><font color="#008000" size=2>// Step 3: --------------------------------------------------- </font><font size=2>
</font><font color="#008000" size=2>// Obtain the initial locator to WMI ------------------------- </font><font size=2>
</font><font color="#010001" size=2>IWbemLocator</font><font size=2> *</font><font color="#010001" size=2>pLoc</font><font size=2> = </font><font color="#010001" size=2>NULL</font><font size=2>;
</font><font color="#010001" size=2>hres</font><font size=2> = </font><font color="#010001" size=2>CoCreateInstance</font><font size=2>(
</font><font color="#010001" size=2>CLSID_WbemLocator</font><font size=2>,
0,
</font><font color="#010001" size=2>CLSCTX_INPROC_SERVER</font><font size=2>,
</font><font color="#010001" size=2>IID_IWbemLocator</font><font size=2>, (</font><font color="#010001" size=2>LPVOID</font><font size=2> *) &</font><font color="#010001" size=2>pLoc</font><font size=2>);
</font><font color="#0000ff" size=2>if</font><font size=2> (</font><font color="#010001" size=2>FAILED</font><font size=2>(</font><font color="#010001" size=2>hres</font><font size=2>))
{
</font><font color="#010001" size=2>    cout</font><font size=2> << </font><font color="#a31515" size=2>"Failed to create IWbemLocator object. " </font><font size=2><< </font><font color="#a31515" size=2>"Err code = 0x" </font><font size=2><< </font><font color="#010001" size=2>hex</font><font size=2> << </font><font color="#010001" size=2>hres</font><font size=2> << </font><font color="#010001" size=2>endl</font><font size=2>;
</font><font color="#010001" size=2>    CoUninitialize</font><font size=2>();
</font><font color="#0000ff" size=2>    return</font><font size=2> 1; </font><font color="#008000" size=2>// Program has failed. </font><font size=2>
}
</font><font color="#008000" size=2>// Step 4: --------------------------------------------------- </font><font size=2>
</font><font color="#008000" size=2>// Connect to WMI through the IWbemLocator::ConnectServer method </font><font size=2>
</font><font color="#010001" size=2>IWbemServices</font><font size=2> *</font><font color="#010001" size=2>pSvc</font><font size=2> = </font><font color="#010001" size=2>NULL</font><font size=2>;
</font><font color="#008000" size=2>// Connect to the local rootcimv2 namespace </font><font size=2>
</font><font color="#008000" size=2>// and obtain pointer pSvc to make IWbemServices calls. </font><font size=2>
</font><font color="#010001" size=2>hres</font><font size=2> = </font><font color="#010001" size=2>pLoc</font><font size=2>-></font><font color="#010001" size=2>ConnectServer</font><font size=2>(
</font><font color="#010001" size=2>_bstr_t</font><font size=2>(</font><font color="#010001" size=2>L</font><font color="#a31515" size=2>"ROOT\CIMV2"</font><font size=2>),
</font><font color="#010001" size=2>NULL</font><font size=2>,
</font><font color="#010001" size=2>NULL</font><font size=2>,
0,
</font><font color="#010001" size=2>NULL</font><font size=2>,
0,
0,
&</font><font color="#010001" size=2>pSvc </font><font size=2>
);
</font><font color="#0000ff" size=2>if</font><font size=2> (</font><font color="#010001" size=2>FAILED</font><font size=2>(</font><font color="#010001" size=2>hres</font><font size=2>))
{
</font><font color="#010001" size=2>    cout</font><font size=2> << </font><font color="#a31515" size=2>"Could not connect. Error code = 0x"</font><font size=2>  << </font><font color="#010001" size=2>hex</font><font size=2> << </font><font color="#010001" size=2>hres</font><font size=2> << </font><font color="#010001" size=2>endl</font><font size=2>;
</font><font color="#010001" size=2>    pLoc</font><font size=2>-></font><font color="#010001" size=2>Release</font><font size=2>();
</font><font color="#010001" size=2>    CoUninitialize</font><font size=2>();
</font><font color="#0000ff" size=2>    return</font><font size=2> 1; </font><font color="#008000" size=2>// Program has failed. </font><font size=2>
}
</font><font color="#010001" size=2>cout</font><font size=2> << </font><font color="#a31515" size=2>"Connected to ROOT\CIMV2 WMI namespace"</font><font size=2> << </font><font color="#010001" size=2>endl</font><font size=2>;

</font><font color="#008000" size=2>// Step 5: -------------------------------------------------- </font><font size=2>
</font><font color="#008000" size=2>// Set security levels for the proxy ------------------------ </font><font size=2>
</font><font color="#010001" size=2>hres</font><font size=2> = </font><font color="#010001" size=2>CoSetProxyBlanket</font><font size=2>(
</font><font color="#010001" size=2>pSvc</font><font size=2>, </font><font color="#008000" size=2>// Indicates the proxy to set </font><font size=2>
</font><font color="#010001" size=2>RPC_C_AUTHN_WINNT</font><font size=2>, </font><font color="#008000" size=2>// RPC_C_AUTHN_xxx </font><font size=2>
</font><font color="#010001" size=2>RPC_C_AUTHZ_NONE</font><font size=2>, </font><font color="#008000" size=2>// RPC_C_AUTHZ_xxx </font><font size=2>
</font><font color="#010001" size=2>NULL</font><font size=2>, </font><font color="#008000" size=2>// Server principal name </font><font size=2>
</font><font color="#010001" size=2>RPC_C_AUTHN_LEVEL_CALL</font><font size=2>, </font><font color="#008000" size=2>// RPC_C_AUTHN_LEVEL_xxx </font><font size=2>
</font><font color="#010001" size=2>RPC_C_IMP_LEVEL_IMPERSONATE</font><font size=2>, </font><font color="#008000" size=2>// RPC_C_IMP_LEVEL_xxx </font><font size=2>
</font><font color="#010001" size=2>NULL</font><font size=2>, </font><font color="#008000" size=2>// client identity </font><font size=2>
</font><font color="#010001" size=2>EOAC_NONE</font> <font color="#008000" size=2>// proxy capabilities </font><font size=2>
);
</font><font color="#0000ff" size=2>if</font><font size=2> (</font><font color="#010001" size=2>FAILED</font><font size=2>(</font><font color="#010001" size=2>hres</font><font size=2>))
{
</font><font color="#010001" size=2>    cout</font><font size=2> << </font><font color="#a31515" size=2>"Could not set proxy blanket. Error code = 0x"</font><font size=2> << </font><font color="#010001" size=2>hex</font><font size=2> << </font><font color="#010001" size=2>hres</font><font size=2> << </font><font color="#010001" size=2>endl</font><font size=2>;
</font><font color="#010001" size=2>    pSvc</font><font size=2>-></font><font color="#010001" size=2>Release</font><font size=2>();
</font><font color="#010001" size=2>    pLoc</font><font size=2>-></font><font color="#010001" size=2>Release</font><font size=2>();
</font><font color="#010001" size=2>    CoUninitialize</font><font size=2>();
</font><font color="#0000ff" size=2>    return</font><font size=2> 1; </font><font color="#008000" size=2>// Program has failed. </font><font size=2>
}
</font><font color="#008000" size=2>// Step 6: -------------------------------------------------- </font><font size=2>
</font><font color="#008000" size=2>// Use the IWbemServices pointer to make requests of WMI ---- </font><font size=2>
</font><font color="#010001" size=2>BSTR</font> <font color="#010001" size=2>MethodName</font><font size=2> = </font><font color="#010001" size=2>SysAllocString</font><font size=2>(</font><font color="#010001" size=2>L</font><font color="#a31515" size=2>"Format"</font><font size=2>);
</font><font color="#010001" size=2>BSTR</font> <font color="#010001" size=2>ClassName</font><font size=2> = </font><font color="#010001" size=2>SysAllocString</font><font size=2>(</font><font color="#010001" size=2>L</font><font color="#a31515" size=2>"Win32_Volume"</font><font size=2>);
</font><font color="#010001" size=2>IWbemClassObject</font><font size=2>* </font><font color="#010001" size=2>pClass</font><font size=2> = </font><font color="#010001" size=2>NULL</font><font size=2>;
</font><font color="#010001" size=2>hres</font><font size=2> = </font><font color="#010001" size=2>pSvc</font><font size=2>-></font><font color="#010001" size=2>GetObject</font><font size=2>(</font><font color="#010001" size=2>ClassName</font><font size=2>, 0, </font><font color="#010001" size=2>NULL</font><font size=2>, &</font><font color="#010001" size=2>pClass</font><font size=2>, </font><font color="#010001" size=2>NULL</font><font size=2>);
</font><font color="#010001" size=2>IEnumWbemClassObject</font><font size=2>* </font><font color="#010001" size=2>pEnumerator</font><font size=2> = </font><font color="#010001" size=2>NULL</font><font size=2>;
</font><font color="#010001" size=2>hres</font><font size=2> = </font><font color="#010001" size=2>pSvc</font><font size=2>-></font><font color="#010001" size=2>ExecQuery</font><font size=2>(
</font><font color="#010001" size=2>bstr_t</font><font size=2>(</font><font color="#a31515" size=2>"WQL"</font><font size=2>),
</font><font color="#010001" size=2>bstr_t</font><font size=2>(</font><font color="#a31515" size=2>"SELECT * FROM Win32_Volume"</font><font size=2>),
</font><font color="#010001" size=2>WBEM_FLAG_FORWARD_ONLY</font><font size=2> | </font><font color="#010001" size=2>WBEM_FLAG_RETURN_IMMEDIATELY</font><font size=2>,
</font><font color="#010001" size=2>NULL</font><font size=2>,
&</font><font color="#010001" size=2>pEnumerator</font><font size=2>);
</font><font color="#010001" size=2>IWbemClassObject</font><font size=2> *</font><font color="#010001" size=2>pclsObj</font><font size=2>;
</font><font color="#010001" size=2>ULONG</font> <font color="#010001" size=2>uReturn</font><font size=2> = 0;
</font><font color="#0000ff" size=2>while</font><font size=2> (</font><font color="#010001" size=2>pEnumerator</font><font size=2>)
{
</font><font color="#010001" size=2>    HRESULT</font> <font color="#010001" size=2>hr</font><font size=2> = </font><font color="#010001" size=2>pEnumerator</font><font size=2>-></font><font color="#010001" size=2>Next</font><font size=2>(</font><font color="#010001" size=2>WBEM_INFINITE</font><font size=2>, 1, &</font><font color="#010001" size=2>pclsObj</font><font size=2>, &</font><font color="#010001" size=2>uReturn</font><font size=2>);
</font><font color="#0000ff" size=2>    if</font><font size=2>(0 == </font><font color="#010001" size=2>uReturn</font><font size=2>)
    {
</font><font color="#0000ff" size=2>        break</font><font size=2>;
    }
</font><font color="#010001" size=2>    VARIANT</font> <font color="#010001" size=2>vtProp</font><font size=2>;
</font><font color="#008000" size=2>    // Get the value of the Name property </font><font size=2>
</font><font color="#010001" size=2>    hr</font><font size=2> = </font><font color="#010001" size=2>pclsObj</font><font size=2>-></font><font color="#010001" size=2>Get</font><font size=2>(</font><font color="#010001" size=2>L</font><font color="#a31515" size=2>"DeviceID"</font><font size=2>, 0, &</font><font color="#010001" size=2>vtProp</font><font size=2>, 0, 0);
</font><font color="#010001" size=2>    wcout</font><font size=2> << </font><font color="#a31515" size=2>" DeviceID : "</font><font size=2> << </font><font color="#010001" size=2>vtProp</font><font size=2>.</font><font color="#010001" size=2>bstrVal</font><font size=2> << </font><font color="#010001" size=2>endl</font><font size=2>;
</font><font color="#010001" size=2>    IWbemClassObject</font><font size=2>* </font><font color="#010001" size=2>pClass</font><font size=2> = </font><font color="#010001" size=2>NULL</font><font size=2>;
</font><font color="#010001" size=2>    hres</font><font size=2> = </font><font color="#010001" size=2>pSvc</font><font size=2>-></font><font color="#010001" size=2>GetObject</font><font size=2>(</font><font color="#010001" size=2>ClassName</font><font size=2>, 0, </font><font color="#010001" size=2>NULL</font><font size=2>, &</font><font color="#010001" size=2>pClass</font><font size=2>, </font><font color="#010001" size=2>NULL</font><font size=2>);
</font><font color="#010001" size=2>    IWbemClassObject</font><font size=2>* </font><font color="#010001" size=2>pInParamsDefinition</font><font size=2> = </font><font color="#010001" size=2>NULL</font><font size=2>;
</font><font color="#010001" size=2>    hres</font><font size=2> = </font><font color="#010001" size=2>pClass</font><font size=2>-></font><font color="#010001" size=2>GetMethod</font><font size=2>(</font><font color="#010001" size=2>MethodName</font><font size=2>, 0, &</font><font color="#010001" size=2>pInParamsDefinition</font><font size=2>, </font><font color="#010001" size=2>NULL</font><font size=2>);</font><font color="#008000" size=2> </font><font size=2>
</font><font color="#010001" size=2>    IWbemClassObject</font><font size=2>* </font><font color="#010001" size=2>pClassInstance</font><font size=2> = </font><font color="#010001" size=2>NULL</font><font size=2>;
</font><font color="#010001" size=2>    hres</font><font size=2> = </font><font color="#010001" size=2>pInParamsDefinition</font><font size=2>-></font><font color="#010001" size=2>SpawnInstance</font><font size=2>(0, &</font><font color="#010001" size=2>pClassInstance</font><font size=2>);
</font><font color="#008000" size=2>    // Create the values for the in parameters </font><font size=2>
</font><font color="#010001" size=2>    VARIANT</font> <font color="#010001" size=2>varFileSystem</font><font size=2>;
</font><font color="#010001" size=2>    VariantInit</font><font size=2>(&</font><font color="#010001" size=2>varFileSystem</font><font size=2>);
</font><font color="#010001" size=2>    varFileSystem</font><font size=2>.</font><font color="#010001" size=2>vt</font><font size=2> = </font><font color="#010001" size=2>VT_BSTR</font><font size=2>;
</font><font color="#010001" size=2>    varFileSystem</font><font size=2>.</font><font color="#010001" size=2>bstrVal</font><font size=2> = </font><font color="#010001" size=2>L</font><font color="#a31515" size=2>"NTFS"</font><font size=2>;
</font><font color="#010001" size=2>    VARIANT</font> <font color="#010001" size=2>varQuickFormat</font><font size=2>;
</font><font color="#010001" size=2>    VariantInit</font><font size=2>(&</font><font color="#010001" size=2>varQuickFormat</font><font size=2>);
</font><font color="#010001" size=2>    varQuickFormat</font><font size=2>.</font><font color="#010001" size=2>vt</font><font size=2> = </font><font color="#010001" size=2>VT_BOOL</font><font size=2>;
</font><font color="#010001" size=2>    varQuickFormat</font><font size=2>.</font><font color="#010001" size=2>boolVal</font><font size=2> = </font><font color="#010001" size=2>VARIANT_TRUE</font><font size=2>;</font><font color="#008000" size=2> </font><font size=2>
</font><font color="#010001" size=2>    VARIANT</font> <font color="#010001" size=2>varLabel</font><font size=2>;
</font><font color="#010001" size=2>    VariantInit</font><font size=2>(&</font><font color="#010001" size=2>varLabel</font><font size=2>);
</font><font color="#010001" size=2>    varLabel</font><font size=2>.</font><font color="#010001" size=2>vt</font><font size=2> = </font><font color="#010001" size=2>VT_BSTR</font><font size=2>;
</font><font color="#010001" size=2>    varLabel</font><font size=2>.</font><font color="#010001" size=2>bstrVal</font><font size=2> = </font><font color="#010001" size=2>L</font><font color="#a31515" size=2>""</font><font size=2>;
</font><font color="#010001" size=2>    VARIANT</font> <font color="#010001" size=2>varEnableCompression</font><font size=2>;
</font><font color="#010001" size=2>    VariantInit</font><font size=2>(&</font><font color="#010001" size=2>varEnableCompression</font><font size=2>);
</font><font color="#010001" size=2>    varEnableCompression</font><font size=2>.</font><font color="#010001" size=2>vt</font><font size=2> = </font><font color="#010001" size=2>VT_BOOL</font><font size=2>;
</font><font color="#010001" size=2>    varEnableCompression</font><font size=2>.</font><font color="#010001" size=2>boolVal</font><font size=2> = </font><font color="#010001" size=2>VARIANT_FALSE</font><font size=2>;
</font><font color="#008000" size=2>    // Store the value for the in parameters </font><font size=2>
</font><font color="#010001" size=2>    hres</font><font size=2> = </font><font color="#010001" size=2>pClassInstance</font><font size=2>-></font><font color="#010001" size=2>Put</font><font size=2>(</font><font color="#010001" size=2>L</font><font color="#a31515" size=2>"FileSystem"</font><font size=2>, 0, &</font><font color="#010001" size=2>varFileSystem</font><font size=2>, 0);
</font><font color="#010001" size=2>    hres</font><font size=2> = </font><font color="#010001" size=2>pClassInstance</font><font size=2>-></font><font color="#010001" size=2>Put</font><font size=2>(</font><font color="#010001" size=2>L</font><font color="#a31515" size=2>"QuickFormat"</font><font size=2>, 0, &</font><font color="#010001" size=2>varQuickFormat</font><font size=2>, 0);</font><font color="#008000" size=2> </font><font size=2>
</font><font color="#010001" size=2>    hres</font><font size=2> = </font><font color="#010001" size=2>pClassInstance</font><font size=2>-></font><font color="#010001" size=2>Put</font><font size=2>(</font><font color="#010001" size=2>L</font><font color="#a31515" size=2>"Label"</font><font size=2>, 0, &</font><font color="#010001" size=2>varLabel</font><font size=2>, 0);
</font><font color="#010001" size=2>    hres</font><font size=2> = </font><font color="#010001" size=2>pClassInstance</font><font size=2>-></font><font color="#010001" size=2>Put</font><font size=2>(</font><font color="#010001" size=2>L</font><font color="#a31515" size=2>"EnableCompression"</font><font size=2>, 0, &</font><font color="#010001" size=2>varEnableCompression</font><font size=2>, 0);
</font><font color="#008000" size=2>    // Execute Method </font><font size=2>
</font><font color="#010001" size=2>    IWbemClassObject</font><font size=2>* </font><font color="#010001" size=2>pOutParams</font><font size=2> = </font><font color="#010001" size=2>NULL</font><font size=2>; </font><font size=2>
</font><font color="#010001" size=2>    <font color="#008000 // Next method return 0x80041008 (Invalid parameter was specified.) for every volume</font>
    hres</font><font size=2> = </font><font color="#010001" size=2>pSvc</font><font size=2>-></font><font color="#010001" size=2>ExecMethod</font><font size=2>(</font><font color="#010001" size=2>vtProp</font><font size=2>.</font><font color="#010001" size=2>bstrVal</font><font size=2>, </font><font color="#010001" size=2>MethodName</font><font size=2>, 0, </font><font color="#010001" size=2>NULL</font><font size=2>, </font><font color="#010001" size=2>pClassInstance</font><font size=2>, &</font><font color="#010001" size=2>pOutParams</font><font size=2>, </font><font color="#010001" size=2>NULL</font><font size=2>);
}</font>
</font>

View the full article
 
Back
Top