2016-10-20 82 views
0

我試圖從WMI對象中檢索一個「ID」,並保留它。來自IWbemClassObject的未知異常::獲取

建立實例後,我堅持它像這樣:

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

所有罰款和花花公子 - 這使得它的方式進入WMI。然後我用我的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對此獲取。

如果我將它全部包裝在try{}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; 
    } 
+1

請提供一個[最小,完整和可驗證示例](http://stackoverflow.com/help/mcve),顯示您正在做的所有事情。 –

回答

0

我改變

解決問題
hRes = pCallRes->GetResultObject(WBEM_INFINITE, &objPtr); 

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

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

製成vnt0

似乎在實例被實際保留之前對該實例進行了一些處理,因此正在檢索的result object未「準備就緒」。當我GetResultString並基於該對象路徑重新獲取對象時,它具有可用的值。去搞清楚。