Making an async COM call — do I have to create my

2019-09-20 21:22发布

问题:

I have a custom interface defined in a type library and implemented in my code. I have not created my own proxy/stub. I have successfully marshaled the interface pointer from my main thread (STA) to a background thread (STA), in my case using IGlobalInterfaceTable.

From that background thread, I want to make an asynchronous call to the object on the UI thread. I have not implemented ICallFactory in my object. I see that the standard proxy does indeed implement ICallFactory (i.e, I can successfully QI on the background thread for ICallFactory). But the CreateCall for my custom interface fails with HRESULT 0x80040150 (Could not read key from registry).

Do I need to create my own proxy that explicitly implements ICallFactory in order to do this?

Here's my IDL:

[
    object,
    uuid(92303FE7-A79D-47DD-923F-62062105C00E),
    async_uuid(2880C40C-9965-4544-AE39-DF08056E8CB6),
    nonextensible,
    pointer_default(unique),
    oleautomation
]
interface IFoo: IUnknown
{
    HRESULT Foo([in] long a, [in] long b);
}

[
    uuid(D58B0A31-A2D5-4BFB-8702-3B710320493B)
]
coclass Foo
{
    [default] interface IFoo;
};

And here's the background thread proc in my unit test:

    static DWORD WINAPI threadproc(LPVOID lpParameter)
    {
        // get arguments
        DWORD cookie = *(DWORD*)lpParameter;

        // initialize COM
        Assert::AreEqual(S_OK, CoInitializeEx(NULL, COINIT_APARTMENTTHREADED));
        {
            // get global interface table
            IGlobalInterfaceTablePtr globalInterfaceTable;
            Assert::AreEqual(S_OK, globalInterfaceTable.CreateInstance(CLSID_StdGlobalInterfaceTable));

            // get object
            MyLib::IFooPtr object;
            Assert::AreEqual(S_OK, globalInterfaceTable->GetInterfaceFromGlobal(cookie, MyLib::IID_IFoo, (LPVOID*)&object));

            // get async call factory
            ICallFactoryPtr callFactory;
            Assert::AreEqual(S_OK, object->QueryInterface(&callFactory));

            //
            // Everything is fine up until the CreateCall call below,
            // which fails with HRESULT 0x80040150
            //

            // create async call object
            IUnknownPtr callObject;
            Assert::AreEqual(S_OK, callFactory->CreateCall(MyLib::IID_AsyncIFoo, NULL, IID_IUnknown, &callObject));
        }
        // uninitialize COM
        CoUninitialize();

        // success
        return 0;
    }

回答1:

As far as I know, the universal marshaller does not work with Async COM. You need to build the (MIDL-generated) proxy (although, IIRC, you can merge the stubs if you're building a DLL).

You also need to register the proxy DLL (and build it with the -DREGISTER_PROXY_DLL define). Async COM needs more registry keys defined in order to make it work.