Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Programmatically press a button on another application (C, Windows)

I'm trying to use the following code to press a button on my other application:

HWND ButtonHandle;
if( (wnd = FindWindow(0, "Do you want to save?")) )
{   
   ButtonHandle = FindWindowEx(wnd, 0, "SaveButton", "&Save");
   SendMessage(wnd, WM_COMMAND, MAKEWORD(GetDlgCtrlID(ButtonHandle), BN_CLICKED ), (LPARAM)ButtonHandle);

}

It doesn't work. I tried passing different handles to MAKEWORD and to change the WPARM and LPARAM but nothing.

Any ideas on how to click a button on another application's window?

Code is appreciated. Thanks.

EDIT: The reason it doesn't seem to work permissions. I sent a PostMessage() and the result was an error with GetLastError() = 5 (or Access Denied). Any ideas?

EDIT2 I don't mean to be rude but please please please, I already searched all the API's including getting and setting the regions for the button and then sending a button down and button up, getting the control ID, getting the class ID and a zillion more. The reason I asked the question here in the first place is because I already exhausted my search on the internet. If you know the answer PLEASE POST CODE, do not suggest an API and that's it, show me how does that API solves the problem. It's not hard. thank you.

EDIT 3: The question's answer was selected automatically when the bounty finished. The question still remains without an answer.

like image 398
wonderer Avatar asked Aug 03 '09 18:08

wonderer


4 Answers

  1. Are you sure that "SaveButton" class name is valid? Do you get the button handle?
  2. Try to send messages to ButtonHandle window (directly to the button).

Update: I believe this should work,

SendMessage(ButtonHandle, BM_CLICK, 0, 0);
like image 148
Nick Dandoulakis Avatar answered Nov 03 '22 22:11

Nick Dandoulakis


SendMessage(btnHandle, WM_LBUTTONDOWN, 0, 0);
SendMessage(btnHandle, WM_LBUTTONUP, 0, 0);
SendMessage(btnHandle, WM_LBUTTONDOWN, 0, 0);
SendMessage(btnHandle, WM_LBUTTONUP, 0, 0);

You have to send a button click twice. Not sure why (maybe the first click only activates the window of the button), but I'm using this code for a long time and it always worked for me.

like image 31
mwore Avatar answered Nov 03 '22 21:11

mwore


maybe this can help: http://www.cplusplus.com/forum/beginner/8806/

like image 31
fnurglewitz Avatar answered Nov 03 '22 20:11

fnurglewitz


See the following solution, also you can use

SendMessage(ButtonHandle, WM_LBUTTONDOWN, 0, 0);
SendMessage(ButtonHandle, WM_LBUTTONUP, 0, 0);

Or

SendMessage(ButtonHandle, BM_CLICK, 0, 0);

HWND buttonHandle = 0;

BOOL CALLBACK GetButtonHandle(HWND handle, LPARAM)
{
 char label[100];
 int size = GetWindowTextA(handle,label,sizeof(label));
 if(strcmp(label,"&Save") == 0)
 {
  buttonHandle = handle;
  return false;
 }
 return true;
}
void main()
{
 HWND windowHandle = FindWindowA(NULL,"Do you want to Save?");
 if(windowHandle != 0)
 {
  BOOL ret = EnumChildWindows(windowHandle,GetButtonHandle,0);

  if(buttonHandle != 0)
  {
   LRESULT res = SendMessage(buttonHandle,BM_CLICK,0,0);
   //SendMessage(buttonHandle,WM_LBUTTONDOWN,0,0); 
   //SendMessage(buttonHandle,WM_LBUTTONUP,0,0);
  }

 }



}

Note: Getting sure from the window text,button text (check if there is space at the end of the window title)

like image 3
Ahmed Avatar answered Nov 03 '22 21:11

Ahmed