2014-09-04 93 views
1

我在這裏試圖做的只是創建一個接口的實例。真的應該是那麼簡單。以下任何在線資料,閱讀材料,我可以找到,並不能解決這個問題,爲我的生活。CoCreateInstance返回E_INVALIDARG

它歸結爲CoCreateInstance返回的HRESULT爲E_INVALIDARG。我儘可能多地改變參數,試着讓它工作,但仍然無法獲得它。所以請看一眼,希望有人能指出我看過的簡單的東西。

//Instantiate the sink class and hold a pointer to it. 
    m_pSink = new CSink(); 

    HRESULT hr = CoInitialize(NULL); 

    //Get a pointer to sinks IUnknown, no AddRef. CMySink implements only 
    //dispinterface and the IUnknown and IDispatch pointers will be same. 
    LPUNKNOWN pUnkSink = m_pSink->GetIDispatch(FALSE); 

    CLSID clsidInterface; 
    hr = CLSIDFromProgID(L"Automation.AutomationInterface", &clsidInterface); 

    ICALib::IAutomationInterface *p_Interface = NULL; 
    hr = CoCreateInstance(clsidInterface, NULL, CLSCTX_LOCAL_SERVER, ICALib::IID_IAutomationInterface, (void**)p_Interface); 

    if (hr != S_OK) // Show a message box if the Instance of the interface is not created and do not create the object. 
    { 
     CMessageBox(CMessageBox::WARNING_OK).Show(IDS_WARNING_BADLICENSE); 
     m_failedToCreate = TRUE; 
     this->~CMainClass(); 
     return; 
    } 

    //Establish a connection between source and sink. 
    //m_pUnkSrc is IUnknown of server obtained by CoCreateInstance(). 
    //m_dwCookie is a cookie identifying the connection, and is needed to terminate the connection. 
    BOOL result = AfxConnectionAdvise(p_Interface, m_pSink->GetGUID(), pUnkSink, FALSE, &m_dwCookie); 

(實際名稱未在此代碼所示,由於法律責任)

+0

OT,但'this->〜CMainClass();'是相當可疑的 – 2014-09-05 02:29:50

回答

2

你需要採取的p_Interface地址並將其傳遞到CoCreateInstance。因爲它只是爲最後一個參數傳遞一個NULL指針。

+0

神聖的廢話我不敢相信我一直想念,非常感謝:D – 2014-09-04 15:20:46

+0

爲了避免這種錯誤考慮使用[IID_PPV_ARGS](http://msdn.microsoft.com/en-us/library/windows/desktop/ee330727(v = vs.85).aspx)macro – 2014-09-05 02:28:57