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

前端 未结 9 811
无人共我
无人共我 2020-11-29 02:23

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 becaus

9条回答
  •  萌比男神i
    2020-11-29 02:32

    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 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.

提交回复
热议问题