clicking button in external app

  • Thread starter Thread starter blah
  • Start date Start date
B

blah

Hello everyone, my program is supposed to click a button on another
application when I click a button on my application. If I use a spy
program to get its identity, how can I do this? I know I should use
winapi and the interop services, but I cant quite set the code up to
work. Ive managed to get the window focus so far with winapi, so all I
need to do is execute a specific button. any know how to do this when I
click the button on my program?
 
Hello, blah!

b> Hello everyone, my program is supposed to click a button on another
b> application when I click a button on my application. If I use a spy
b> program to get its identity, how can I do this? I know I should use
b> winapi and the interop services, but I cant quite set the code up to
b> work. Ive managed to get the window focus so far with winapi, so all I
b> need to do is execute a specific button. any know how to do this when I
b> click the button on my program?

You have to get the window handle of the button, and then call SendMessage on this handle with
BM_CLICK message.

You can use Spy++ took to get window name of the button.

--
Regards, Vadym Stetsyak
www: http://vadmyst.blogspot.com
 
blah,

What you want to do is to define the SendMessage API function and send
the BM_CLICK message to the button (assuming you have the handle to it).

Hope this helps.
 
Thank you for the replies, Im not at my computer right now but would
the code by any chance look something like this (upon button click):
{
HWND w = FindWindow("Window Class", NULL);
{
SendMessage(w, WM_LBUTTONDOWN, 0, 0);
}
}
unfortunately I cant try it right now, but since it looks so simple Im
guessing its wrong anyway?
 
blah,

Yes, it will look like that, but you have to convert it to .NET.

Also, passing WM_LBUTTONDOWN is INCORRECT. You need to pass BM_CLICK.
This will correctly simulate a click on the button (the button down event,
the click event, the button up event, etc, etc).
 
Oh yes, BM_CLICK!, I was thinking too much into it. I will give it a
try when I can. Thanks for the assistance!
 
How does find window work if the button is in a differnt application? What
if the app is currntly showing mulitple buttons? How do you know you got the
right one?

Also SendMessage, I'm guessing you have to import it using
System.Runtime.InteropServices?

I ask as I have a whole set of routines that I DLL Imported a bunch of APIs
for doing this, EnumWindows, EnumChildern, Send/Post Message, and defined a
bunch of constants for the messages.

Thanks
Wayne Sepega



Nicholas Paldino said:
blah,

Yes, it will look like that, but you have to convert it to .NET.

Also, passing WM_LBUTTONDOWN is INCORRECT. You need to pass BM_CLICK.
This will correctly simulate a click on the button (the button down event,
the click event, the button up event, etc, etc).


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

blah said:
Thank you for the replies, Im not at my computer right now but would
the code by any chance look something like this (upon button click):
{
HWND w = FindWindow("Window Class", NULL);
{
SendMessage(w, WM_LBUTTONDOWN, 0, 0);
}
}
unfortunately I cant try it right now, but since it looks so simple Im
guessing its wrong anyway?
 
Wayne,
How does find window work if the button is in a differnt application? What
if the app is currntly showing mulitple buttons? How do you know you got
the right one?

That's up to you to figure out. You can traverse the handle tree and
try to filter it out yourself, or use FindWindow. However, it's not easy to
do, and you have to be very sure of which window you are trying to
communicate to (which is a reason why as a cross process mechanism, this
isn't the best, but sometimes, it is the only way).
Also SendMessage, I'm guessing you have to import it using
System.Runtime.InteropServices?

Yes, the attributes defined in there will help you define SendMessage
for use in C# code.
I ask as I have a whole set of routines that I DLL Imported a bunch of
APIs for doing this, EnumWindows, EnumChildern, Send/Post Message, and
defined a bunch of constants for the messages.

You might want to check http://www.pinvoke.net, as it will have a good
number of definitions (constants, functions and structs) there as well.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)
Thanks
Wayne Sepega



Nicholas Paldino said:
blah,

Yes, it will look like that, but you have to convert it to .NET.

Also, passing WM_LBUTTONDOWN is INCORRECT. You need to pass BM_CLICK.
This will correctly simulate a click on the button (the button down
event, the click event, the button up event, etc, etc).


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

blah said:
Thank you for the replies, Im not at my computer right now but would
the code by any chance look something like this (upon button click):
{
HWND w = FindWindow("Window Class", NULL);
{
SendMessage(w, WM_LBUTTONDOWN, 0, 0);
}
}
unfortunately I cant try it right now, but since it looks so simple Im
guessing its wrong anyway?
 
Back
Top