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.__ComObject]$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
SqlContext.Pipe.Execute();
SqlContext.Pipe.Send();
// Instead of
SqlContext.Pipe.ExecuteAndSend();
# ShellUtil.msh
# author: dance2die
# original code author: /\/\o\/\/# date: 11/11/2005 @ 08:51PM
[System.Reflection.Assembly]::LoadWithPartialName("System.Drawing")
[System.Reflection.Assembly]::LoadWithPartialName("System.windows.forms")
function ShellUtil {
$shell = new-object -com shell.application
$form = new-object System.Windows.Forms.form
$cm = new-object System.Windows.Forms.ContextMenu
$mi = new-object System.Windows.Forms.MenuItem
$ni = new-object System.Windows.Forms.NotifyIcon
# Display "Show Desktop" menu
$miToggle = new-object System.Windows.Forms.MenuItem
$miToggle.Index = 0
$miToggle.Text = "&Show Desktop"
# Display "Minimize All" menu
$miMinAll = new-object System.Windows.Forms.MenuItem
$miMinAll.Text = "&Minimize All Windows"
$miMinAll.Index = 1
# Display "Undo Minimize All" menu
$miUndoMinAll = new-object System.Windows.Forms.MenuItem
$miUndoMinAll.Index = 2
$miUndoMinAll.Text = "&Undo Minimize All Windows";
# Display "Tray Properties" menu
$miTrayProps = new-object System.Windows.Forms.MenuItem
$miTrayProps.Index = 3
$miTrayProps.Text = "&Tray Properties..."
# Display "Cascade Windows" menu
$miCascade = new-object System.Windows.Forms.MenuItem
$miCascade.Index = 4
$miCascade.Text = "&Cascade Windows"
# Display "Tile Horizontally" menu
$miTileH = new-object System.Windows.Forms.MenuItem
$miTileH.Index = 5
$miTileH.Text = "Tile Windows &Horizontally"
# Display "Tile Vertically" menu
$miTileV = new-object System.Windows.Forms.MenuItem
$miTileV.Index = 6
$miTileV.Text = "Tile Windows &Vertically"
# Display "Exit" menu
$miExit = new-object System.windows.Forms.MenuItem
$miExit.Index = 10
$miExit.Text = "E&xit"
$form.contextMenu = $cm
$form.contextMenu.MenuItems.Add($miToggle)
$form.contextMenu.MenuItems.Add($miMinAll)
$form.contextMenu.MenuItems.Add($miUndoMinAll)
$form.contextMenu.MenuItems.Add($miTrayProps)
$form.contextMenu.MenuItems.Add($miCascade)
$form.contextMenu.MenuItems.Add($miTileH)
$form.contextMenu.MenuItems.Add($miTileV)
$form.contextMenu.MenuItems.Add($miExit)
$form.ShowInTaskbar = $False
$form.WindowState = "minimized"
$form.add_Closing({ $form.ShowInTaskBar = $False })
$ni = $ni
$ni.Icon = New-object System.Drawing.Icon("c:\programming\msh\icons\test.ico")
$NI.ContextMenu = $cm
$NI.Text += "Shell Utility";
$tipTitle = "Bah"
$tipText = "Bah"
$miToggle.add_Click({
$shell.ToggleDesktop()
$tipTitle = "Show Desktop"
$tipText = "Toggle Desktop..."
})
$miMinAll.add_Click({
$shell.MinimizeAll()
$tipTitle = "Minimize All..."
$tipText = "Minimize All windows..."
})
$miUndoMinAll.add_Click({
$shell.UndoMinimizeAll()
$tipTitle = "Undo minimize all"
$tipText = "Undo minimize all"
})
$miTrayProps.add_Click({
$shell.TrayProperties()
$tipTitle = "Display Tray Properties"
$tipText = "Modify system tray properties..."
})
$miCascade.add_Click({
$shell.CascadeWindows()
$tipTitle = "Cascade windows"
$tipText = "Cascade all windows"
})
$miTileH.add_Click({
$shell.TileHorizontally()
$tipTitle = "Tile Horizontally"
$tipText = "Tile windows horizontally"
})
$miTileV.add_Click({
$shell.TileVertically()
$tipTitle = "Tile Vertically"
$tipText = "Tile windows vertically"
})
$miExit.add_Click({
$form.Close()
$NI.visible = $false
})
$ni.ShowBalloonTip(0, $tipTitle, $tipText, [System.Windows.Forms.ToolTipIcon]"Info")
$NI.Visible = $True
$form.showDialog()
}
ShellUtil
function speak {
param([string] $text = "Hello, World!")
$ex = new-object -com excel.application
$ex.speech.speak($text, $null, $null, $null)
}
MSH> speak "your mind..."
# original code author: /\/\o\/\/
function startHere {
param([string] $dir = (pwd).path)
$sh = new-object -com shell.application
$sh.explore($dir)
}
[System.Reflection.Assembly]::LoadWithPartialName("System.Drawing")
[System.Reflection.Assembly]::LoadWithPartialName("System.windows.forms")
function weather {
$form = new-object System.Windows.Forms.Form
$cm = new-object System.Windows.Forms.ContextMenu
$miExit = new-object System.Windows.Forms.MenuItem
$miRefresh = new-object System.Windows.Forms.MenuItem
$ni = new-object System.Windows.Forms.NotifyIcon
$form.ContextMenu = $cm
$form.ContextMenu.MenuItems.Add($miRefresh)
$form.ContextMenu.MenuItems.Add($miExit)
$form.ShowInTaskbar = $false
$form.WindowState = "minimized"
$form.add_Load({
$wc = new-object System.Net.WebClient
$uri = "http://weather.msn.com/RSS.aspx?wealocations=wc:USNY0833&weadegreetype=F"
$rssData = [xml] $wc.DownloadString($uri)
$title = $rssData.rss.channel.title
$desc = $rssData.rss.channel.item[0].title
$ni.ShowBalloonTip(100, $title, $desc, [system.windows.forms.ToolTipIcon]"Info")
})
$ni = $ni
$ni.Icon = new-object System.Drawing.Icon("c:\msh\icons\test.ico")
$ni.ContextMenu = $cm
$miRefresh.Index = 0
$miRefresh.Text = "&Refresh"
$miRefresh.add_Click({
$wc = new-object System.Net.WebClient
$uri = "http://weather.msn.com/RSS.aspx?wealocations=wc:USNY0833&weadegreetype=F"
$rssData = [xml] $wc.DownloadString($uri)
$title = $rssData.rss.channel.title
$desc = $rssData.rss.channel.item[0].title
$ni.ShowBalloonTip(100, $title, $desc, [system.windows.forms.ToolTipIcon]"Info")
})
$miExit.Index = 1
$miExit.Text = "E&xit"
$miExit.add_Click({ $form.Close() })
$ni.visible = $true
$form.showDialog()
}
weather
Experimenting with a different format of blogs...