Click here to Skip to main content
15,900,511 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi
Can any one please suggest some idea on how to read the contents of active notepad file. i.e say i have opened c:\read.txt and working with it, so there is already some data in it ..... using my application i need to read the contents of this notepad and print it on the console window or save it to some other file.

Is there any win32 api to perform this, if yes please, else tell me how can i read the contents and what are the possible ways of finding solution.

More clearly Suppose user opens a file called D:\somedata.txt and this file contains some data. So for now this will be the active window, when i run my application i should be able to read the contents of this somdata.txt file, the file name is not given as input, because user may open any text file he/she needs. Irrespective of file name my application should be able to read contents from already opened notepad file.

Thank you
Posted
Updated 18-Aug-11 4:30am
v2
Comments
Timberbird 18-Aug-11 10:41am    
I wouldn't recommend you to even consider using file name. As far as I understand, you need notepad window content, which may differ from file content - when user has already edited file but changes aren't saved.
Besides, what if notepad is not an active window? Should you read text when notepad is in the background?
You can enumerate processes, find notepad.exe, get its window and then... well, enumerate windows searching for the content area and get its text? I can't say for sure, but it's worth investigating
Richard MacCutchan 18-Aug-11 11:51am    
Yes that's the only way; you may want to repost this as a solution.
Sergey Alexandrovich Kryukov 18-Aug-11 18:37pm    
I'm not sure notepad.exe should be found, also, some other process can use this name, accidentally. There is no reliable method. I provided detailed instructions on APIs to use, please see.
--SA

If you want to get the active window, use the GetForegroundWindow API.
If you want to get all the running instances of Notepad, use FindWindow like so -
FindWindow(_T("Notepad"), 0);

After you get the handle to the window, use FindWindowEx to get the handle to the edit control of notepad -
FindWindowEx(hNotepad, 0, _T("Edit"), 0);

You can then send the WM_GETTEXT message using SendMessage to the handle returned by FindWindowEx to get the text within notepad.
 
Share this answer
 
Comments
manu g m 19-Aug-11 0:01am    
Thanx a lot superman :) :) it worked like a gem :)
There is no such thing as "notepad file".

What you can do is this: enumerate all windows in the system. First, get all top-level windows on the desktop which you can get using Windows API GetDesktopWindow, http://msdn.microsoft.com/en-us/library/ms633504(v=VS.85).aspx[^].

You can iterate through children first using GetWindow with the uCmd parameter GW_CHILD, than GetNextWindow, see http://msdn.microsoft.com/en-us/library/ms633515%28v=VS.85%29.aspx[^], http://msdn.microsoft.com/en-us/library/ms633509(v=VS.85).aspx[^].

Using these function recursively, you can get first get all top-level windows on the desktop and than all the children of each of those top-level windows.

Now, to select the window which represents the text editing control, you need to know something about its type. One of the ways to filter right type is using the function GetClassName, see http://msdn.microsoft.com/en-us/library/ms633582%28v=vs.85%29.aspx[^].

When this is done; and you figured out what window do you want, use the function GetWindowText to retrieve the text you need. See http://msdn.microsoft.com/en-us/library/ms633520%28v=VS.85%29.aspx[^]. For many windows it would not make sense, for overlapped top-level windows with the title bar it will return the title text, but for text edit control it will return what you need.

—SA
 
Share this answer
 
Comments
QiaoZiliang 20-Aug-11 11:27am    
I have checked it out in MSDN, SAKryukov is right, see the explanation of WM_GETTEXT in MSDN.
Sergey Alexandrovich Kryukov 20-Aug-11 23:10pm    
Thank you for confirmation.
--SA
manu g m 12-Sep-11 10:16am    
Thanx SA :)
Sergey Alexandrovich Kryukov 12-Sep-11 12:31pm    
You're welcome.
If you agree that it makes sense, please formally accept the answer (green button); you can accept more than one -- thanks.
--SA
[EDIT:] It seems my answer was so long that Superman has already provided a (much better) solution in the time I've spent typing this. Oops.


Sure thing. While the initial temptation may be to find the Notepad main window then to GetWindowText on it, this won't work. This will give you the title of the window only. The actual text area is a child window of the Notepad main window.

I'd get the handle of the main window of notepad then I would enumerate all of it's child windows. I would expect the target window to be Child #N, where N will be the same for each time you run the program.
I'd type (say) 100 characters into Notepad, then find which child window has a text length of 100 chars. This will(should!) be the edit-box that the user types into. If this window is the 2nd or 3rd child window, it should be consistently the 2nd or 3rd window - useful when the text length of the edit-window is unknown.

Here, this code will get the window title of the first running instance of Notepad found.

C++
char *buffer;
HWND noteHwnd = NULL;

noteHwnd = FindWindowEx(NULL, NULL, "Notepad", NULL);
if (noteHwnd)
{
    int len = GetWindowTextLength(noteHwnd);
    if (len > 0)
    {
        buffer = (char*)calloc(1, len+1);
        GetWindowText(noteHwnd, buffer, len+1);
        printf("Number of characters found: %d\n", len);
        printf("Text: %s\n", buffer);
        free(buffer);
    }
}


Of course, another way would be to debug a copy of Notepad. By carefull stepping through the program, you can catch the moment that the text-editing window gets created. You can then see exactly what the address is that this is stored. Coming back later you could OpenProcessMemory and read the Hwnd of the window directly from the memory that it was saved to originally. (You can use this technique of memory-reading to make a bot that will play Minesweeper for you flawlessly)

That said, I'd expect the method of enumerating the child windows of the main window to be the more effective method.


Of course, hundreds of people have probably already done just this. Searching for a finished result may well be less entertaining than deriving one yourself. That's why I never searched for a finished solution. :wink:
 
Share this answer
 
v3
Comments
manu g m 19-Aug-11 0:04am    
Thanks for ur precious time. In this case we get only the text of window. The buffer size will be set to size of window title.
#include "stdafx.h"
#include "windows.h"
#include "stdio.h"
#include "conio.h"


int _tmain(int argc, _TCHAR* argv[])
{
	HWND hnd;
	HWND edithnd;
	CHAR *buf;
	hnd=FindWindow(_T("NotePad"),0);
	if(hnd==NULL)
		printf("Handle not found");
	edithnd=FindWindowEx(hnd,0,_T("Edit"),0);
	buf=(char *)calloc(200,1024);
	//printf("%d",sizeof(buf));
	SendMessage(edithnd,WM_GETTEXT,10000,(LPARAM)buf);
	wprintf(_T("%s"),buf);
	getch();
	/*return 0;*/
}


This is the code which displays the contents of active window on to console
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900