I want to get a running application text e.g if i am running notepad then i want to get the text written inside it .For this first i have to get the handle of notepad but i don't know how to get the notepad handle so please tell me .Then through which functions i can get its inside text ? which header files to include ? what are necessary declarations? Please help me i am new to windows API programming.i have gone through basic tutorials of windows programming but that doesn't help me a lot.
2 Answers
Use FindWindowEx. Though you must have been able to find this yourself, if you were looking/googling for a way to "find notepad handle in C++" ;)
You can even find complete examples on "Sending text to notepad in C++"
8 Comments
Murtaza Hussain
My basic problem is how to get notepad window handle and moreover i want to retrieve text from notepad
John
ShellExecuteA(NULL, "open", "mytext.txt", NULL, NULL, SW_SHOWNORMAL); Just for completeness, that example on daniweb needs to request the user opens notepad, this line may open it for you (I uses it for firefox and "stackoverflow.com/index.html" for example). The 'A' suffix is some strange (un)ASCI-ness on my system.
GolezTrol
You get the notepad window handle using FindWindowEx, as demonstrated in the examples I linked to. The links being that blue text in my answer. You can actually click on them to get to a page with that information on it. :)
GolezTrol
That said, there is hardly ever a practicle use to controlling Notepad in this way. It's way easier and more solid if you just put a text box on a form of your own. But that's an entirely different tutorial.
GolezTrol
You can use the WM_GETTEXT message to get text from a control in another application. It's in the same docs. :)
|
To expand on GolezTrol's answer, you could do this:
#include <windows.h>
#include <tchar.h>
int CALLBACK _tWinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance, LPTSTR lpCmdLine, int nCmdShow ) {
HWND hwnd = FindWindow( _T("Notepad"), NULL);
hwnd = FindWindowEx( hwnd, NULL, _T("edit"), NULL );
TCHAR lpText[256];
SendMessage( hwnd, WM_GETTEXT, _countof(lpText), (LPARAM)lpText);
MessageBox(0, lpText, lpText, 0);
return ERROR_SUCCESS;
}
In reality, you would probably use a more reliable method of window identification (eg. enumerating all windows and verifying what process it belongs to)