admin管理员组

文章数量:1221299

I have a Outproc COM server exe created in C++ using ATL which implements interface methods.

I have 2 test clients to test this. One is native C++ and other is Managed.

Native test client implementation as below

::CoInitialize(NULL);
 
IMyInterfacePtr pMyInterfacePtr;

HRESULT hr = ::CoCreateInstance(__uuidof(CMyImpl), NULL, CLSCTX_LOCAL_SERVER, __uuidof(IMyInterface), 
    (void**) &pMyInterfacePtr);

if (SUCCEEDED(hr))
{
   
    hr = pMyInterfacePtr->MyMethod();
    if (SUCCEEDED(hr))
    {
        wprintf(L"My method called successfully"); 
    }
    else
    {
        wprintf(L"My method failed"); 
    } 
}
else
{
    wprintf(L"Failed to create instance of CMyImpl\n");
}

I have created managed assembly using tlb and refferring the managed assembly in my Managed C# client.

Managed C# Client implementatio is as below,

 static void Main(string[] _)
 {
     MyImpl myImpl = new MyImpl();
     myImpl.MyMethod();
 }

In both cases, When object is created new instance of my COM exe is getting lauched and MyMethod is getting executed in out of proc.

In case of Native client when execution is completed the outproc exe is automatically getting closed and I could see call to destructor of the MyImpl.

But incase of managed C# client when execution is completed the outproc exe is not getting closed. It's still running.

Is there any special handling required for Managed client?

I have a Outproc COM server exe created in C++ using ATL which implements interface methods.

I have 2 test clients to test this. One is native C++ and other is Managed.

Native test client implementation as below

::CoInitialize(NULL);
 
IMyInterfacePtr pMyInterfacePtr;

HRESULT hr = ::CoCreateInstance(__uuidof(CMyImpl), NULL, CLSCTX_LOCAL_SERVER, __uuidof(IMyInterface), 
    (void**) &pMyInterfacePtr);

if (SUCCEEDED(hr))
{
   
    hr = pMyInterfacePtr->MyMethod();
    if (SUCCEEDED(hr))
    {
        wprintf(L"My method called successfully"); 
    }
    else
    {
        wprintf(L"My method failed"); 
    } 
}
else
{
    wprintf(L"Failed to create instance of CMyImpl\n");
}

I have created managed assembly using tlb and refferring the managed assembly in my Managed C# client.

Managed C# Client implementatio is as below,

 static void Main(string[] _)
 {
     MyImpl myImpl = new MyImpl();
     myImpl.MyMethod();
 }

In both cases, When object is created new instance of my COM exe is getting lauched and MyMethod is getting executed in out of proc.

In case of Native client when execution is completed the outproc exe is automatically getting closed and I could see call to destructor of the MyImpl.

But incase of managed C# client when execution is completed the outproc exe is not getting closed. It's still running.

Is there any special handling required for Managed client?

Share Improve this question asked Feb 7 at 8:37 srajeshnklsrajeshnkl 8953 gold badges16 silver badges51 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 1

I found that Marshal.ReleaseComObject can release the Marshaled data, Modified the Managed C# code like below, now the out proc exe is closing after release.

object obj;
 int hr = Ole32.CoCreateInstance(Constants.MyImplClassGuid,
     IntPtr.Zero, Ole32.CLSCTX_LOCAL_SERVER, typeof(IMyInterface).GUID, out obj);
 if (hr < 0)
 {
     Marshal.ThrowExceptionForHR(hr);
 }

 var server = (IMyInterface)obj;
 string output = server.MyMethod();

 Marshal.ReleaseComObject(obj); 

本文标签: cOutProc COM from Managed client not closing the out proc server exeStack Overflow