I'm thinking the best way to do it is use FindWindow() to obtain the HWND of the other application, then use GetDlgItem() to obtain the HWND of the text box (edit control) whose parent is the HWND of the other application. Of course, for this to work, one needs the Control ID of the text box in the other application, because that is the 2nd parameter of GetDlgItem(). Not having that, all I could think to do would be to use one of the EnumWindow() functions to enumerate all the HWNDs of the other app.
Anyway, here are two Win32 Api SDK style apps that demonstrate this technique. The first is the app from which we wish to extract the text. Its name and Window Class is "ExtractTextSource". It has two buttons and two text boxes on it. When you click the "Love" button it writes "The World Needs More Love!" to the text box underneath the button. When you click the "Hate" button, the text "The World Needs More Hate!" is copied to the text box underneath it.
The second app's Window Class is "ExtractTextTarget". Its exactly like the first app, but if the first app is running and you click the top "Love" button, it extracts the text from the 1st app and writes it to the text box. Ditto for the Hate button. Naturally, you need to start the 1st app first and click the buttons to put the text in the text boxes. Then start the 2nd app and it should retrieve the text.
Tested with x86 on GCC 4.4.1 and MS VC 9 (ver. 15).
Code:
//Main.cpp - ExtractTextSource
//g++ Main.cpp -mwindows -o ExtractTextSource -s -Os
//cl Main.cpp Kernel32.lib User32.lib /O1 /Os /MT /FeExtractTextSource.exe
//GCC 4.4.1 compiles/links to 8,192 bytes
//VC9 (Version 15) 38400 bytes memory, 40,960 On Disk
#include <windows.h>
#define BTN_LOVE 1500
#define BTN_HATE 1505
#define TXT_LOVE 1510
#define TXT_HATE 1515
LRESULT CALLBACK fnWndProc(HWND hWnd, unsigned int msg, WPARAM wParam, LPARAM lParam)
{
switch(msg)
{
case WM_CREATE:
{
HINSTANCE hIns=((LPCREATESTRUCT)lParam)->hInstance;
CreateWindowEx(0,"button","Love",WS_CHILD|WS_VISIBLE,125,15,80,30,hWnd,(HMENU)BTN_LOVE,hIns,0);
CreateWindowEx(WS_EX_CLIENTEDGE,"edit","",WS_CHILD|WS_VISIBLE,25,60,285,22,hWnd,(HMENU)TXT_LOVE,hIns,0);
CreateWindowEx(0,"button","Hate",WS_CHILD|WS_VISIBLE,125,105,80,30,hWnd,(HMENU)BTN_HATE,hIns,0);
CreateWindowEx(WS_EX_CLIENTEDGE,"edit","",WS_CHILD|WS_VISIBLE,25,150,285,22,hWnd,(HMENU)TXT_HATE,hIns,0);
return 0;
}
case WM_COMMAND:
{
switch(LOWORD(wParam))
{
case BTN_LOVE:
SetWindowText(GetDlgItem(hWnd,TXT_LOVE),"The World Needs More Love!");
break;
case BTN_HATE:
SetWindowText(GetDlgItem(hWnd,TXT_HATE),"The World Needs More Hate!");
break;
}
return 0;
}
case WM_DESTROY:
{
PostQuitMessage(0);
return 0;
}
}
return (DefWindowProc(hWnd, msg, wParam, lParam));
}
int WINAPI WinMain(HINSTANCE hIns, HINSTANCE hPrevIns, LPSTR lpszArgument, int iShow)
{
char szClassName[]="ExtractTextSource";
WNDCLASSEX wc;
MSG messages;
HWND hWnd;
memset(&wc,0,sizeof(WNDCLASSEX));
wc.lpszClassName = szClassName, wc.lpfnWndProc = fnWndProc;
wc.cbSize = sizeof (WNDCLASSEX), wc.hIcon = LoadIcon(NULL,IDI_APPLICATION);
wc.hInstance = hIns, wc.hCursor = LoadCursor(NULL,IDC_ARROW);
wc.hbrBackground = (HBRUSH)COLOR_BTNSHADOW;
RegisterClassEx(&wc);
hWnd=CreateWindowEx(0,szClassName,szClassName,WS_OVERLAPPEDWINDOW,150,150,350,250,HWND_DESKTOP,0,hIns,0);
ShowWindow(hWnd,iShow);
while(GetMessage(&messages,NULL,0,0))
{
TranslateMessage(&messages);
DispatchMessage(&messages);
}
return messages.wParam;
}
Code for "ExtractTextTarget"
Code:
//Main.cpp - "ExtractTextTarget"
//g++ Main.cpp -mwindows -o ExtractTextTarget -s -Os
//cl Main.cpp Kernel32.lib User32.lib /O1 /Os /MT /FeExtractTextTarget.exe
//compiles/links to 7680, 8192 bytes on disk GCC 4.4.1; x86
//compiles/links VC 15 38,400 bytes memory, 40,960 On Disk; x86
#include <windows.h>
#define BTN_LOVE 1500
#define BTN_HATE 1505
#define TXT_LOVE 1510
#define TXT_HATE 1515
LRESULT CALLBACK fnWndProc(HWND hWnd, unsigned int msg, WPARAM wParam, LPARAM lParam)
{
char szBuffer[64];
HWND hSource = NULL;
HWND hTxt = NULL;
switch(msg)
{
case WM_CREATE:
{
HINSTANCE hIns=((LPCREATESTRUCT)lParam)->hInstance;
CreateWindowEx(0,"button","Love",WS_CHILD|WS_VISIBLE,125,15,80,30,hWnd,(HMENU)BTN_LOVE,hIns,0);
CreateWindowEx(WS_EX_CLIENTEDGE,"edit","",WS_CHILD|WS_VISIBLE,25,60,285,22,hWnd,(HMENU)TXT_LOVE,hIns,0);
CreateWindowEx(0,"button","Hate",WS_CHILD|WS_VISIBLE,125,105,80,30,hWnd,(HMENU)BTN_HATE,hIns,0);
CreateWindowEx(WS_EX_CLIENTEDGE,"edit","",WS_CHILD|WS_VISIBLE,25,150,285,22,hWnd,(HMENU)TXT_HATE,hIns,0);
return 0;
}
case WM_COMMAND:
{
switch(LOWORD(wParam))
{
case BTN_LOVE:
{
hSource = FindWindow("ExtractTextSource","ExtractTextSource");
if(hSource)
{
hTxt = GetDlgItem(hSource,TXT_LOVE);
if(hTxt)
{
SendMessage(hTxt,WM_GETTEXT,32,(LPARAM)szBuffer);
SetWindowText(GetDlgItem(hWnd,TXT_LOVE),szBuffer);
}
}
}
break;
case BTN_HATE:
{
hSource = FindWindow("ExtractTextSource","ExtractTextSource");
if(hSource)
{
hTxt = GetDlgItem(hSource,TXT_HATE);
if(hTxt)
{
SendMessage(hTxt,WM_GETTEXT,32,(LPARAM)szBuffer);
SetWindowText(GetDlgItem(hWnd,TXT_HATE),szBuffer);
}
}
}
break;
}
return 0;
}
case WM_DESTROY:
{
PostQuitMessage(0);
return 0;
}
}
return (DefWindowProc(hWnd, msg, wParam, lParam));
}
int WINAPI WinMain(HINSTANCE hIns, HINSTANCE hPrevIns, LPSTR lpszArgument, int iShow)
{
char szClassName[]="ExtractTextTarget";
WNDCLASSEX wc;
MSG messages;
HWND hWnd;
memset(&wc,0,sizeof(WNDCLASSEX));
wc.lpszClassName = szClassName, wc.lpfnWndProc = fnWndProc;
wc.cbSize = sizeof (WNDCLASSEX), wc.hIcon = LoadIcon(NULL,IDI_APPLICATION);
wc.hInstance = hIns, wc.hCursor = LoadCursor(NULL,IDC_ARROW);
wc.hbrBackground = (HBRUSH)COLOR_BTNSHADOW;
RegisterClassEx(&wc);
hWnd=CreateWindowEx(0,szClassName,szClassName,WS_OVERLAPPEDWINDOW,150,150,350,250,HWND_DESKTOP,0,hIns,0);
ShowWindow(hWnd,iShow);
while(GetMessage(&messages,NULL,0,0))
{
TranslateMessage(&messages);
DispatchMessage(&messages);
}
return messages.wParam;
}