c++ - 从 C++ 中的 WMI(Win32_LogicalDisk 类)调用 Chkdsk -> 'Parameter is not valid'

标签 c++ windows qt com wmi

我正在尝试从 C++ 中的 WMI 运行 Chkdsk(我正在使用 Qt Framework)。 “ExecMethod”返回“参数无效”。 我已经根据 http://msdn.microsoft.com/en-us/library/cc250766.aspx 设置了参数 -> 我正在使用半同步调用。

我正在使用 MSDN 文档(Win32_LogicalDisk、WMI C++ 应用程序示例、Win32_Volume,...)

我也尝试过使用 Win32_Volume,但效果不佳。

我的部分代码:

IWbemLocator *pLoc = NULL;
HRESULT hres = CoCreateInstance(CLSID_WbemLocator, 0, CLSCTX_INPROC_SERVER, IID_IWbemLocator, (LPVOID *) &pLoc);

IWbemServices *pSvc = NULL;
hres = pLoc->ConnectServer(_bstr_t(L"ROOT\\CIMV2"), NULL, NULL, 0, NULL, 0, 0, &pSvc);

BSTR MethodName = SysAllocString(L"Chkdsk");
BSTR ClassName = SysAllocString(L"Win32_LogicalDisk");
IWbemCallResult *pCallRes = 0;
hres = pSvc->ExecMethod(ClassName, MethodName, WBEM_FLAG_FORWARD_ONLY | WBEM_FLAG_RETURN_IMMEDIATELY, NULL,  NULL, NULL, &pCallRes);

我也尝试过:

BSTR ClassName = SysAllocString(L"Win32_LogicalDisk.DeviceID='C:'"); 

但也有同样的问题。

欢迎任何帮助。

最佳答案

您没有将正确的值传递给类名参数

您必须使用类似L"Win32_LogicalDisk.DeviceID=\"C:\""

无论如何,这是一个完整的示例控制台应用程序。

#include "stdafx.h"
#define _WIN32_DCOM
#include <iostream>
using namespace std;
#include <comdef.h>
#include <Wbemidl.h>
# pragma comment(lib, "wbemuuid.lib")

//CREDENTIAL structure
//http://msdn.microsoft.com/en-us/library/windows/desktop/aa374788%28v=vs.85%29.aspx
#define CRED_MAX_USERNAME_LENGTH            513
#define CRED_MAX_CREDENTIAL_BLOB_SIZE       512
#define CREDUI_MAX_USERNAME_LENGTH CRED_MAX_USERNAME_LENGTH
#define CREDUI_MAX_PASSWORD_LENGTH (CRED_MAX_CREDENTIAL_BLOB_SIZE / 2)


#pragma argsused
int main(int argc, char* argv[])
{
    wchar_t pszName[CREDUI_MAX_USERNAME_LENGTH+1] = L"user";
    wchar_t pszPwd[CREDUI_MAX_PASSWORD_LENGTH+1]  = L"password";
    BSTR strNetworkResource;
    //To use a WMI remote connection set localconn to false and configure the values of the pszName, pszPwd and the name of the remote machine in strNetworkResource
    bool localconn = true;  
    strNetworkResource = localconn ?  L"\\\\.\\root\\CIMV2" : L"\\\\remote--machine\\root\\CIMV2";

    COAUTHIDENTITY *userAcct =  NULL ;
    COAUTHIDENTITY authIdent;
    HRESULT hres;

    // Initialize COM. ------------------------------------------

    hres =  CoInitializeEx(0, COINIT_MULTITHREADED);
    if (FAILED(hres))
    {
        cout << "Failed to initialize COM library. Error code = 0x" << hex << hres << endl;
        cout << _com_error(hres).ErrorMessage() << endl;
        cout << "press enter to exit" << endl;
        cin.get();          
        return 1;                  // Program has failed.
    }

    // Set general COM security levels --------------------------
    if (localconn)
        hres =  CoInitializeSecurity(
            NULL,
            -1,                          // COM authentication
            NULL,                        // Authentication services
            NULL,                        // Reserved
            RPC_C_AUTHN_LEVEL_DEFAULT,   // Default authentication
            RPC_C_IMP_LEVEL_IMPERSONATE, // Default Impersonation
            NULL,                        // Authentication info
            EOAC_NONE,                   // Additional capabilities
            NULL                         // Reserved
            );
    else
        hres =  CoInitializeSecurity(
            NULL,
            -1,                          // COM authentication
            NULL,                        // Authentication services
            NULL,                        // Reserved
            RPC_C_AUTHN_LEVEL_DEFAULT,   // Default authentication
            RPC_C_IMP_LEVEL_IDENTIFY,    // Default Impersonation
            NULL,                        // Authentication info
            EOAC_NONE,                   // Additional capabilities
            NULL                         // Reserved
            );


    if (FAILED(hres))
    {
        cout << "Failed to initialize security. Error code = 0x" << hex << hres << endl;
        cout << _com_error(hres).ErrorMessage() << endl;
        CoUninitialize();
        cout << "press enter to exit" << endl;
        cin.get();          
        return 1;                      // Program has failed.
    }

    // Obtain the initial locator to WMI -------------------------

    IWbemLocator *pLoc = NULL;
    hres = CoCreateInstance(CLSID_WbemLocator, 0, CLSCTX_INPROC_SERVER, IID_IWbemLocator, (LPVOID *) &pLoc);

    if (FAILED(hres))
    {
        cout << "Failed to create IWbemLocator object. " << "Err code = 0x" << hex << hres << endl;
        cout << _com_error(hres).ErrorMessage() << endl;
        CoUninitialize();
        cout << "press enter to exit" << endl;
        cin.get();              
        return 1;                 // Program has failed.
    }

    // Connect to WMI through the IWbemLocator::ConnectServer method

    IWbemServices *pSvc = NULL;

    // Connect to the root\\CIMV2 namespace
    // and obtain pointer pSvc to make IWbemServices calls.
    if (localconn)  
        hres = pLoc->ConnectServer(
             _bstr_t(strNetworkResource),      // 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
             );
    else
        hres = pLoc->ConnectServer(
            _bstr_t(strNetworkResource),  // Object path of WMI namespace
            _bstr_t(pszName),             // User name
            _bstr_t(pszPwd),              // User password
            NULL,                // Locale
            NULL,                // Security flags
            NULL,                // Authority
            NULL,                // Context object
            &pSvc                // IWbemServices proxy
            );


    if (FAILED(hres))
    {
        cout << "Could not connect. Error code = 0x" << hex << hres << endl;
        cout << _com_error(hres).ErrorMessage() << endl;
        pLoc->Release();
        CoUninitialize();
        cout << "press enter to exit" << endl;
        cin.get();                  
        return 1;                // Program has failed.
    }

    cout << "Connected to root\\CIMV2 WMI namespace" << endl;

    // Set security levels on the proxy -------------------------
    if (localconn)
        hres = CoSetProxyBlanket(
           pSvc,                        // Indicates the proxy to set
           RPC_C_AUTHN_WINNT,           // RPC_C_AUTHN_xxx
           RPC_C_AUTHZ_NONE,            // RPC_C_AUTHZ_xxx
           NULL,                        // Server principal name
           RPC_C_AUTHN_LEVEL_CALL,      // RPC_C_AUTHN_LEVEL_xxx
           RPC_C_IMP_LEVEL_IMPERSONATE, // RPC_C_IMP_LEVEL_xxx
           NULL,                        // client identity
           EOAC_NONE                    // proxy capabilities
        );
    else
    {
        // Create COAUTHIDENTITY that can be used for setting security on proxy
        memset(&authIdent, 0, sizeof(COAUTHIDENTITY));
        authIdent.PasswordLength = wcslen (pszPwd);
        authIdent.Password = (USHORT*)pszPwd;
        authIdent.User = (USHORT*)pszName;
        authIdent.UserLength = wcslen(pszName);
        authIdent.Domain = 0;
        authIdent.DomainLength = 0;
        authIdent.Flags = SEC_WINNT_AUTH_IDENTITY_UNICODE;
        userAcct = &authIdent;

        hres = CoSetProxyBlanket(
           pSvc,                           // Indicates the proxy to set
           RPC_C_AUTHN_DEFAULT,            // RPC_C_AUTHN_xxx
           RPC_C_AUTHZ_DEFAULT,            // RPC_C_AUTHZ_xxx
           COLE_DEFAULT_PRINCIPAL,         // Server principal name
           RPC_C_AUTHN_LEVEL_PKT_PRIVACY,  // RPC_C_AUTHN_LEVEL_xxx
           RPC_C_IMP_LEVEL_IMPERSONATE,    // RPC_C_IMP_LEVEL_xxx
           userAcct,                       // client identity
           EOAC_NONE                       // proxy capabilities
        );
    }


    if (FAILED(hres))
    {
        cout << "Could not set proxy blanket. Error code = 0x" << hex << hres << endl;
        cout << _com_error(hres).ErrorMessage() << endl;
        pSvc->Release();
        pLoc->Release();     
        CoUninitialize();
        cout << "press enter to exit" << endl;
        cin.get();                  
        return 1;               // Program has failed.
    }

    // Use the IWbemServices pointer to make requests of WMI ----

    BSTR MethodName = SysAllocString(L"Chkdsk");
    BSTR ClassName = SysAllocString(L"Win32_LogicalDisk");

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

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

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

    VARIANT varCommand;


    // Execute Method
    IWbemClassObject* pOutParams = NULL;
    hres = pSvc->ExecMethod(L"Win32_LogicalDisk.DeviceID=\"C:\"", MethodName, 0,
    NULL, pClassInstance, &pOutParams, NULL);

    if (FAILED(hres))
    {
        cout << "Could not execute method. Error code = 0x" << hex << hres << endl;
        cout << _com_error(hres).ErrorMessage() << endl;        
        SysFreeString(ClassName);
        SysFreeString(MethodName);
        if (pClass) pClass->Release();
        if (pInParamsDefinition) pInParamsDefinition->Release();
        if (pOutParams) pOutParams->Release();
        if (pSvc) pSvc->Release();
        if (pLoc) pLoc->Release();     
        CoUninitialize();
        cout << "press enter to exit" << endl;
        cin.get();          
        return 1;               // Program has failed.
    }


    VARIANT varReturnValue;
    hres = pOutParams->Get(L"ReturnValue", 0, &varReturnValue, NULL, 0);
    if (!FAILED(hres))
    wcout << "ReturnValue " << varReturnValue.intVal << endl;
    VariantClear(&varReturnValue);


    // Clean up    
    SysFreeString(ClassName);
    SysFreeString(MethodName);  
    if (pClass) pClass->Release();
    if (pInParamsDefinition) pInParamsDefinition->Release();
    if (pOutParams) pOutParams->Release();
    if (pLoc) pLoc->Release();
    if (pSvc) pSvc->Release();
    CoUninitialize();
    cout << "press enter to exit" << endl;
    cin.get();  
    return 0;
}

如果您是 WMI 和 C++ 的新手,您可以尝试像 WMI Delphi Code Creator 这样的工具这可以帮助您创建 C++ 代码来访问 WMI。

关于c++ - 从 C++ 中的 WMI(Win32_LogicalDisk 类)调用 Chkdsk -> 'Parameter is not valid',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25892486/

相关文章:

C++从类内部调用类外部的方法

c++ - 累积中的 Lambda 函数

python - 从 python 打开 .bat

python - 如何使 Python 的 winreg 查看注册表中在 Adob​​e CC 程序的注册表编辑器中可见的条目?

linux - 如何将 Dcmtk 与 Qt for Linux 正确链接?

c++ - 分离轴定理: rotation around center of mass

c# - 即使 c++ dll/native 插件中有 exit(1),如何保留我的 unity3d 程序 (c# v3.5)?

css - 所有 Windows 浏览器的@font-face 问题

c++ - 尝试访问命名空间 Ui 中的类

qt - 如何有效地对 QByteArray 进行分区?