1

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?

1 Answer 1

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); 
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.