Search code examples
c#.netcomipccom-interop

COM Interop with IPC in C#


I have a question about IPC with COM Interop objects. I want to create and fill with data COM Interop object in one process (for example Windows Service written in C#). Then I want to get it in another process (console application in C#). I know that COM objects don't serialize and they are unmanaged code. I tried realize solution by .Net Remoting (Windows Service is server, console application is client), but I couldn't get COM Interop object in client (I made .net class inherited from MarshalByRef in shared library, created public property with COM Interop object). Can anyone tell me, what can I do?


Solution

  • COM objects are not serializable as such, but when writing a COM object, you're supposed to use sophisticated proxy / stub definition - althouh in general, undercover, you don't really have to think about it, especially if these objects are written using some framework (ATL, delphi, VB6, .NET, etc.).

    These proxies / stubs will at compile time or runtime use powerful RPC mechanisms (which is more or less "DCOM") if the client-server communication works out-of-process or across threads (COM "apartments" in fact).

    All this to say that if your COM objects are in-process servers and were correctly built, COM provides free services to host them as "COM+ applications".

    By creating logical groups of COM components as COM+ applications, you can take advantage of the following benefits of COM+: Component dynamic-link libraries (DLLs) loaded into processes (DLLHost.exe) on demand and Managed (nothing to do with .NET here) server processes to host components.

    If you put your object in a COM+ Application, then you should be able to use it from .NET code just like it was in-process, but now hosted in a special "surrogate" system-provided host process (which happens to be the famous dllhost.exe). You can even configure it to become a full Windows service as shown here in the "Activation" tab of the application configuration:

    enter image description here