tags:

views:

622

answers:

3

I know how to create a COM DLL (a Class Library) in C#. Is it possible to create a COM Surrogate EXE using C#?

This would be a standalone server capable of launching and hosting COM objects, which would then be accessible to COM clients.

A: 

One option, if you want an out-of-process COM component, is to host a dll in COM+ via serviced components. This only supports dll though, but you could write a shell exe (for standalone use) that simply defers to the dll.

Not quite as simple as VB, but it works.

I do remember somebody showing me a more direct way (no COM+), but I can't for the life of me remember what it was...

Marc Gravell
+1  A: 

The default surrogate process for COM - the thing that hosts COM DLLs, aka the COM Surrogate - is dllhost.exe. It is possible to create a surrogate process in C++. This article explains how.

But those APIs are not exposed in wrappers as part of the base class library in the .NET Framework. If you want to write to write only managed code, you need something else.

I see a couple options.

  1. The Visual Studio SDK, a free download that is intended for devs who want to extend Visual Studio. Within that SDK, there's a class lib that has such wrappers. In particular, look at the ISurrogate class.
    BUT, the VS SDK license says that the SDK is ok to use only for products that extend or add value to Visual Studio. I am no lawyer, but that is my understanding of the license, which is pretty clear. These terms means the VS SDK would not be useful for general app building.
    The one remaining question is, exactly how do you use the VS SDK technically to produce a COM Surrogate using only C# code? Again, here I don't know. I looked in the docs briefly for guides on using the ISurrogate wrapper, but found none.
  2. Use the code in this article.
    The article explores a bunch of different aspects around COM and .NET interop. Towards the end of the article it offers source code for building your own COM server in C#, complete with all the p/invoke calls to CoRegisterClassObject() and friends.
Cheeso
A: 

I wanted to make same thing and found excellent project example "CSExeCOMServer" on All-In-One Code Framework http://1code.codeplex.com. It actually reconstructs normal COM server logic by means of .NET and native calls to Windows API. But it looks all still overcomplicated. I suppose there is no simple and fast way to expose .NET objects as COM in out-of-process server and it is not the architecture of choice.

Boris Zinchenko