1

我试图在持久化 WMI 对象后立即从它中检索“ID”。

建立实例后,我像这样坚持它:

hRes = pSvc->PutInstance(pNewInstance, 0, pCtx, &pResult);

一切都很好——它进入了 WMI。然后我使用 my pResult,它是IWbemCallResult **ppCallResult这样调用IWbemCallResult::GetResultObject的:

IWbemClassObject *ppResultObject = NULL;
hRes = pResult->GetResultObject(WBEM_INFINITE, &ppResultObject);

伟大的。hResS_OK,我似乎有一个实例,我可以通过使用IWbemClassObject::Get...检索属性,所以我这样做:

VARIANT v;
hRes = ppResultObject->Get(L"ID", 0, &v, 0, 0);

无论实际作为“ID”存在的值如何,VARIANT 中的值都是 0。我也无法获得我在其上设置的其他属性(它们也是 0)。我已经尝试了这个代码流的无数变体,使用->Get(CComBSTR("ID")...,->Get(_bstr_t("ID")...等等......

hResS_OK也在这个Get上。

如果我将它全部包装在 a 中try{}catch(...){},它会进入 catch,但我不知道如何确定异常类型或它为什么会失败。这种逻辑或语法有问题吗?如何确定Get(L"ID"...通话出了什么问题?

这是我学习 C++ 的第三周。请帮助我不要那么讨厌它。

编辑: 最小、完整和可验证的示例(尽可能完整和可验证,无需访问我的机器和 WMI 连接):

bool 
myNamespace::myWMIWrapper::createUser(User_t * user)
{
    IWbemLocator * pIWbemLocator = NULL;
    HRESULT hRes = CoCreateInstance(CLSID_WbemLocator,NULL,CLSCTX_ALL,IID_IWbemLocator, (LPVOID *) &pIWbemLocator);

    if (SUCCEEDED(hRes))
    {   
        BSTR combinedUserName = NULL;
        if (m_username != NULL && m_domainname != NULL)
            combinedUserName =_bstr_t(m_domainname) + _bstr_t(_T("\\")) + _bstr_t(m_username);

        hRes = pIWbemLocator->ConnectServer(
                        m_namespace,      // machine name and namespace
                        combinedUserName, // Userid
                        m_password,       // PW
                        NULL,             // Locale
                        0,                // flags
                        NULL,             // Authority
                        pWCtx,            // Context
                        &pWbemServices );
        }

        if (FAILED(hRes))
        {
            LOGGER << _T("Could not connect to the WMI server. Error code = ") << hRes ;
            pIWbemLocator->Release();
            pIWbemLocator = NULL;   
            CoUninitialize();
            return false;
        }

        hRes = NULL;
        IWbemClassObject *pNewClass = NULL;

        BSTR myClassName = SysAllocString(L"My_Class");
        hRes = pWbemServices->GetObject(myClassName, 0, NULL, &pNewClass, NULL);

        if (FAILED(hRes))
        {
            LOGGER << _T("hRes failed. Couldn't find class using GetObject with class name:");
            LOGGER << className;
        }
        SysFreeString(myClassName);

        if (pNewClass == NULL) {
            LOGGER << _T(" No My_Class class Object was found using query.");
            return false;
        }

        LOGGER << _T("Class definition from WMI is found.");
        IWbemClassObject *pNUser = NULL;

        LOGGER << _T(" Creating an instance of My_Class.");
        hRes = S_OK;

        hRes = pNewClass->SpawnInstance(NULL, &pNUser);
        pNewClass->Release();

        if (FAILED(hRes)) {
            LOGGER << _T(" My_Class Object can not be instansiated.");
            return false;
        }

        LOGGER << _T(" Updating properties of My_Class found by WMI query");

        if (user->getFirstName() != NULL) {
            _variant_t v(user->getFirstName()->c_str());
            LOGGER << _T(" Updating property FIRSTNAME->") << V_BSTR(&v) ;
            pNuser->Put(L"FIRSTNAME", 0, &v, 0);
        }

        if (user->getLastName() != NULL) {
        _variant_t v(user->getLastName()->c_str());
        LOGGER <<_T(" Updating property LASTNAME->")<< V_BSTR(&v) ;
        pNuser->Put(L"LASTNAME", 0, &v, 0);
        }

        LOGGER << _T(" Persisting My_Class information");

        IWbemCallResult *pCallRes;
        hRes = pWbemServices->PutInstance(pNuser, WBEM_FLAG_CREATE_OR_UPDATE, NULL, &pCallRes);
        if (FAILED(hRes)) {
          LOGGER << _T(" Persisting of the My_Class object failed.");
          return false;
        }

        _variant_t vnt = NULL;
        IWbemClassObject *objPtr = NULL;
        LOGGER << _T(" calling ->GetResultObject()");
        hRes = pCallRes->GetResultObject(WBEM_INFINITE, &objPtr);

        if (FAILED(hRes)) {
          LOGGER << _T(" calling GetResultObject failed");
          return false;
        }

        LOGGER << _T("Calling ->Get(ID) on instance retrieved from GetResultObject.");
        try {
          // This line gets executed before entering catch(...)
          hRes = objPtr->Get(L"ID", 0, &vnt, NULL, NULL);
        }
        catch (const std::exception& ex) {
          LOGGER << _T(" problem with Get") << ex.what();
        }
        catch (...) { // I'm entering this catch block.
          LOGGER << _T(" unknown exception...............");
        }

        return true;
    }
4

1 回答 1

0

我通过改变解决了这个问题

hRes = pCallRes->GetResultObject(WBEM_INFINITE, &objPtr);

BSTR objPath = NULL;
hRes = pCallRes->GetResultString(WBEM_INFINITE, &objPath);
...
hRes = pWbemServices->GetObject(objPath, 0, NULL, &objPtr, NULL);

其中vnt制造

hRes = objPtr->Get(L"ID", 0, &vnt, NULL, NULL);

不是0

似乎在实例实际持久化之前已经对实例进行了一些处理,因此result object正在检索的实例还没有“准备好”。当我GetResultString根据该对象路径重新获取对象时,它具有可用的值。去搞清楚。

于 2016-10-21T11:07:14.747 回答