Deterministically destroying a COM object

R

Robert Rotstein

I have a homework assignment to access a COM object from a .NET client,
get some information from it, then deterministically destroy it.

I search the registry for the directory of the object's CodeBase, then
load the .dll assembly via System.Reflection.Assembly.LoadFile(). I
search the assembly for all types and all members within each type for
the specific method name I'm looking for. I cast the MemberInfo object
I find to a MethodInfo object and then do

object theCOMobject =
Activator.CreateInstance(desiredFuction.DeclaringType);

After I extract the information that I want, I do a

System.Runtime.InteropServices.Marshal.ReleaseComObject(theCOMobject);

But this causes an exception:

"An unhandled exception of type 'System.InvalidCastException' occurred
in mscorlib.dll
Additional information: Specified cast is not valid."

What is the correct way to deterministically destroy the COM object?

Robert R.
 
W

Willy Denoyette [MVP]

Robert Rotstein said:
I have a homework assignment to access a COM object from a .NET client, get
some information from it, then deterministically destroy it.

I search the registry for the directory of the object's CodeBase, then
load the .dll assembly via System.Reflection.Assembly.LoadFile(). I
search the assembly for all types and all members within each type for the
specific method name I'm looking for. I cast the MemberInfo object I find
to a MethodInfo object and then do

object theCOMobject =
Activator.CreateInstance(desiredFuction.DeclaringType);

After I extract the information that I want, I do a

System.Runtime.InteropServices.Marshal.ReleaseComObject(theCOMobject);

But this causes an exception:

"An unhandled exception of type 'System.InvalidCastException' occurred in
mscorlib.dll
Additional information: Specified cast is not valid."

What is the correct way to deterministically destroy the COM object?

Robert R.

Hmm... you are using LoadFile, so you are loading a .NET assembly not a COM
server dll.
That means that "theCOMobject" is NOT a __ComObject or derived from a
__ComObject, that is, your object is not a COM object but a CLR object.
Note that you can't author COM objects in .NET, you can only expose them,
with the help of the CLR, as COM objects to COM clients.


Willy.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top