Click to See Complete Forum and Search --> : close application using PostMessage


danclemson
December 19th, 2002, 01:20 PM
Hi, All
I am trying to use the following VB .Net code (originated from VB code) to close applicaiton. The applicaiton is not closing.

Please let me know what is the problem.

Thanks /dan

'*************************************************
Module Module1
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

Sub Main()
Dim winHwnd As Long
Dim RetVal As Long
winHwnd = FindWindow(vbNullString, "Calculator")
If winHwnd <> 0 Then
PostMessage(winHwnd, WM_CLOSE, 0&, 0&)
MsgBox(winHwnd)
Else
MsgBox("The Calculator is not open.")
End If
End Sub

End Module

DSJ
December 19th, 2002, 02:36 PM
First think I notice is that all your "As Long"s need to be changed to "As Integer"...

TheCPUWizard
December 19th, 2002, 02:54 PM
Actually The First thing I notices was that DSJ is wrong.

VB Long = C++ int = 32 bits
VB Int = C++ short = 16 bits.

I am assuming that the MsgBox which simply displays a number is appearing....

Is the calculator program the same one as was used with the VB6 code? Could there be any reason why the calculator acan NOT close?

Finally, do the first thing that should be done and check the return code from PostMessage (use GetLast Error as needed).

It is poor practice to not check the return value of every API call. Check 999 out of 1000 and that will be the one that fails!

DSJ
December 20th, 2002, 08:37 AM
I beg to differ... unless Mircosoft sent me a bad help file. Mine plainly states in the "Integer Data Type Changes" section:

VB 6 Integer = 16 bits
VB 6 Long = 32 bits

.Net Short = 16 bits
.Net Integer = 32 bits
.Net Long = 64 bits

The declarations given are correct for VB6 but NOT .NET!!!

TheCPUWizard
December 20th, 2002, 09:21 AM
aargh, a thousand lashes!!!!!

I did not notice that this was the .NET forum!!!!!!


My post applied to the differences between Vc6 and vB6.

danclemson
December 20th, 2002, 03:12 PM
DSJ,

Thanks, and it's working now./dan