IWbemClassObject-> Put方法返回WBEM_E_NOT_FOUND错误

时间:2013-01-14 19:50:50

标签: c++ wmi wbem

我有一块C ++代码,我试图调用公司指定的WMI方法。在尝试构建表示参数的IWbemClassObject时,我收到WBEM_E_NOT_FOUND错误消息。根据{{​​3}},这不应该是有效的HRESULT。

/* pSvc has been setup already, all calls returned S_OK. Ugly Debugging statements have been sprinkled throughout the code. */
HRESULT hres;
IWbemClassObject* DataInClass;
IWbemClassObject* DataOutClass;
IWbemClassObject* CallingObject;
/* Grab the types of parameters we will be using for Input and Output.
DataIn and DataOut are set to the classes we will  be using */
hres = pSvc->GetObject(
    _bstr_t("ObjectName"),
    WBEM_FLAG_RETURN_WBEM_COMPLETE,
    NULL,
    &CallingObject,
    NULL
    );
cout << "GetObject:" << hres << endl;

string function_type = "MethodName";

hres = CallingObject->GetMethod(
    _bstr_t(function_type.c_str()),
    0,
    &DataInClass,
    &DataOutClass
    );
cout << "GetMethod:" << hres << endl;
if(hres == WBEM_S_NO_ERROR) cout << "No Error" << endl;
if(hres == WBEM_E_NOT_FOUND) cout << "Not found" << endl;
if(hres == WBEM_E_OUT_OF_MEMORY) cout << "OOM" << endl;
/* Create an instance of DataIn and DataOut and populate the parameters */
IWbemClassObject* DataIn;
IWbemClassObject* DataOut;
hres = DataInClass->SpawnInstance(0, &DataIn);
hres = DataOutClass->SpawnInstance(0, &DataOut);

/* NOTE: up to this point, all HRESULTS have been returning S_OK. I'm getting appropriate pointer values, so it appears that the code can connect to WMI. */

/* Set all of the parameters needed for DataIn */
VARIANT var;
// Active: true
_variant_t varActive(true);
var = varActive.Detach();
hres = DataIn->Put(_bstr_t("Active"), 0, &var, 0);
VariantClear(&var);
switch(hres){
case WBEM_E_FAILED: cout << "Failed" << endl; break;
case WBEM_E_INVALID_PARAMETER: cout << "ip" << endl; break;
case WBEM_E_INVALID_PROPERTY_TYPE: cout << "ipt" << endl; break;
case WBEM_E_OUT_OF_MEMORY: cout << "oom" << endl; break;
case WBEM_E_TYPE_MISMATCH: cout << "typem" << endl; break;
case WBEM_S_NO_ERROR: cout << "ok" << endl; break;
case WBEM_E_NOT_FOUND: cout << "WHAT!?" << endl; break; // This is the error code being returned from DataIn->Put
default: cout << "nota?" << endl; break;
}

有谁知道为什么会这样,以及我如何解决它?

非常感谢, 扎克

1 个答案:

答案 0 :(得分:1)

通常,此错误会出现“GET”命令。 在大多数情况下,错误的特定原因取决于您尝试访问的WMI类。例如,WMIDiag有关于在尝试连接性能计数器时何时返回此错误的说法:

  

当缺少WMI性能类时(即'Win32_PerfRawData_TermService_TerminalServices'),通常是由于   暴露WMI性能计数器的WMI类提供程序缺少缓冲区刷新。   
您可以使用以下命令刷新WMI类提供程序缓冲区:   
''WINMGMT.EXE / SYNCPERF'

在其他情况下,此错误似乎最常由损坏的存储库引起。有关示例,请参阅this MS KB文章。

由于您提到了“公司指定的”WMI方法,我假设您已经注册了自己的WMI提供程序。在重建存储库之前,我怀疑此提供程序未正确注册WMI。

你问这个问题差不多一年了,我想你可能已经弄明白了。如果是这样,我认为社区会很感激您在此处发布结果,以便其他用户可以从中受益。