Windows Vista Forums
Vista Forums Home Join Vista Forums Donate Vista Tutorials Tags

Welcome to Vista Forums we are your forum to discuss Windows Vista x64 and x86 systems. Whether you need help or just want to post an idea you have on Vista, this is the forum for you.
Register at Vista forums...the world biggest Windows Vista resource Join Vista Forums Now

Go Back   Vista Forums > Microsoft Technical Newsgroups > PowerShell

Exit Notification Area App Gracefully

Update your Vista Drivers Update Your Drivers Now!!
Closed Thread
 
Thread Tools Display Modes
Old 07-02-2008   #1 (permalink)
huddie
Guest


 

Exit Notification Area App Gracefully

Hi,

I want to use a PowerShell script to exit a Notification Area app
gracefully. I've tried the obvious commands - TaskKill.exe and TSKill, and
neither suits my purpose. This is because they either forcibly terminate the
app (ungracefully), or just close any open window, but leave the app running
in the notification area (system tray / systray). What I want to do is
equivalent to right-clicking the app's notification area icon and selecting
'exit', but this must be done programmatically.

Any help would be gratefully appreciated.

--
Paul Anderson

My System SpecsSystem Spec
Old 07-02-2008   #2 (permalink)
Jon
Guest


 

Re: Exit Notification Area App Gracefully

Hi

Sending a friendly 'Close' message to its System menu should do it eg

#--------------------------
Function CloseApp {

param([string]$Name=$(Throw "Please specify an application name"))

$processes=[System.Diagnostics.Process]::GetProcessesByName($Name)
foreach ($Process in $Processes) {

$hwnd=$process.MainWindowHandle

$WM_SYSCOMMAND = 0x0112
$SC_CLOSE = 0xF060

if($hwnd){
[void] [Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms")
| Out-Null
$message=[System.Windows.Forms.Message]::Create($hwnd,$WM_SYSCOMMAND,$SC_CLOSE,0)
$nativeWindow=new-object System.Windows.Forms.NativeWindow
$nativeWindow.DefWndProc([ref]$message)
$nativeWindow=$null
}

} #Each process

} #Function
#--------------------------

CloseApp "notepad"
CloseApp -Name "sapisvr"

--
Jon


"huddie" <paul.1.anderson.NOSPAM@xxxxxx> wrote in message
news:BDE233CD-DD7A-4C90-9168-DF5406007F3D@xxxxxx
Quote:

> Hi,
>
> I want to use a PowerShell script to exit a Notification Area app
> gracefully. I've tried the obvious commands - TaskKill.exe and TSKill,
> and
> neither suits my purpose. This is because they either forcibly terminate
> the
> app (ungracefully), or just close any open window, but leave the app
> running
> in the notification area (system tray / systray). What I want to do is
> equivalent to right-clicking the app's notification area icon and
> selecting
> 'exit', but this must be done programmatically.
>
> Any help would be gratefully appreciated.
>
> --
> Paul Anderson
My System SpecsSystem Spec
Old 07-02-2008   #3 (permalink)
Jon
Guest


 

Re: Exit Notification Area App Gracefully

You'll probably need to play with that a bit. Only works if there's a
visible window ......

--
Jon


"Jon" <Email_Address@xxxxxx> wrote in message
news:eStQYaE3IHA.2580@xxxxxx
Quote:

> Hi
>
> Sending a friendly 'Close' message to its System menu should do it eg
>
> #--------------------------
> Function CloseApp {
>
> param([string]$Name=$(Throw "Please specify an application name"))
>
> $processes=[System.Diagnostics.Process]::GetProcessesByName($Name)
> foreach ($Process in $Processes) {
>
> $hwnd=$process.MainWindowHandle
>
> $WM_SYSCOMMAND = 0x0112
> $SC_CLOSE = 0xF060
>
> if($hwnd){
> [void] [Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms")
> | Out-Null
>
> $message=[System.Windows.Forms.Message]::Create($hwnd,$WM_SYSCOMMAND,$SC_CLOSE,0)
> $nativeWindow=new-object System.Windows.Forms.NativeWindow
> $nativeWindow.DefWndProc([ref]$message)
> $nativeWindow=$null
> }
>
> } #Each process
>
> } #Function
> #--------------------------
>
> CloseApp "notepad"
> CloseApp -Name "sapisvr"
>
> --
> Jon
>
>
> "huddie" <paul.1.anderson.NOSPAM@xxxxxx> wrote in message
> news:BDE233CD-DD7A-4C90-9168-DF5406007F3D@xxxxxx
Quote:

>> Hi,
>>
>> I want to use a PowerShell script to exit a Notification Area app
>> gracefully. I've tried the obvious commands - TaskKill.exe and TSKill,
>> and
>> neither suits my purpose. This is because they either forcibly terminate
>> the
>> app (ungracefully), or just close any open window, but leave the app
>> running
>> in the notification area (system tray / systray). What I want to do is
>> equivalent to right-clicking the app's notification area icon and
>> selecting
>> 'exit', but this must be done programmatically.
>>
>> Any help would be gratefully appreciated.
>>
>> --
>> Paul Anderson
>
My System SpecsSystem Spec
Old 07-02-2008   #4 (permalink)
huddie
Guest


 

Re: Exit Notification Area App Gracefully

Many thanks Jon.

You're right. It only closes an open app window. It doesn't exit the app.
Basically, it minimises to the systray. Same as using the taskkill /im
imagename command.

I was wondering is there a message I can send to the app to *exit*. Would
WM_QUIT work ? Even if it would, I'm no programmer, so don't know how your
code could be adapted to do this.

Could you help me by giving me some idea how I could use something like this
to close down apps in the system tray ? Basically a File / Exit application
exit.
--
--
Paul Anderson


"Jon" wrote:
Quote:

> You'll probably need to play with that a bit. Only works if there's a
> visible window ......
>
> --
> Jon
>
>
> "Jon" <Email_Address@xxxxxx> wrote in message
> news:eStQYaE3IHA.2580@xxxxxx
Quote:

> > Hi
> >
> > Sending a friendly 'Close' message to its System menu should do it eg
> >
> > #--------------------------
> > Function CloseApp {
> >
> > param([string]$Name=$(Throw "Please specify an application name"))
> >
> > $processes=[System.Diagnostics.Process]::GetProcessesByName($Name)
> > foreach ($Process in $Processes) {
> >
> > $hwnd=$process.MainWindowHandle
> >
> > $WM_SYSCOMMAND = 0x0112
> > $SC_CLOSE = 0xF060
> >
> > if($hwnd){
> > [void] [Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms")
> > | Out-Null
> >
> > $message=[System.Windows.Forms.Message]::Create($hwnd,$WM_SYSCOMMAND,$SC_CLOSE,0)
> > $nativeWindow=new-object System.Windows.Forms.NativeWindow
> > $nativeWindow.DefWndProc([ref]$message)
> > $nativeWindow=$null
> > }
> >
> > } #Each process
> >
> > } #Function
> > #--------------------------
> >
> > CloseApp "notepad"
> > CloseApp -Name "sapisvr"
> >
> > --
> > Jon
> >
> >
> > "huddie" <paul.1.anderson.NOSPAM@xxxxxx> wrote in message
> > news:BDE233CD-DD7A-4C90-9168-DF5406007F3D@xxxxxx
Quote:

> >> Hi,
> >>
> >> I want to use a PowerShell script to exit a Notification Area app
> >> gracefully. I've tried the obvious commands - TaskKill.exe and TSKill,
> >> and
> >> neither suits my purpose. This is because they either forcibly terminate
> >> the
> >> app (ungracefully), or just close any open window, but leave the app
> >> running
> >> in the notification area (system tray / systray). What I want to do is
> >> equivalent to right-clicking the app's notification area icon and
> >> selecting
> >> 'exit', but this must be done programmatically.
> >>
> >> Any help would be gratefully appreciated.
> >>
> >> --
> >> Paul Anderson
> >
>
>
My System SpecsSystem Spec
Old 07-02-2008   #5 (permalink)
Jon
Guest


 

Re: Exit Notification Area App Gracefully

"huddie" <paul.1.anderson.NOSPAM@xxxxxx> wrote in message
news:F5EFEC43-05B3-41C2-A2BC-35B63965F9FC@xxxxxx
Quote:

> Many thanks Jon.
>
> You're right. It only closes an open app window. It doesn't exit the
> app.
> Basically, it minimises to the systray. Same as using the taskkill /im
> imagename command.
>
> I was wondering is there a message I can send to the app to *exit*. Would
> WM_QUIT work ? Even if it would, I'm no programmer, so don't know how
> your
> code could be adapted to do this.
>
> Could you help me by giving me some idea how I could use something like
> this
> to close down apps in the system tray ? Basically a File / Exit
> application
> exit.
> --
> --
> Paul Anderson
>


Hi Huddie

This may be a better approach. I do something similar to this to control
the behaviour of the sidebar in Vista.

If you're the one who initially starts an application, then you can keep a
hold on it, so to speak, and then terminate it (hopefully) gracefully when
appropriate.

#Start an application
$WshShell = New-Object -ComObject "WScript.Shell"
$oExec = $WshShell.Exec('notepad.exe')
$oExec = $WshShell.Exec('C:\Program Files\Windows Sidebar\sidebar.exe')

#Close it later
#start-sleep 10
$oExec.Terminate()
$WshShell = $oExec = $null

I think this uses wm_close messages under the hood, and seems to work better
with systray icons from what I can see.

Hope that helps anyhow.

--
Jon



My System SpecsSystem Spec
Old 07-02-2008   #6 (permalink)
Jon
Guest


 

Re: Exit Notification Area App Gracefully



"Jon" <Email_Address@xxxxxx> wrote in message
news:%23bat6CG3IHA.2348@xxxxxx
Quote:

> $WshShell = New-Object -ComObject "WScript.Shell"
> $oExec = $WshShell.Exec('notepad.exe')
> $oExec = $WshShell.Exec('C:\Program Files\Windows Sidebar\sidebar.exe')
>
Should have been only one definition active for $oExec there ......

$WshShell = New-Object -ComObject "WScript.Shell"
#$oExec = $WshShell.Exec('notepad.exe')
$oExec = $WshShell.Exec('C:\Program Files\Windows Sidebar\sidebar.exe')

--
Jon



My System SpecsSystem Spec
Old 07-03-2008   #7 (permalink)
huddie_l
Guest


 

Re: Exit Notification Area App Gracefully

On Jul 2, 5:32*pm, "Jon" <Email_Addr...@xxxxxx> wrote:
Quote:

> "Jon" <Email_Addr...@xxxxxx> wrote in message
>
> news:%23bat6CG3IHA.2348@xxxxxx
>
Quote:

> > $WshShell = New-Object -ComObject "WScript.Shell"
> > $oExec = $WshShell.Exec('notepad.exe')
> > $oExec = $WshShell.Exec('C:\Program Files\Windows Sidebar\sidebar.exe')
>
> Should have been only one definition active for $oExec there ......
>
> $WshShell = New-Object -ComObject "WScript.Shell"
> #$oExec = $WshShell.Exec('notepad.exe')
> $oExec = $WshShell.Exec('C:\Program Files\Windows Sidebar\sidebar.exe')
>
> --
> Jon
That's great Jon. I'll give have a go at both starting and
terminating the apps using WSH. Thanks for the tip!
My System SpecsSystem Spec
Closed Thread

Thread Tools
Display Modes



Similar Threads
Thread Thread Starter Forum Replies Last Post
Task Bar Notification area Fred S ***** Vista General 1 07-19-2008 11:06 AM
Network in the notification area Christoph Boget Vista General 1 07-09-2008 01:16 PM
notification area JHC Vista General 4 05-07-2008 05:21 PM
taskbar Notification area JeBuchanan Vista General 1 04-28-2008 07:12 AM
Notification Area spjr2 Vista General 10 10-18-2007 05:38 PM


Vistax64.com is an independent web site and has not been authorized,
sponsored, or otherwise approved by Microsoft Corporation.
"Windows Vista", the Start Orb, and related materials are trademarks of Microsoft Corp.
© Designer Media 2005-2008

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51