Click to See Complete Forum and Search --> : How to Terminate a running application from another application?


vhgupta
March 29th, 2001, 10:31 PM
I want to close a application
from another applicatioon so plz help me
out.
Expecting details....as the level is beginner
rgds,

:-)

cksiow
March 29th, 2001, 11:08 PM
if you know the window caption,

you can try

http://vblib.virtualave.net, there is a function call ShutDownApp in vbSystem to terminate it.

hope this help.

shree
March 30th, 2001, 08:22 AM
You save the handle to the window that you created and then kill it later. The following code should help you.


option Explicit

private Declare Function WaitForSingleObject Lib "kernel32" _
(byval hHandle as Long, _
byval dwMilliseconds as Long) as Long

private Declare Function FindWindow Lib "user32" _
Alias "FindWindowA" _
(byval lpClassName as string, _
byval lpWindowName as string) as Long

private Declare Function PostMessage Lib "user32" _
Alias "PostMessageA" _
(byval hwnd as Long, _
byval wMsg as Long, _
byval wParam as Long, _
byval lParam as Long) as Long

private Declare Function IsWindow Lib "user32" _
(byval hwnd as Long) as Long

'Constants used by the API functions
Const WM_CLOSE = &H10
Const INFINITE = &HFFFFFFFF

private Sub Form_Load()
Command1.Caption = "Start the Calculator"
Command2.Caption = "Close the Calculator"
End Sub

private Sub Command1_Click()
'Starts the Windows Calculator
Shell "calc.exe", vbNormalNoFocus
End Sub

private Sub Command2_Click()
'Closes the Windows Calculator
Dim hWindow as Long
Dim lngResult as Long
Dim lngReturnValue as Long

hWindow = FindWindow(vbNullString, "Calculator")
lngReturnValue = PostMessage(hWindow, WM_CLOSE, vbNull, vbNull)
lngResult = WaitForSingleObject(hWindow, INFINITE)

'Does the handle still exist?
If IsWindow(hWindow) = 0 then
'The handle still exists. Use the TerminateProcess function
'to close all related processes to this handle.
MsgBox "Handle still exists."
else
'Handle does not exist.
MsgBox "Program closed."
End If
End Sub

Cimperiali
October 11th, 2001, 03:04 AM
This is worth an excelent - but I am already out of votes. Someone else, please?...
;-)

Have a nice day and happy coding.


Special thanks to Lothar "the Great" Haensler, Tom Archer, Chris Eastwood, TCartwright, Bruno Paris, Dr_Michael
and all the other wonderful people who made and make Codeguru a great place.
Come back soon, you Gurus.

The Rater