Thread View: borland.public.cppbuilder.nativeapi
4 messages
4 total messages
Started by Jeff
Fri, 02 Jun 2006 20:49
Click another app's button
Author: Jeff
Date: Fri, 02 Jun 2006 20:49
Date: Fri, 02 Jun 2006 20:49
41 lines
2423 bytes
2423 bytes
I'm trying to search through another application's client area and click any
"Cancel" or "Close" buttons found in an attempt to safely close the
application. I can't seem to find a way to click another application's
buttons, or find them for that matter. I tried getting the child handles
(see code below) and getting the text using GetWindowText() but Microsoft
states, "However, GetWindowText cannot retrieve the text of a control in
another application." How do I do this?
void TfrmMain::CancelDialogs(HWND hParentWin)
{
HWND hChild; //Child window handle
char *ptr_chrText = ""; //Control text
POINT pPoint; //Coordinate structure
WINDOWINFO WinInfo; //Window information structure
WinInfo.cbSize = sizeof(WINDOWINFO);
if (!GetWindowInfo(hParentWin, &WinInfo)); //Get the dimensions of the
window
{
for (pPoint.x = WinInfo.rcClient.left; pPoint.x <= WinInfo.rcClient.right;
pPoint.x = pPoint.x + 1)
{
for (pPoint.y = WinInfo.rcClient.top; pPoint.y <=
WinInfo.rcClient.bottom; pPoint.y = pPoint.y + 1)
{
hChild = ChildWindowFromPoint(hParentWin, pPoint); //Get a handle to any
child window
if (hChild != NULL)
{
GetWindowText(hChild, ptr_chrText, GetWindowTextLength(hChild) + 1);
//Get the text of the control
if ((StrComp(ptr_chrText, CANCEL_BTN) == 0) || (StrComp(ptr_chrText,
CLOSE_BTN) == 0))
Application->MessageBox(ptr_chrText, "Test", MB_OK);
//SendMessage(hChild, WM_CLICK, NULL, NULL); //Click the Cancel or
Close button
}
}
}
}
}
Re: Click another app's button
Author: Remy Lebeau (TeamB)
Date: Fri, 02 Jun 2006 22:22
Date: Fri, 02 Jun 2006 22:22
49 lines
2803 bytes
2803 bytes
"Jeff" <Jeff@nospam.com> wrote in message news:4480a43a$1@newsgroups.borland.com... > I'm trying to search through another application's client area and click > any "Cancel" or "Close" buttons found in an attempt to safely close > the application. Just send WM_CLOSE and/or WM_QUIT messages to the main windows instead. You don't need to deal with the buttons just to close the application. > I can't seem to find a way to click another application's buttons You have to send a WM_COMMAND message to the button's parent window, specifying BN_CLICKED. Or a BM_CLICK message to the button itself. > or find them for that matter. That is because you are not looking correctly. > I tried ... getting the text using GetWindowText() but Microsoft states, > "However, GetWindowText cannot retrieve the text of a control in > another application." How do I do this? That is correct. GetWindowText() needs you to pass it a memory address to a buffer that will receive the text. A memory address in one application is not a valid memory address in another application. You would have to write a custom DLL and then use DLL injection, such as with a message hook via SetWindowHookEx(), to force the DLL's code to run in the context of the other application. Then the DLL can use GetWindowText() and copy the text back to the application, such as with WM_COPYDATA. > char *ptr_chrText = ""; //Control text That won't work. You are not allocating a memory buffer to receive the text. You are pointing to a string constant that is located in the application's read-only memory. > POINT pPoint; //Coordinate structure > WINDOWINFO WinInfo; //Window information structure > WinInfo.cbSize = sizeof(WINDOWINFO); > > if (!GetWindowInfo(hParentWin, &WinInfo)); //Get the dimensions of the > window Get rid of all of that. Use GetWindow() or EnumChildWindows() instead. Gambit
Re: Click another app's button
Author: Remy Lebeau (TeamB)
Date: Fri, 02 Jun 2006 22:44
Date: Fri, 02 Jun 2006 22:44
17 lines
1493 bytes
1493 bytes
"Jeff" <Jeff@nospam.com> wrote in message news:4480bfa4@newsgroups.borland.com... > What happens is that if the application I'm trying to close has a > dialog box (modal window) open, then Windows thinks it was > terminated incorrectly when I send it the WM_CLOSE message. So? You are closing the application down anyway. > I was just thinking I could close all the dialogs and then send the > WM_CLOSE to the parent window to close it gracefully. Dialogs usually respond to the WM_QUIT message, if not WM_CLOSE. Gambit
Re: Click another app's button
Author: Jeff
Date: Fri, 02 Jun 2006 22:45
Date: Fri, 02 Jun 2006 22:45
65 lines
3469 bytes
3469 bytes
What happens is that if the application I'm trying to close has a dialog box (modal window) open, then Windows thinks it was terminated incorrectly when I send it the WM_CLOSE message. I was just thinking I could close all the dialogs and then send the WM_CLOSE to the parent window to close it gracefully. I'll have to investigate your suggestions below, thanks a lot. "Remy Lebeau (TeamB)" <no.spam@no.spam.com> wrote in message news:4480bc1c$1@newsgroups.borland.com... > > "Jeff" <Jeff@nospam.com> wrote in message > news:4480a43a$1@newsgroups.borland.com... > >> I'm trying to search through another application's client area and click >> any "Cancel" or "Close" buttons found in an attempt to safely close >> the application. > > Just send WM_CLOSE and/or WM_QUIT messages to the main windows instead. > You > don't need to deal with the buttons just to close the application. > >> I can't seem to find a way to click another application's buttons > > You have to send a WM_COMMAND message to the button's parent window, > specifying BN_CLICKED. Or a BM_CLICK message to the button itself. > >> or find them for that matter. > > That is because you are not looking correctly. > >> I tried ... getting the text using GetWindowText() but Microsoft states, >> "However, GetWindowText cannot retrieve the text of a control in >> another application." How do I do this? > > That is correct. GetWindowText() needs you to pass it a memory address to > a > buffer that will receive the text. A memory address in one application is > not a valid memory address in another application. You would have to > write > a custom DLL and then use DLL injection, such as with a message hook via > SetWindowHookEx(), to force the DLL's code to run in the context of the > other application. Then the DLL can use GetWindowText() and copy the text > back to the application, such as with WM_COPYDATA. > >> char *ptr_chrText = ""; //Control text > > That won't work. You are not allocating a memory buffer to receive the > text. You are pointing to a string constant that is located in the > application's read-only memory. > >> POINT pPoint; //Coordinate structure >> WINDOWINFO WinInfo; //Window information structure >> WinInfo.cbSize = sizeof(WINDOWINFO); >> >> if (!GetWindowInfo(hParentWin, &WinInfo)); //Get the dimensions of the >> window > > Get rid of all of that. Use GetWindow() or EnumChildWindows() instead. > > > Gambit > >
Thread Navigation
This is a paginated view of messages in the thread with full content displayed inline.
Messages are displayed in chronological order, with the original post highlighted in green.
Use pagination controls to navigate through all messages in large threads.
Back to All Threads