Search code examples
c++comafx

CoCreateInstance Returning E_INVALIDARG


What I'm attempting to do here is simply create an instance of an interface. Really is supposed to be that simple. Been following any online material, and reading material I can find and can't solve this issue for the life of me.

It comes down to the HRESULT that comes back from CoCreateInstance being E_INVALIDARG. I've changed around the parameters as much as I can to try and get it to work but still can't get it. So please take a glance and hopefully someone can point out something simple I'm over looking.

//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);

(Actual names are not shown in this code due to Legal Liabilities)


Solution

  • You need to take the address of p_Interface and pass that to CoCreateInstance. As it is you're just passing a NULL pointer for the last argument.