Call SendMessage API Question

L

Lars Netzel

Hello!

Thanx to this newgroup I have finally, with the help of you guys, gotten
this to work halfway.. but the final action is still not working, clicking
the "Button2" thru SendMessage().


Private Declare Auto Function FindWindow Lib "user32" Alias "FindWindowA"
(ByVal lpClassName As String, ByVal lpWindowName As String) As Integer
Private Declare Auto Function FindWindowEx Lib "user32" (ByVal hWnd1 As
Long, ByVal hWnd2 As Long, ByVal lpsz1 As String, ByVal lpsz2 As String) As
Long

Private Declare Auto Function SendMessage Lib "user32" (ByVal hwnd As Long,
ByVal wMsg As Long, ByVal wParam As Long, ByVal lParam As Object) As Long

Private Const WM_KEYDOWN = &H100

Private Const WM_KEYUP = &H101

Private Const VK_SPACE = &H20



Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles

hWnd = FindWindow(vbNullString, "Form1")

MsgBox("Form1: " & hWnd)

hWndbutton = FindWindowEx(hWnd, 0&, "Button2", vbNullString)

MsgBox("Button2: " & hWndbutton)

'want to send a Click to button number 2 here

MsgBox(SendMessage(hWndbutton, WM_KEYDOWN, VK_SPACE, 0&) & " " &
SendMessage(hWndbutton, WM_KEYUP, VK_SPACE, 0&))

End Sub



I get a long number from both the SendMEssage Calls.. But the Button2 is not
clicked...

Best Regards/

Lars
 
H

Herfried K. Wagner [MVP]

Lars Netzel said:
Thanx to this newgroup I have finally, with the help of you guys, gotten
this to work halfway.. but the final action is still not working, clicking
the "Button2" thru SendMessage().


Private Declare Auto Function FindWindow Lib "user32" Alias "FindWindowA"
(ByVal lpClassName As String, ByVal lpWindowName As String) As Integer
Private Declare Auto Function FindWindowEx Lib "user32" (ByVal hWnd1 As
Long, ByVal hWnd2 As Long, ByVal lpsz1 As String, ByVal lpsz2 As String)
As Long

Private Declare Auto Function SendMessage Lib "user32" (ByVal hwnd As
Long, ByVal wMsg As Long, ByVal wParam As Long, ByVal lParam As Object) As
Long

The declaration above is wrong. Use this one instead:

\\\
Private Declare Auto Function SendMessage Lib "user32.dll" ( _
ByVal hwnd As IntPtr, _
ByVal wMsg As Int32, _
ByVal wParam As Int32, _
ByVal lParam As Int32 _
) As Int32
///

Note that 'Long' maps to 'Int64', so declares for VB.NET and VB6 cannot be
interchanged.
Private Const WM_KEYDOWN = &H100

Private Const WM_KEYUP = &H101

Private Const VK_SPACE = &H20

=> 'Private Const ... As Int32 = ...'.
MsgBox(SendMessage(hWndbutton, WM_KEYDOWN, VK_SPACE, 0&) & " " &
SendMessage(hWndbutton, WM_KEYUP, VK_SPACE, 0&))

'0&' => '0'.
 
L

Lars Netzel

Yes it it would be, in the same application but I'm trying to press a button
in another applications window...

/Lars
 
L

Lars Netzel

Hi, thank you for your reply unfortunately I get an error and can't compile
now..

Since I'm using this declaration now where the first parameter is an IntPtr
instead of a long... I can't call the SendMessage() the same way anymore. I
mean, the hWndbutton variable is a Long... that's what I get back from
FindWindowEx(). How to I get this to be an IntPtr instead?

'My New declaration (as you wrote it)
Private Declare Auto Function SendMessage Lib "user32.dll" (ByVal hwnd As
IntPtr, ByVal wMsg As Int32, ByVal wParam As Int32, ByVal lParam As Int32)
As Int32

'My old call but with 0 instead of 0& in the last parameter.
MsgBox(SendMessage(hWndbutton, WM_KEYDOWN, VK_SPACE, 0) & " " &
SendMessage(hWndbutton, WM_KEYUP, VK_SPACE, 0))
Value of type 'Long' cannot be converted to 'System.IntPtr'. (hWndbutton is
a Long)

Please help/Lars
 
H

Herfried K. Wagner [MVP]

Lars Netzel said:
Since I'm using this declaration now where the first parameter is an
IntPtr instead of a long... I can't call the SendMessage() the same way
anymore. I mean, the hWndbutton variable is a Long... that's what I get
back from FindWindowEx(). How to I get this to be an IntPtr instead?

'My New declaration (as you wrote it)
Private Declare Auto Function SendMessage Lib "user32.dll" (ByVal hwnd As
IntPtr, ByVal wMsg As Int32, ByVal wParam As Int32, ByVal lParam As Int32)
As Int32

'My old call but with 0 instead of 0& in the last parameter.
MsgBox(SendMessage(hWndbutton, WM_KEYDOWN, VK_SPACE, 0) & " " &
SendMessage(hWndbutton, WM_KEYUP, VK_SPACE, 0))
Value of type 'Long' cannot be converted to 'System.IntPtr'. (hWndbutton
is a Long)

Declare 'hWndButton' As 'IntPtr'. 'Long' is a 64-bit type and thus not
suitable.
 
A

Al Reid

Lars Netzel said:
Hi, thank you for your reply unfortunately I get an error and can't compile
now..

Since I'm using this declaration now where the first parameter is an IntPtr
instead of a long... I can't call the SendMessage() the same way anymore. I
mean, the hWndbutton variable is a Long... that's what I get back from
FindWindowEx(). How to I get this to be an IntPtr instead?

'My New declaration (as you wrote it)
Private Declare Auto Function SendMessage Lib "user32.dll" (ByVal hwnd As
IntPtr, ByVal wMsg As Int32, ByVal wParam As Int32, ByVal lParam As Int32)
As Int32

'My old call but with 0 instead of 0& in the last parameter.
MsgBox(SendMessage(hWndbutton, WM_KEYDOWN, VK_SPACE, 0) & " " &
SendMessage(hWndbutton, WM_KEYUP, VK_SPACE, 0))
Value of type 'Long' cannot be converted to 'System.IntPtr'. (hWndbutton is
a Long)

Please help/Lars

That would indicate that you are using the incorrect data type for the return value from the FindWindowEX call. Since it returns a
HWND which is a DWORD or 32-bit integer, Long is the incorrect .Net data type for the API Declare.

You show your declare as:

Private Declare Auto Function FindWindowEx Lib "user32" (ByVal hWnd1 As
Long, ByVal hWnd2 As Long, ByVal lpsz1 As String, ByVal lpsz2 As String) As
Long

And as it has already been pointed out, Long is the incorrect data type for the hwnd parameters and return value. You need to use
32-bit integer data types for the declares, rather than Longs and you need to be consistent.

Private Declare Auto Function FindWindowEx Lib "user32" (ByVal hWnd1 As
Int32, ByVal hWnd2 As Int32, ByVal lpsz1 As String, ByVal lpsz2 As String) As
Int32


I hope this helps.
 
L

Lars Netzel

Hi

Yeah... I tested that but then of course the FindWindowEx is declared as a
long so.. I get a "Specified Cast Not valid" when trying to assign the
hWndbutton from FindWidowEx()

/Lars
 
L

Lars Netzel

Well I must admit this is a little bit above my normal "areas" in
programming.. but it can't be that hard to get to work...

I have this...
-----------------------------------------------------------
Private Declare Auto Function FindWindow Lib "user32" Alias "FindWindowA"
(ByVal lpClassName As String, ByVal lpWindowName As String) As Long

Private Declare Auto Function FindWindowEx Lib "user32" (ByVal hWnd1 As
Long, ByVal hWnd2 As Long, ByVal lpsz1 As String, ByVal lpsz2 As String) As
Int32

Private Declare Auto Function SendMessage Lib "user32.dll" (ByVal hWnd As
IntPtr, ByVal wMsg As Int32, ByVal wParam As Int32, ByVal lParam As Int32)
As Int32

Private Const WM_KEYDOWN As Int32 = &H100

Private Const WM_KEYUP As Int32 = &H101

Private Const VK_SPACE As Int32 = &H20

Dim hWndbutton As IntPtr

Dim hWnd As Long

hWnd = FindWindow(vbNullString, "FSM Klient")

hWndbutton = FindWindowEx(hWnd, 0&, "Button2", vbNullString)

SendMessage(hWndbutton, WM_KEYDOWN, VK_SPACE, 0)

SendMessage(hWndbutton, WM_KEYUP, VK_SPACE, 0)

------------------------------------------------------------
It underlines the "FindWindowEx(hWnd, 0&, "Button2", vbNullString)" row now
and says "Integer cannot be converted to IntPtr"

/Lars
 
A

Al Reid

Lars Netzel said:
Well I must admit this is a little bit above my normal "areas" in
programming.. but it can't be that hard to get to work...

It really is simple. You just have to use consistent data types.

Change all API parameters and variables that reference Hwnd's to IntPtr or Int32. As you have it now you are using Long, Int32 and
IntPtr for the Hwnd's. Fix that first. I would suggest using IntPtr.

Once you resolve the issues with data types, it should work. Hint: There should be no "Long" data types in the API declares.
 
H

Herfried K. Wagner [MVP]

Lars Netzel said:
Yeah... I tested that but then of course the FindWindowEx is declared as a
long so.. I get a "Specified Cast Not valid" when trying to assign the
hWndbutton from FindWidowEx()

Mhm... I suggest to use the declaration of 'FindWindowEx' I gave you in a
previous thread.
 
H

Herfried K. Wagner [MVP]

Lars Netzel said:
Private Declare Auto Function FindWindow Lib "user32" Alias "FindWindowA"
(ByVal lpClassName As String, ByVal lpWindowName As String) As Long

Private Declare Auto Function FindWindowEx Lib "user32" (ByVal hWnd1 As
Long, ByVal hWnd2 As Long, ByVal lpsz1 As String, ByVal lpsz2 As String)
As Int32

'ByVal hWnd1 As Long, ByVal hWnd2 As Long' => 'ByVal hWnd1 As IntPtr, ByVal
hWnd2 As IntPtr'.

'...) As Int32' => '...) As IntPtr'.
Private Declare Auto Function SendMessage Lib "user32.dll" (ByVal hWnd As
IntPtr, ByVal wMsg As Int32, ByVal wParam As Int32, ByVal lParam As Int32)
As Int32

Private Const WM_KEYDOWN As Int32 = &H100

Private Const WM_KEYUP As Int32 = &H101

Private Const VK_SPACE As Int32 = &H20

Dim hWndbutton As IntPtr

Dim hWnd As Long

'As Long' => 'As IntPtr'.
hWnd = FindWindow(vbNullString, "FSM Klient")

hWndbutton = FindWindowEx(hWnd, 0&, "Button2", vbNullString)

'0&' => 'IntPtr.Zero'.
 
L

Lars Netzel

Okay.. I'm getting more and more confused... I have no Longs what so ever
now...
...and Can't compile cause the "0&" on row "hWndbutton = FindWindowEx(hWnd,
0&, "Button2", vbNullString)" cannot be converted to an IntPtr.. what should
I set that parameter too?
Private Declare Auto Function FindWindow Lib "user32" Alias "FindWindowA"
(ByVal lpClassName As String, ByVal lpWindowName As String) As IntPtr

Private Declare Auto Function FindWindowEx Lib "user32" (ByVal hWnd1 As
IntPtr, ByVal hWnd2 As IntPtr, ByVal lpsz1 As String, ByVal lpsz2 As String)
As IntPtr

Private Declare Auto Function SendMessage Lib "user32.dll" (ByVal hWnd As
IntPtr, ByVal wMsg As Int32, ByVal wParam As Int32, ByVal lParam As Int32)
As IntPtr

Private Const WM_KEYDOWN As Int32 = &H100

Private Const WM_KEYUP As Int32 = &H101

Private Const VK_SPACE As Int32 = &H20

Dim hWndbutton As IntPtr

Dim hWnd As IntPtr

hWnd = FindWindow(vbNullString, "FSM Klient")

hWndbutton = FindWindowEx(hWnd, 0&, "Button2", vbNullString)

SendMessage(hWndbutton, WM_KEYDOWN, VK_SPACE, 0)

SendMessage(hWndbutton, WM_KEYUP, VK_SPACE, 0)



/Lars
 
H

Herfried K. Wagner [MVP]

Lars Netzel said:
Okay.. I'm getting more and more confused... I have no Longs what so ever
now...
..and Can't compile cause the "0&" on row "hWndbutton =
FindWindowEx(hWnd, 0&, "Button2", vbNullString)" cannot be converted to an
IntPtr.. what should I set that parameter too?

'0' is treated as 'Int32', '0&' as 'Long'. Pass 'IntPtr.Zero' instead of
'0&'.
Private Declare Auto Function FindWindow Lib "user32" Alias "FindWindowA"
(ByVal lpClassName As String, ByVal lpWindowName As String) As IntPtr

Remove the 'Alias "FindWindowA"'. I posted the correct declares recently,
you only have to copy them.
hWndbutton = FindWindowEx(hWnd, 0&, "Button2", vbNullString)

'0&' => 'IntPtr.Zero'.
 
L

Lars Netzel

It still doesn't work... now I can compile but I get a 0 value back from the
FindWindowEx(). The FindWindow() return a value though...

Private Declare Auto Function FindWindow Lib "user32" (ByVal lpClassName As
String, ByVal lpWindowName As String) As IntPtr
Private Declare Auto Function FindWindowEx Lib "user32" (ByVal hWnd1 As
IntPtr, ByVal hWnd2 As IntPtr, ByVal lpsz1 As String, ByVal lpsz2 As String)
As IntPtr

Private Declare Auto Function SendMessage Lib "user32.dll" (ByVal hWnd As
IntPtr, ByVal wMsg As Int32, ByVal wParam As Int32, ByVal lParam As Int32)
As IntPtr

Private Const WM_KEYDOWN As Int32 = &H100

Private Const WM_KEYUP As Int32 = &H101

Private Const VK_SPACE As Int32 = &H20

Dim hWndbutton As IntPtr

Dim hWnd As IntPtr

hWnd = FindWindow(vbNullString, "FSM Klient")

hWndbutton = FindWindowEx(hWnd, IntPtr.Zero, "Button2", vbNullString)

MsgBox(hWndbutton.ToString)

SendMessage(hWndbutton, WM_KEYDOWN, VK_SPACE, 0)

SendMessage(hWndbutton, WM_KEYUP, VK_SPACE, 0)

/Lars
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Top