I need to create an out-of-process COM server (.exe) in C# that will be accessed by multiple other processes on the same box. The component has to be a single process because it will cache the information it provides to its consumers in memory.
Note: the processes that will access my COM Server are mostly Matlab processes, thus the necessity for a COM interface.
I have seen threads regarding creating in-process COM components in .Net on stack overflow (Create COM ...) and on the web, but am having a hard time to find a way to create out-of-process components with .Net.
How is this achievable? Any suggested references?
Thanks.
The official answer is in KB article How to develop an out-of-process COM component by using Visual C++, Visual C#, or Visual Basic .NET.
One option is serviced components - i.e. host it in COM+ as the shell exe. See also the howto here.
We too had some issues many years ago with regasm and running the COM class as a Local EXE Server.
This is a bit of a hack and I'd welcome any suggestions to make it more elegant. It was implemented for a project back in the .NET 1.0 days and has not been touched since then!
Basically it performs a regasm style of registration each time the application starts (it needs to be run once to make registry entries before the COM object is instantiated in the COM container application).
I've copied the following important bits from our implementation and renamed a few classes to illustrate the example.
The following method is called from the Form Loaded event to register the COM class(renamed to
MyCOMClass
for this example)For the type in question,
MyCOMObject
, will need some special attributes to be COM compatible. One important attribute is to specify a fixed GUID otherwise each time you compile the registry will fill up with orphaned COM GUIDs. You can use the Tools menu in VisualStudio to create you a unique GUID.The
InitialiseCOM
method in the main form usesRegistrationServices
to register the type. The framework then uses reflection to find the method marked with theComRegisterFunction
attribute and calls that function with the type being registered.The
ComRegisterFunction
marked method, hand creates the registry settings for a Local EXE Server COM object and this can be compared with regasm if you useREGEDIT
and find the keys in question.I've commented out the three
\\Registry.ClassesRoot.CreateSubKey
method calls as this was another reason we needed to register the type ourselves as this was an OPC server and third party OPC clients use these implemented categories to scan for compatible OPC servers. REGASM would not add these in for us unless we did the work ourselves.You can easily see how this works if you put break points on the functions as it is starting.
Our implementation used an interface that was already registered with COM. For your application you will either need to :-