CoCreateInstance returning E_NOINTERFACE even though interface is found

后端 未结 4 1277
温柔的废话
温柔的废话 2020-12-05 20:58

I have a COM class CMyCOMServer implementing IMyInterface in one application, both with correct GUIDs. CMyCOMServer::QueryInterface wi

4条回答
  •  广开言路
    2020-12-05 21:41

    This happens because COM subsystem tries to marshal your custom interface (IMyInterface) and simply has no idea how to do that. That happens either because the server is out-proc or because the server is in-proc and the thread of the consumer application that calls CoCreateInstance() has called CoInitialize()/ CoInitializeEx() incorrectly so that "multithreaded apartment" is requested as mentioned in the article user Thomas refers to in the other answer.

    If you only need an in-proc server you could suppress marshalling by ensuring that the thread calling CoCreateInstance() either calls CoInitialize() or CoInitializeEx() with COINIT_APARTMENTTHREADED to enforce "single-threaded apartment".

    If you need an out-proc server you can't get around marshalling. In the latter case you could do one of the following:

    • implement IMarshal - least preferable
    • add proxy/stubs and register them for your custom interface
    • (not sure if it will work for out-proc, but it's the simplest) if your interface can be marshalled with automation marshaller simply include a typlib into the resources of your COM server and register that typelib in the registry.

提交回复
热议问题