::::::::: PowerShell :::::::::
Saturday, November 12, 2005
  [MSH] Disposing COM object.
I had a problem with this "Speak" function I had posted on 11/08/2005, Here

The problem was that, "Speak" function did not dispose the excel object create so even after MSH was exited, the COM object would still be alive and could be seen in Task Manager. So i hit the Newsgroup and have found the answer(Thanks to both "Jouko Kynsijärvi" and "Keith Hill") on how to dipose that darn COM object.


Keith Hill
Nov 12, 12:55 pm show options

Newsgroups: microsoft.public.windows.server.scripting
From: "Keith Hill" - Find messages by this author
Date: Sat, 12 Nov 2005 10:55:39 -0700
Local: Sat, Nov 12 2005 12:55 pm
Subject: Re: [MSH] How to dispose a COM object
Reply | Reply to Author | Forward | Print | Individual Message | Show original | Report Abuse


"Jouko Kynsijärvi" wrote in message


news:%23pf9pW65FHA.2040@TK2MSFTNGP14.phx.gbl...


> You can release the COM object by using Marshal.ReleaseComObject():



Keep in mind that if you marshal a COM object back and forth between
different COM servers and MSH, the ref count on the RCW can get bumped up
past 1. That means that if you really, really want to get rid of the COM
object you typically do this:

while
([System.Runtime.InteropServices.Marshal]::ReleaseComObject([System.__ComOb­ject]$excel)
-gt 0) {}


Essentially ReleaseComObject returns the new ref count and you want to
continue calling it until the ref count reaches 0. At least this was the
way it worked in .NET 1.1. And indeed the .NET 2.0 docs say:


Note
To ensure that the runtime callable wrapper and the original COM
object are released, construct a loop from which you call this method until
the returned reference count reaches zero.


Yeah I've been bit by this issue before.


--
Keith
 
Comments: Post a Comment

Links to this post:

Create a Link



<< Home
Let's get lazy with PowerShell!

Name:
Location: Flushing, NY, United States

Experimenting with a different format of blogs...

Links
ARCHIVES
10/01/2005 - 11/01/2005 / 11/01/2005 - 12/01/2005 / 12/01/2005 - 01/01/2006 / 01/01/2006 - 02/01/2006 / 02/01/2006 - 03/01/2006 / 03/01/2006 - 04/01/2006 / 04/01/2006 - 05/01/2006 / 05/01/2006 - 06/01/2006 / 06/01/2006 - 07/01/2006 / 07/01/2006 - 08/01/2006 / 08/01/2006 - 09/01/2006 / 10/01/2006 - 11/01/2006 / 11/01/2006 - 12/01/2006 /


Powered by Blogger