c++ - 我在使用 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;
}
}
解决方案
推荐阅读
- java - 检查 2 个不区分大小写的字符串或使用 equalsIgnoreCase 方法
- datepicker - AntD Range Picker如何只启用3个月?
- excel - 使用 VBA 代码自动填充单元格,其中包含来自不同列的条件
- .net - 实体框架问题 - 启用迁移
- windows - 在 git bash 中运行 sh 文件甚至会导致终端中显示变量赋值语句
- ios - 警告:无法为签名者“iPhone Distribution:XXXXXX”构建自签名根的链
- java - 如何使用在 AndroidManifest 上标记的关于声明的shakebugs ScreenRecordingService 防止此android 构建消息?
- android - 如何将 int 类型全局变量的值从一个活动传输到另一个活动并在 textview 中显示?
- python - 如何从 django 表单中获取日期值并将其存储在 views.py 中的变量中
- reactjs - React 最终表单中的表单验证不起作用