CoCreateInstance返回E_INVALIDARG

时间:2014-09-04 14:20:35

标签: c++ com afx

我在这里尝试做的只是创建一个接口实例。真的应该是那么简单。一直在关注任何在线资料,阅读我能找到的材料,并且无法解决这个问题。

归结为从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);

(由于法律责任,实际名称未在此代码中显示)

1 个答案:

答案 0 :(得分:2)

您需要获取p_Interface的地址并将其传递给CoCreateInstance。因为它只是为最后一个参数传递一个NULL指针。