首页 > 解决方案 > 我在使用 ExecMethod() 时遇到问题。如何在 VC++ 中执行此操作?

问题描述

查找“//下面的代码总是失败”行,后面的行是失败的行,我认为参数不正确。这里我使用 execmethod 来执行 GetConversionStatus() 但它失败了。有人可以看看我做错了什么吗?我知道后面的行可能不对

#include "stdafx.h"
#define _WIN32_DCOM
#include <iostream>
using namespace std;
#include <comdef.h>
#include <Wbemidl.h>

#pragma comment(lib, "wbemuuid.lib")

int main()
{
    HRESULT hres;   
    hres =  CoInitializeEx(0, COINIT_MULTITHREADED); 
    if (FAILED(hres))
    {
        cout << "Failed to initialize COM library. Error code = 0x" 
            << hex << hres << endl;
        return 1;                 
    }
    hres =  CoInitializeSecurity(
        NULL, 
        -1,                          
        NULL,                       
        NULL,                        
        RPC_C_AUTHN_LEVEL_DEFAULT,   
        RPC_C_IMP_LEVEL_IMPERSONATE,   
        NULL,                        
        EOAC_NONE,                    
        NULL                        
        );


    if (FAILED(hres))
    {
        cout << "Failed to initialize security. Error code = 0x" 
            << hex << hres << endl;
        CoUninitialize();
        return 1;                    
    }


    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;
        CoUninitialize();
        return 1;                 
    }


    IWbemServices *pSvc = NULL;    
    hres = pLoc->ConnectServer(
         _bstr_t(L"Root\\CIMV2\\Security\\MicrosoftVolumeEncryption"), // Object path of WMI namespace
         NULL,                    
         NULL,                    
         0,                       
         NULL,                    
         0,                       
         0,                       
         &pSvc                    
         );

    if (FAILED(hres))
    {
        cout << "Could not connect. Error code = 0x" 
             << hex << hres << endl;
        pLoc->Release();     
        CoUninitialize();
        return 1;                
    }

    cout << "Connected to ROOT\\CIMV2 WMI namespace" << endl;
       hres = CoSetProxyBlanket(
       pSvc,                       
       RPC_C_AUTHN_WINNT,           
       RPC_C_AUTHZ_NONE,           
       NULL,                         
       RPC_C_AUTHN_LEVEL_CALL,      
       RPC_C_IMP_LEVEL_IMPERSONATE, 
       NULL,                        
       EOAC_NONE                    
    );

    if (FAILED(hres))
    {
        cout << "Could not set proxy blanket. Error code = 0x" 
            << hex << hres << endl;
        pSvc->Release();
        pLoc->Release();     
        CoUninitialize();
        return 1;              
    }


    IEnumWbemClassObject* pEnumerator = NULL;
    IWbemClassObject* pOutParams = NULL;
IWbemClassObject* pInParams = NULL;

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

IWbemClassObject* pInParamsDefinition = NULL;
hres = pClass->GetMethod(L"GetLockStatus", 0, &pInParamsDefinition, &pOutParams);
UINT32 out1;

//this below code always fails

    hres =pSvc>ExecMethod(_bstr_t(L"Win32_EncryptableVolume"),_bstr_t(L"GetLockStatus"),0,
        NULL,NULL,&pOutParams,NULL);
if (FAILED(hres))
{
cout<<"Could not execute this method"<<hres<<endl;
}
}

标签: c++

解决方案


推荐阅读