|
-
Jul 26th, 2001, 03:45 AM
#1
Thread Starter
Member
Close Application
I called other application from my program, say Notepad, using ShellExecute function (winapi). I got the window (Notepad) handle from FindWindow function(winapi). But after that, I don't know how to close it. I couldn't use DestroyWindow because the application (Notepad) doesn't belong to my program. Can anyone suggest a way to close this application (Notepad)? Thanks a lot!!!
Last edited by benchin; Apr 23rd, 2003 at 04:23 AM.
Ben Chin
-
Jul 26th, 2001, 04:06 AM
#2
Try this:
VB Code:
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
Const WM_CLOSE = &H10
Const WM_DESTROY = &H2
Private Sub Command1_Click()
hWin = FindWindow("notepad", vbNullString)
If hWin <> 0 Then
PostMessage hWin, WM_CLOSE, 0, 0
PostMessage hWin, WM_DESTROY, 0, 0
End If
End Sub
-
Jul 26th, 2001, 09:10 AM
#3
You should also post the WM_QUIT message as well.
-
Jul 26th, 2001, 09:31 AM
#4
Member
Re: Close Application
Originally posted by benchin
I called other application from my program, say Notepad, using ShellExecute function (winapi). I got the window (Notepad) handle from FindWindow function(winapi). But after that, I don't know how to close it. I couldn't use DestroyWindow because the application (Notepad) doesn't belong to my program. Can anyone suggest a way to close this application (Notepad)? Thanks a lot!!!
If you don't need to exit cleanly, you can kill the process using TerminateProcess.
-
Jul 26th, 2001, 10:46 AM
#5
TerminateProcess should usually be used as a last resort. You should call ExitProcess first, and if that doesn't work, then use TerminateProcess.
-
Jul 27th, 2001, 05:42 AM
#6
Originally posted by Megatron
TerminateProcess should usually be used as a last resort. You should call ExitProcess first, and if that doesn't work, then use TerminateProcess.
ExitProcess can only be used to close your own program, not another program. People should really start testing out their own suggestions..
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|