Create Out-Of-Process COM in C#/.Net?

2020-01-26 05:16发布

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.

标签: c# com
9条回答
爷的心禁止访问
2楼-- · 2020-01-26 05:23

It could be done using umanaged ATL framework and plumbing it with the managed code (simple by changing the result project properties to /clr).

Here are illustrative snippets:

.H-part:

\#include < vcclr.h >

\#using < MyCSharpModule.dll >

using namespace System;

class ATL_NO_VTABLE MyCSharpProxyServer :
    public CComObjectRootEx< CComMultiThreadModel >,

.....

{

      HRESULT FinalConstruct();

      STDMETHODIMP LoadMyFile(BSTR FileName);
.....
      gcroot<MyCSNamespace::MyCSharpClass^> m_CSClass;

}

.CPP-part:

using namespace System::Collections;

using namespace MyCSNamespace;

HRESULT MyCSharpProxyServer::FinalConstruct()
{

    m_CSClass = gcnew MyCSharpClass();

}

STDMETHODIMP MyCSharpProxyServer::LoadMyFile(BSTR FileName)
{

    try {
       int hr = m_CSClass->LoadFile(gcnew String( FileName));
        return hr;
    }
    catch( Exception^ e )  {
        return E_FAIL;
    }
}

The C# part (MyCSharpClass class) lives in a separate project with output type Class Library.

查看更多
3楼-- · 2020-01-26 05:31

ActiveX.NET, is a true Out-Of-Proc (EXE) COM Server implementation in C#.NET. This one has a cleaner implementation compared to the original CSExeCOMServer, published in Code.MSDN.

ActiveX.NET has features like it does use a .NET Message Pump (instead of native) and uses MEF Plugin model, so that the EXE Server is decoupled and can be shared among multiple COM Plugins, which can be developed independently

查看更多
女痞
4楼-- · 2020-01-26 05:34

The visual studio project "CSExeCOMServer" that you can find here (All-in-One), gives a full example.

查看更多
Evening l夕情丶
5楼-- · 2020-01-26 05:35

Use the flag REGCLS_MULTIPLEUSE with CoRegisterClassObject() to make the coclass a multiuse class. Here is more info: http://support.microsoft.com/kb/169321.

查看更多
Evening l夕情丶
6楼-- · 2020-01-26 05:37

IMO, one of the ways through which this can be done is to create a normal COM Dll as per the method you mentioned in the link and then after registering your COM dll, change it to a surrogate DLL. This can be done very easily through OLEView utility, although you can do it manually as well by changing registry entries as well through the method mentioned at http://msdn.microsoft.com/en-us/library/ms686606(VS.85).aspx.

By making this a surrogate DLL, it will run in it's own dllhost.exe and hence will be out-of-process.

查看更多
仙女界的扛把子
7楼-- · 2020-01-26 05:38

You can use RegistrationServices.RegisterTypeForComClients, which is the managed equivalent of CoRegisterClassObject - for sample code see here.

查看更多
登录 后发表回答