views:

47

answers:

2

Trying to use a COM visible .NET class via other .NET application and get exception:

Message: The object's type must be __ComObject or derived from __ComObject.

Parameter name: o

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

The class looks 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 the component with:

regasm [Assembly Path] /tlb /codebase

The client application, which is also in .NET instantiates successfully the given class, but when he callsMarshal.ReleaseComObject() it gets the exception described above.

Any idea for solving this problem?

EDIT: Unfortunately I can't provide the client application code for instantiating my object. However I know the client is using the same method to instantiate real COM objects.

A: 

But how are you creating the class instance? Simply using the expression new MyViewer() doesn't create a COM object. Instead it creates a plain old .Net object which cannot be used with the ReleaseComObject method.

Based on your sample code, in particular the line about MyViewer having an implementation, it doesn't sound like you're dealing with a COM object. Instead it looks like you have a managed object which implements a COM interface.

In order to use the ReleaseComObject you'd need to actually have a COM / RCW object.

JaredPar
If the assembly containing `MyViewer` was added as a COM reference, wouldn't you rather get a COM object with `new MyViewer()` then (instead of a plain old .NET object)? I can't think of a reason why to do so but it *might* be possible.
0xA3
@0xA3 yes if this type was a CoClass in a PIA (at least AFAIK). But in this case it's just a plain old managed object so new isn't doing anything special.
JaredPar
@JaredPar doesn't COM registered .NET class mean having an object that implements all the basic COM functionality including Release?
Aaron
@Aaron it depends on which part of the object. The .Net side doesn't have the Add/Release methods because it's simple not necessary on a .Net object. The CCW (COM callable wrapper) which is generated by the CLR will have them out of necessity.
JaredPar
A: 

My guess would be that you are actually not using COM but simply use a referenced .NET class. If your project contains code like

MyViewer viewer = new MyViewer();

and you have added the library containing MyViewer not as a COM reference, you are actually not using COM.

0xA3