Hi ZABU,
In the Sub Test, we will post a message to the window of notepad and tell
it to close, we need to find the applicaiton's main window first before use
the method.
In the Sub Test2, we will use the terminateprocess, but as I said before it
is not recommend to do so, which may cause some underlying job.
The TerminateProcess function is used to unconditionally cause a process to
exit. Use it only in extreme circumstances. The state of global data
maintained by dynamic-link libraries (DLLs) may be compromised if
TerminateProcess is used rather than ExitProcess.
TerminateProcess initiates termination and returns immediately. This stops
execution of all threads within the process and requests cancellation of
all pending I/O. The terminated process cannot exit until all pending I/O
has been completed or canceled.
For detailed information, you may take a look at the link below.
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dllproc/bas
e/terminateprocess.asp
Here goes the code.
Public Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal
lpClassName As String, ByVal lpWindowName As String) As Long
Public 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
Public Declare Function CloseHandle Lib "kernel32" (ByVal hObject As Long)
As Long
Public Declare Function OpenProcess Lib "kernel32" (ByVal dwDesiredAccess
As Long, ByVal bInheritHandle As Long, ByVal dwProcessId As Long) As Long
Public Declare Function TerminateProcess Lib "kernel32" (ByVal hProcess As
Long, ByVal uExitCode As Long) As Long
Public Const WM_CLOSE = &H10
Public Const INFINITE = -1&
Public Const SYNCHRONIZE = &H100000
Public Const STANDARD_RIGHTS_REQUIRED = &HF0000
Sub Test()
Dim hwnd As Long
hwnd = FindWindow("NotePad", vbNullString)
Dim rt As Long
rt = PostMessage(hwnd, WM_CLOSE, 0&, 0&)
End Sub
Sub Test2()
Dim task As Long
Dim handle As Long
Dim result As Long
task = Shell("notepad.exe", vbNormalFocus)
handle = OpenProcess(SYNCHRONIZE Or STANDARD_RIGHTS_REQUIRED Or &HFFF,
False, task)
result = TerminateProcess(handle, 0)
result = CloseHandle(handle)
End Sub
Best regards,
Peter Huang
Microsoft Online Partner Support
Get Secure! -
www.microsoft.com/security
This posting is provided "AS IS" with no warranties, and confers no rights.