Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Closing app using SendMessage API call 1

Status
Not open for further replies.

moreCA

Programmer
Feb 11, 2003
68
US
I use the statement RetVal = SendMessage(CurrApp, WM_CLOSE, 0, 0) to close an app. I also want to be able to close the app. using the 'X'. I have code in the QueryUnload event that I only want to run when they click the 'X' to close the app. so I checkto see if the UnloadMode = 0, but when I use the SendMessage API call to close the app., it also is UnloadMode = 0. How can I change this or use another method of closing the app. so that it's not an UnloadMode = 0.

Thanks,
CA
 
When you send a WM_CLOSE message to a window, it tries to close the window as if the X button were pressed. This causes the UnloadMode to set to 0. You cannot know whether the application was closed externally or by clicking the X button; by looking only at the UnloadMode parameter.

But there is an easy alternative. When you are closing the window externally using WM_CLOSE, you can initialize its 32-bit user data value using the SetWindowLong function before sending the message. In the target application (being closed) you will query this user data using GetWindowLong function and execute your code accordingly.

The user data value is set to 0 by default. You can set it to any non-zero value before sending the WM_CLOSE message.

See how it goes. Below is the code for the application trying to close the window.
___
[tt]
Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long
Private Declare Function SetWindowLong Lib "user32" Alias "SetWindowLongA" (ByVal hwnd As Long, ByVal nIndex As Long, ByVal dwNewLong As Long) As Long
Const WM_CLOSE = &H10
Const GWL_USERDATA = -21
...
'code to close the target window (CurrApp)
'
'Set the user data value of the target window to -1
'(originally 0)
SetWindowLong CurrApp, GWL_USERDATA, -1

'send closing messgae
SendMessage CurrApp, WM_CLOSE, 0, ByVal 0&[/tt]
___

In the QueryUnload event of the target window, you would check the user data value something like this...
___
[tt]
Private Declare Function GetWindowLong Lib "user32" Alias "GetWindowLongA" (ByVal hwnd As Long, ByVal nIndex As Long) As Long
Const GWL_USERDATA = -21

Private Sub Form_QueryUnload(Cancel As Integer, UnloadMode As Integer)
If GetWindowLong(hwnd, GWL_USERDATA) = 0 Then
MsgBox "Closing from X."
Else '(if -1)
MsgBox "Closing externally using WM_CLOSE."
End If
End Sub[/tt]
___

Checked this method on my machine and its works fine.
 
Sorry it took so long to get back to you guys, I couldn't test it until I was back at work and then I had other things that I needed to do. Anyways....SUPERB CODE. The code did it's job, although I had to change the SendMessage API command with a PostMessage API call due to another issue, but that's beside the point. Hypetia's my hero!!! Two stars for your effort and knowledge, or at least your research of someone else's knowledge...guess that's all the same huh.

Thanks,
CA
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top