473,883 Members | 3,128 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Call SendMessage API Question

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 "FindWindow A"
(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(B yVal sender As System.Object, ByVal e As
System.EventArg s) Handles

hWnd = FindWindow(vbNu llString, "Form1")

MsgBox("Form1: " & hWnd)

hWndbutton = FindWindowEx(hW nd, 0&, "Button2", vbNullString)

MsgBox("Button2 : " & hWndbutton)

'want to send a Click to button number 2 here

MsgBox(SendMess age(hWndbutton, WM_KEYDOWN, VK_SPACE, 0&) & " " &
SendMessage(hWn dbutton, 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


Nov 21 '05
18 6573
"Lars Netzel" <ui****@adf.s e> wrote in message news:ek******** *****@TK2MSFTNG P15.phx.gbl...
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.

--
Al Reid
Nov 21 '05 #11
"Lars Netzel" <ui****@adf.s e> schrieb:
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.

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://classicvb.org/petition/>

Nov 21 '05 #12
"Lars Netzel" <ui****@adf.s e> schrieb:
Private Declare Auto Function FindWindow Lib "user32" Alias "FindWindow A"
(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(vbNu llString, "FSM Klient")

hWndbutton = FindWindowEx(hW nd, 0&, "Button2", vbNullString)
'0&' => 'IntPtr.Zero'.
SendMessage(hWn dbutton, WM_KEYDOWN, VK_SPACE, 0)

SendMessage(hWn dbutton, WM_KEYUP, VK_SPACE, 0)


--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://classicvb.org/petition/>

Nov 21 '05 #13

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(hW nd,
0&, "Button2", vbNullString)" cannot be converted to an IntPtr.. what should
I set that parameter too?
Private Declare Auto Function FindWindow Lib "user32" Alias "FindWindow A"
(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(vbNu llString, "FSM Klient")

hWndbutton = FindWindowEx(hW nd, 0&, "Button2", vbNullString)

SendMessage(hWn dbutton, WM_KEYDOWN, VK_SPACE, 0)

SendMessage(hWn dbutton, WM_KEYUP, VK_SPACE, 0)

/Lars
"Al Reid" <ar*****@reidDA SHhome.com> wrote in message
news:ec******** ********@TK2MSF TNGP12.phx.gbl. ..
"Lars Netzel" <ui****@adf.s e> wrote in message
news:ek******** *****@TK2MSFTNG P15.phx.gbl...
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.

--
Al Reid

Nov 21 '05 #14
Pass an IntPtr rather than an Integer. One way of doing it is shown inline.

"Lars Netzel" <ui****@adf.s e> wrote in message news:Or******** ******@TK2MSFTN GP14.phx.gbl...

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(hW nd,
0&, "Button2", vbNullString)" cannot be converted to an IntPtr.. what should
I set that parameter too?
Private Declare Auto Function FindWindow Lib "user32" Alias "FindWindow A"
(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(vbNu llString, "FSM Klient")

////
hWndbutton = FindWindowEx(hW nd, New IntPtr(0), "Button2", vbNullString)<< <

\\\\

SendMessage(hWn dbutton, WM_KEYDOWN, VK_SPACE, 0)

SendMessage(hWn dbutton, WM_KEYUP, VK_SPACE, 0)

/Lars

Nov 21 '05 #15
"Lars Netzel" <ui****@adf.s e> schrieb:
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(hW nd, 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 "FindWindow A"
(ByVal lpClassName As String, ByVal lpWindowName As String) As IntPtr
Remove the 'Alias "FindWindow A"'. I posted the correct declares recently,
you only have to copy them.
hWndbutton = FindWindowEx(hW nd, 0&, "Button2", vbNullString)


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

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://classicvb.org/petition/>

Nov 21 '05 #16
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(vbNu llString, "FSM Klient")

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

MsgBox(hWndbutt on.ToString)

SendMessage(hWn dbutton, WM_KEYDOWN, VK_SPACE, 0)

SendMessage(hWn dbutton, WM_KEYUP, VK_SPACE, 0)

/Lars
Nov 21 '05 #17
"Lars Netzel" <ui****@adf.s e> schrieb:
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...
[...]
hWndbutton = FindWindowEx(hW nd, IntPtr.Zero, "Button2", vbNullString)


I already said that you'll have to pass the window's Win32 control class
name (this is not the same as the .NET class name!) instead of "Button2",
and the caption of the button in the last parameter. You can obtain the
Win32 control class name of the button by using the Spy++ tool to pick the
control of the running application.

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://classicvb.org/petition/>

Nov 21 '05 #18
Hi!

Thank you, yes I had missed that in your other thread.. .it is now Solved I
have managed to click a button in another Window thru this code.. and I'm
very grateful..

I tested "clicking" a 3 in the Calculator too and that worked.. whohoooo:)

by the Way I used Winspector to look up the Class names and stuff.. couldn't
find SPy ++ fast enough.

Very happy!

best regards
/Lars Netzel

"Herfried K. Wagner [MVP]" <hi************ ***@gmx.at> wrote in message
news:e%******** ********@TK2MSF TNGP09.phx.gbl. ..
"Lars Netzel" <ui****@adf.s e> schrieb:
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...
[...]
hWndbutton = FindWindowEx(hW nd, IntPtr.Zero, "Button2", vbNullString)


I already said that you'll have to pass the window's Win32 control class
name (this is not the same as the .NET class name!) instead of "Button2",
and the caption of the button in the last parameter. You can obtain the
Win32 control class name of the button by using the Spy++ tool to pick the
control of the running application.

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://classicvb.org/petition/>

Nov 21 '05 #19

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
15654
by: Nidhi | last post by:
Hi, I am trying to write code to retrieve the small icon for an application in C#. I am using SendMessage with WM_GETICON for this purpose but the return value I get is 0. I don't understand this. Is that expected? What am I doing wrong? Any help would be appreciated. Here is the snippet of code that I am using - public static extern int SendMessage(int
2
2432
by: Sonia Igla | last post by:
Hi. I need to perform DBLCLK on TreeView Node. I try the following: private static extern int SendMessage(IntPtr hWnd, uint msg, UInt32 wParam, UInt32 lParam); private const UInt32 WM_LBUTTONDBLCLK = 0x203; pTreeNode.TreeView.SelectedNode = pTreeNode;
5
5863
by: Adam Clauss | last post by:
I am attempting to set the text on a richedit control in another application using EM_SETTEXTEX: http://msdn.microsoft.com/library/default.asp?url=/library/en-us/shellcc/platform/commctls/richedit/richeditcontrols/richeditcontrolreference/richeditmessages/em_settextex.asp I have the following: public static extern int SendMessage(IntPtr hWnd, uint Msg, int wParam, int lParam);
3
10169
by: JSK | last post by:
Hi, As any one worked in VB.NET and made use of Sendmessage API? The Issue I am running into is how to pass pointers of Data Structures (UDT) to the SendMessage. I starting looking at "IntPrt" as a data type but it appears to fall short of using pointer types. Any help or insight would be greatly appreciated. Thank You
2
1485
by: Abubakar | last post by:
Hi, in my application, different threads send notifications to GUI thread through sendmessage api. They have special integer numbers inside the wParam arg of sendmessage which the gui thread uses to display messages in the textboxes etc. My question is that in case the threads send messages very fast (which does NOT always happen in my app), faster than the gui thread is processing them, are the messages qued automatically (btw the gui...
11
48093
by: NVergunst | last post by:
Hello everyone. I have what I believe to be a simple question, yet I can not find anything that is helpful to me. Basically I have an application, that I want to use to control external applications. My program will run either embedded within another program or minimized to the system tray. So SendKeys is out because it only works if the controlled application has focus, which it will not. So my conclusion is the windows API. I have...
1
13426
by: Necromis | last post by:
Ok, I have gotten my head around things better regarding SendMessage and FindWindow functions. However, I am running into an issue with my code still. The program I am working with is EXTRA! by Attachmate. It is a mainframe terminal. Here is the issue and strange part of it. It is accepting sendmessage when I use the WM_KEYDOWN/UP commands. However, when I use WM_SETTEXT to send my string it is not imputing the string to the session...
9
4720
by: Neil | last post by:
Anyone have any idea why SendMessage ctl.hwnd, WM_PASTE, 0, 0 isn't working from a regular Access text box (form is opened normally, not in dialog more)? I realize I can just use docmd.RunCommand acCmdPaste. But I have a reason for needing to do it this way. When I run the code I get the message, "Object doesn't support this property or method" (error 438). Any ideas why this is happening? Thanks!
8
1721
by: Dr1ZZ | last post by:
Hi, I'm currently working on a bot for a game. It works like this: 1: Take a picture of the current playing field 2: Do the calculations on what i gotta do (best move) 3: Use sendmessage to send a mouseclick to the game at the correct position
0
9944
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9797
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
11154
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10762
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9586
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7977
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
7136
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
6005
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
4228
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.