communicating netween applications

B

Bill Brehm

Can someone tell my if this is possible and how to go about it?

I want to write an application that can press buttons and read and write
edit controls and navigate through menus, etc. on another application. I
have no source code for the other application, so cannot modify it.
Basically, I have to add some functionality to what that application does,
so I thought of writing an additional application which basically can remote
control the other one by manipulating its controls just like a user would.

I see spy++ can give me handle information about controls, but I doubt they
would be the same everytime the other app and my app are run. I found
ResourceHacker that lets me know get the ID numbers of dialogs and controls.
I would even be willing to make the mouse move to the correct X, Y
coordinates of the other app. I could create a list of coordinates of the
things important to me for this purpose. If the other app changes because of
an upgrade, I'm willing to change mine. But if the other app doesn't change,
I need whatever I do to work regardless of whether the other app is minimize
or where is it located of if it has focus or not, etc.

If there is any example code to help me understand or implement more quickly
it would be appreciated.

Thanks...
 
E

Eric Schumann

All windows are children of the desktop window. This allows you to find a
window from the desktop, once you have a pointer to a window you can use SDK
functions to get all the children windows. Be careful however as what you
see isn't always what you get, owner draw items or user-defined controls may
be difficult to manipulate/use.

In any case here is a sample program that performs manipulates the
Calculator program using Platform SDK functions and C++:
//Look for the Calculator window
HWND hCalcWnd = FindWindowEx(NULL,NULL,NULL,"Calculator");
if (NULL != hCalcWnd)
{
//Find the #1 button
HWND hChild = GetWindow(hCalcWnd,GW_CHILD);
HWND hbtnOne = NULL;
HWND hbtnPlus = NULL;
/*
* Buttons on the calculator are owner draw so we can't find the button
* based on the caption. Spy++ was used to get the control Ids.
*/
UINT uibtnOne = 125;
UINT uibtnPlus = 92;
UINT uiId = 0;
while(NULL != hChild)
{
uiId = GetDlgCtrlID(hChild);
if (uibtnOne == uiId)
{
hbtnOne = hChild;

}
else if (uibtnPlus == uiId)
{
hbtnPlus = hChild;
}
hChild = GetNextWindow(hChild,GW_HWNDNEXT);
}
//Ensure both buttons where found
if ((NULL != hbtnOne) && (NULL != hbtnPlus))
{
//Perform some math

SendMessage(hCalcWnd,WM_COMMAND,MAKEWPARAM(uibtnOne,BN_CLICKED),(LPARAM)hbtn
One);

SendMessage(hCalcWnd,WM_COMMAND,MAKEWPARAM(uibtnPlus,BN_CLICKED),(LPARAM)hbt
nPlus);

SendMessage(hCalcWnd,WM_COMMAND,MAKEWPARAM(uibtnOne,BN_CLICKED),(LPARAM)hbtn
One);
//Perform the equal by sending a keystrok
PostMessage(hCalcWnd,WM_KEYDOWN,VK_RETURN,1);//0x11c0001);
PostMessage(hCalcWnd,WM_KEYUP,VK_RETURN,1);//0xc11c0001);
//Now copy the results to the clipboard using the menu.
HMENU hMenu = GetMenu(hCalcWnd);
HMENU hEdit = GetSubMenu(hMenu,0);
UINT uiCopy = GetMenuItemID(hEdit,0);
SendMessage(hCalcWnd,WM_COMMAND,MAKEWPARAM(uiCopy,0),NULL);
}
}
 
B

Bill Brehm

Eric, thanks for your reply. While waiting for a reply, I did some heavy
experimentation with some tips from a friend. I got it to work similar to
your way, except I use MFC calls. So mine is more CWnd* based than HWND. It
leads me to one more question. Will my MFC based way of search for controls,
by text caption, work for programs that are not written with MFC or C++?

Thanks,

Bill
 
E

Eric Schumann

It should, most programs use the standard controls provided by Windows. For
example an edit control is a standard windows control, MFC provides the
class CEdit which is just simplifies the interaction with the windows
control. Similarly a CWnd is just an interface to a generic Windows window.
So the language used to write the programming language isn't important, as
long as the windows are based on the standard controls provided by Windows.

-Eric
 

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

Top