PDA

Click to See Complete Forum and Search --> : shutdown another app?


rippin
Jul 19th, 2001, 05:56 PM
What API function would I need to use in order to shutdown another application if I have a handle to that application's main window? Basically, I have an application that (upon startup) checks for a newer version of itself (on a network drive) and if it finds a newer version it launches another application to perform the file copy. This other application needs to terminate the calling app. Once the 'update' app launches, the first thing it does is get a handle to the calling app's main form, but now what do I do with it? I've tried DestroyWindow(), CloseWindow(), and even SendMessage (with the WM_CLOSE command) but for some reason, it doesn't seem to be closing correctly. Any ideas?

Matthew Gates
Jul 19th, 2001, 08:24 PM
Try the PostMessage API function.


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("Class", "Caption)
If hWin <> 0 Then
PostMessage hWin, WM_CLOSE, 0, 0
PostMessage hWin, WM_DESTROY, 0, 0
End If

End Sub

rippin
Jul 19th, 2001, 08:34 PM
Why would PostMessage work if SendMessage didn't? Unless I am incorrect, they do the same thing except PostMessage returns immediately without waiting for the message to be processed and SendMessage waits for the message to be processed before returning.

Matthew Gates
Jul 20th, 2001, 01:51 AM
Did you try it? If it's still not working, the application may be frozen, what's your OS version?

rippin
Jul 20th, 2001, 09:55 AM
Actually, I got it to work, the problem wasn't HOW I was closing the Window, but rather WHEN. Apparently, when the other app launches, it has to wait a few seconds before trying to close the window. Thanks for your help.

Matthew Gates
Jul 20th, 2001, 02:27 PM
Glad I could help.

Just incase your wondering about PostMessage and SendMessage:


Originally posted by Megatron
PostMessage and SendMessage are exactly the same (neither is more 'powerful' than the other) except for the fact that PostMessage returns instantly whereas SendMessage does not.