Sending Windows Messages

I am playing around trying to send messages to windows (specifically close messages and specific messages to dialog boxes).

I am using visual studio 2003, and am using the windows API to try to send the messages.

What I have found is that FlashWindow and CloseWindow work, MoveWindow kind of works (it does respond, but does some weird things), but SendMessage and DestroyWindow do nothing.

Following is the code I have been tinkering with, does anyone have any advice (eg. are there any .NET framework classes which can help me here):

Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
Public Declare Function FlashWindow Lib "user32" (ByVal hwnd As Long, ByVal bInvert As Long) As Long
Public Declare Function DestroyWindow Lib "user32" (ByVal hwnd As Long) As Long
Public Declare Function CloseWindow Lib "user32" (ByVal hwnd As Long) As Long
Public Declare Function MoveWindow Lib "user32" (ByVal hwnd As Long, ByVal x As Int16, ByVal y As Int16, ByVal width As Int16, ByVal height As Int16, ByVal repaint As Boolean) As Boolean

Const WM_CLOSE = &H10
Const WM_SHOWWINDOW = &H18

Dim winHwnd As Long
Dim RetVal As Long
winHwnd = FindWindow(Nothing, "Calculator")
FlashWindow(winHwnd, 1)
If winHwnd <> 0 Then
'MoveWindow(winHwnd, 50, 50, 100, 100, True)
SendMessage(winHwnd, WM_SHOWWINDOW, 0&, 0&Wink
DestroyWindow(winHwnd)
Else
MsgBox("The Calculator is not open.")
End If

[1801 byte] By [Roose] at [2008-2-12]
# 1

Hi Roose,

Try adding a call to Application.DoEvents() function after you have called SendMessage. It results in the processing of all Windows messages in the queue.

Regards,
Vikram
http://dotnetupdate.blogspot.com

Vikram at 2007-8-21 > top of Msdn Tech,.NET Development,.NET Base Class Library...
# 2
Cheers Vikram,

But no luck I'm afraid. Does that code work for you?

Roose

Roose at 2007-8-21 > top of Msdn Tech,.NET Development,.NET Base Class Library...
# 3
From MSDN:
"A thread cannot use DestroyWindow to destroy a window created by a different thread. "

But you can send it a WM_CLOSE, which typically works fine.

I notice that you are using Long for all the window handles. This is only right if you happen to be running 64-bit Windows, otherwise your parameters will be askew. That's probably why MoveWindow "does some weird things" for you. Instead of Long, use IntPtr.

anfortas at 2007-8-21 > top of Msdn Tech,.NET Development,.NET Base Class Library...
# 4
turns out the problem may have been the Long thing. In the mean time I have created a dll in C which handles the API calls and in the process discovered that that was the problem (I need to use Integer though, not IntPtr).

I imagine it would work directly now but for several reasons I am sticking with using the dll.

Roose at 2007-8-21 > top of Msdn Tech,.NET Development,.NET Base Class Library...

.NET Development

Site Classified