Making an async COM call — do I have to create my own P/S?

╄→尐↘猪︶ㄣ 提交于 2019-12-16 18:04:52

问题


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.



来源:https://stackoverflow.com/questions/30216947/making-an-async-com-call-do-i-have-to-create-my-own-p-s

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!