Marshal .ReleaseComObject exception

Trying to use COM visible .NET class through another .NET application and get an exception:

Message: The type of the object must be __ComObject or derived from __ComObject.

Parameter Name: o

Stack Trace: with System.Runtime.InteropServices.Marshal.ReleaseComObject (Object o)

The class is as follows:

[ComVisible(true)]
[InterfaceType(ComInterfaceType.InterfaceIsDual)]
public interface IViewer : IComInteropDefinedInterface
{
}

[ClassInterface(ClassInterfaceType.None)]
[ComVisible(true)]
[Guid("[some guid]")]
public class MyViewer : UserControl, IViewer    
{
    //IViewer implementation
}

I register component with

regasm [Assembly Path] /tlb /codebase

The client application, which is also located in .NET, successfully creates this class, but when it calls Marshal.ReleaseComObject(), it receives the exception described above.

Any idea to solve this problem?

EDIT: Unfortunately, I cannot provide client application code to instantiate my object. However, I know that the client uses the same method to create real COM objects.

+4
4

, COM . , , COM Marshal.IsComObject, COM Marshal.ReleaseComObject.

:

if (Marshal.IsComObject(comObject))
{
     Marshal.ReleaseComObject(comObject);
}
comObject = null;

: , .

: http://blogs.msdn.com/b/visualstudio/archive/2010/03/01/marshal-releasecomobject-considered-dangerous.aspx

+3

? new MyViewer() COM-. .Net-, ReleaseComObject.

, MyViewer , , COM-. , , COM-.

ReleaseComObject, COM/RCW.

0

, COM, .NET.

MyViewer viewer = new MyViewer();

, MyViewer COM, COM.

0

:

if (comObject != null)
{
      if (System.Runtime.InteropServices.Marshal.IsComObject(comObject))
      {                
          System.Runtime.InteropServices.Marshal.ReleaseComObject(comObject);
      }
      comObject= null;
}
0

Source: https://habr.com/ru/post/1761265/


All Articles