Search code examples
comcom+dcom

Host a COM server inside my process


I know I can register my COM object to be consumed inproc (via standard com registration) and outproc (via dcom) hosted in a dedicated dllhost.exe process.

But is it possible to have my COM object run in an external process to the consumer (like dcom) only not hosted in dllhost rather my own process?

My goal is to have a process (windows service) running my logic, and just host a COM object so that other processes can communicate with me using simple CoCreateInstance of the COM object. Is it possible?

Please don't offer other protocols as an answer, if it's not possible to host COM that way is a sufficient enough answer.

Thanks!


Solution

  • COM has the notion of DLL Surrogates:

    COM makes it possible to create DLL servers that can be loaded into a surrogate EXE process. This combines the ease of writing DLL servers with the benefits of executable implementation. Development tools like Microsoft Visual Studio facilitate the writing of DLL servers, but a DLL server in itself has limits

    dllhost.exe that we often see running (as one or multiple instances) in the background is in fact the default surrogate process, but you can write a custom one: Writing a Custom Surrogate. This is something that is rarely seen.

    However when you say "host a COM object so that other processes can communicate with me using simple CoCreateInstance of the COM object", I don't think you need all that plumbing (that can be rather complex to implement). Just host and register the COM object from your Windows Service. It's called an Out-Of-Process COM server as opposed to In-Process COM server (a DLL).