Results 1 to 6 of 6

Thread: Close Application

  1. #1

    Thread Starter
    Member
    Join Date
    May 2001
    Location
    Malaysia
    Posts
    43

    Wink 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

  2. #2
    Matthew Gates
    Guest
    Try this:

    VB Code:
    1. Private Declare Function FindWindow Lib "user32" _
    2. Alias "FindWindowA" (ByVal lpClassName As String, ByVal _
    3. lpWindowName As String) As Long
    4.  
    5. Private Declare Function PostMessage Lib "user32" _
    6. Alias "PostMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, _
    7. ByVal wParam As Long, ByVal lParam As Long) As Long
    8.  
    9. Const WM_CLOSE = &H10
    10. Const WM_DESTROY = &H2
    11.  
    12. Private Sub Command1_Click()
    13.  
    14.     hWin = FindWindow("notepad", vbNullString)
    15.     If hWin <> 0 Then
    16.         PostMessage hWin, WM_CLOSE, 0, 0
    17.         PostMessage hWin, WM_DESTROY, 0, 0
    18.     End If
    19.    
    20. End Sub

  3. #3
    Megatron
    Guest
    You should also post the WM_QUIT message as well.

  4. #4

    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.

  5. #5
    Megatron
    Guest
    TerminateProcess should usually be used as a last resort. You should call ExitProcess first, and if that doesn't work, then use TerminateProcess.

  6. #6
    Tygur
    Guest
    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
  •  



Click Here to Expand Forum to Full Width