PC Review


Reply
Thread Tools Rate Thread

Deterministically destroying a COM object

 
 
Robert Rotstein
Guest
Posts: n/a
 
      18th Sep 2005
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.
 
Reply With Quote
 
 
 
 
Willy Denoyette [MVP]
Guest
Posts: n/a
 
      18th Sep 2005

"Robert Rotstein" <(E-Mail Removed)> wrote in message
news:uifXe.5012$e_4.1032@trndny08...
>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.




 
Reply With Quote
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
destroying Explorer object pointers on shutdown Gabriel Microsoft Outlook Program Addins 2 4th May 2009 08:57 PM
Destroying my HD Zachary Burns Windows XP Hardware 6 21st Oct 2004 02:45 PM
What is the correct way of destroying an object who is listening to delegate events? Darren Microsoft C# .NET 1 6th Sep 2004 04:56 PM
Object destroying problem... Roman S. Golubin1709176985 Microsoft C# .NET 1 12th Sep 2003 04:06 PM
Re: Destroying objects?!?!?! Jay Martinez Microsoft VB .NET 0 9th Jul 2003 05:11 AM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 04:02 AM.