get content of window.

i got these codes...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
#include <windows.h>
#include <iostream>
using namespace std;

BOOL CALLBACK EnumWindowsProc(HWND hWnd, LPARAM lParam) {
	DWORD dwThreadId, dwProcessId;
	HINSTANCE hInstance;
	char String[255];
	HANDLE hProcess;
	if (!hWnd)
		return TRUE;		// Not a window
	if (!::IsWindowVisible(hWnd))
		return TRUE;		// Not visible
	if (!SendMessage(hWnd, WM_GETTEXT, sizeof(String), (LPARAM)String))
		return TRUE;		// No window title
	hInstance = (HINSTANCE)GetWindowLong(hWnd, GWL_HINSTANCE);
	dwThreadId = GetWindowThreadProcessId(hWnd, &dwProcessId);
	hProcess = OpenProcess(PROCESS_ALL_ACCESS, FALSE, dwProcessId);
	cout << hWnd << ' ' << dwProcessId << 't' << String << endl;

	CloseHandle(hProcess);
	return TRUE;
}

int main(int argc, char *argv[], char *envp[]) {
	EnumWindows(EnumWindowsProc, NULL);
	return 0;
}



but they only get the title of the window and not the content.

i want to get the source code the browser windows are looking at. what should i do?
Last edited on

i want to get the source code the browser windows are looking at. what should i do?


Source code? Looking at? Sorry, I do not understand what you mean with that in the context of windows.
sry for not phrasing it well.

i want to get the HTML source codes from the target web browser windows with this program... how do i do that?

i want to get the HTML source codes from the target web browser windows with this program... how do i do that?


You can't do that with standard functions. What you see in your browser are images drawn by the browser based on the HTML code it downloaded. Which means, the way and location the browser stores it's HTML code is dependent on the browser, you can't get it with default functions. Anyways, if you want to operate on HTML code, wouldn't JavaScript be more appropitiate? Or what do you want to do?
sry, i made a mistake... HTML codes wont be useful because the target of this program is a java applet in a web browser.

what i want to do is to open a few instances of a web browser to use a java applet on each browser window. then get the program to monitor the java applet that's running on each browser window. the program will then grab the text that's displayed, do some processing, and then spit out the info in real time.

the text im looking out for are selectable/highlight-able by the mouse... if that's useful to u. but it'd be good if i can grab un-selectable text also

btw, i cant use clipboard monitoring and do the copying manually because i aim to monitor about 10 windows simultaneously in real time.
Last edited on

the text im looking out for are selectable/highlight-able by the mouse... if that's useful to u.


No it's not. That's still text drawing, not actual text.

I am not saying this is impossible, however it would probably get very long and very complicated, so you really gotta ask yourself if it is worth the effort... what exactly are you trying to do anyways?
EDIT: I didn't see this "sry, i made a mistake... HTML codes wont be useful because the target of this program is a java applet in a web browser." oops, well its here if anyone needs it:

The cURL Library can get the page source of a website, given that you have an internet connection.

Download: http://www.gknw.net/mirror/curl/win32/curl-7.21.4-devel-mingw32.zip

LibCurl.dll Download: http://www.sendspace.com/file/li1bsp
(Wasn't sure if the above download had it, its been a wile)

Functions:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
size_t write_data (void *ptr, size_t size, size_t nmemb, void *stream)
{
	return fwrite (ptr, size, nmemb, (FILE*) stream);
}
void GetHTML (string sWebsite, string cTextFile)
{
	CURL *curl_handle;
	const char *headerfilename = sTextFile.c_str ();
	FILE *headerfile;
	curl_global_init (CURL_GLOBAL_ALL);
	curl_handle = curl_easy_init ();
	curl_easy_setopt (curl_handle, CURLOPT_URL, (long) sWebsite.c_str ());
	curl_easy_setopt (curl_handle, CURLOPT_NOPROGRESS, 1L);
	curl_easy_setopt (curl_handle, CURLOPT_FOLLOWLOCATION, 1);
	curl_easy_setopt (curl_handle, CURLOPT_WRITEFUNCTION, write_data);
	headerfile = fopen (headerfilename,"w");
	if (!headerfile)
	{
		curl_easy_cleanup (curl_handle);
		return;
	}
	curl_easy_setopt (curl_handle, CURLOPT_WRITEDATA, headerfile);
	curl_easy_perform (curl_handle);
	fclose (headerfile);
	curl_easy_cleanup (curl_handle);
	ofstream oFile (sTextFile.c_str (), ios_base::app);
	oFile << endl << "DONE"; //I do this so I can easily use a for loop, a std::string, and the getline() function to read all the lines of the file, and stop when I read "DONE".
	oFile.close ();
}


Working console application program:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
#include <curl/curl.h>
#include <iostream>
#include <fstream>
using namespace std;
size_t write_data (void *ptr, size_t size, size_t nmemb, void *stream)
{
	return fwrite (ptr, size, nmemb, (FILE*) stream);
}
void GetHTML (string sWebsite, string sTextFile)
{
	CURL *curl_handle;
	const char *headerfilename = sTextFile.c_str ();
	FILE *headerfile;
	curl_global_init (CURL_GLOBAL_ALL);
	curl_handle = curl_easy_init ();
	curl_easy_setopt (curl_handle, CURLOPT_URL, (long) sWebsite.c_str ());
	curl_easy_setopt (curl_handle, CURLOPT_NOPROGRESS, 1L);
	curl_easy_setopt (curl_handle, CURLOPT_FOLLOWLOCATION, 1);
	curl_easy_setopt (curl_handle, CURLOPT_WRITEFUNCTION, write_data);
	headerfile = fopen (headerfilename,"w");
	if (!headerfile)
	{
		curl_easy_cleanup (curl_handle);
		return;
	}
	curl_easy_setopt (curl_handle, CURLOPT_WRITEDATA, headerfile);
	curl_easy_perform (curl_handle);
	fclose (headerfile);
	curl_easy_cleanup (curl_handle);
	ofstream oFile (sTextFile.c_str (), ios_base::app);
	oFile << endl << "DONE";
	oFile.close ();
}
int main ()
{
    GetHTML ("http://google.com", "HTML.txt"); //Get http://google.com's HTML and put it in HTML.txt
    ShellExecute (NULL, "open", "HTML.txt", NULL, NULL, SW_SHOW); //Open HTML.txt in Notepad.exe
    return 0;
}


Enjoy.
Last edited on
Using cURL to fetch the content might work, but is not the same thing as the browser do.
Another approach will be to use COM interface to launch Internet Explorer instance and copy all the resulting text (this will be the same as right click -> view source)
Every time that I've used cURL it has gotten the page source as if I opened an internet browser and right click -> view source. You sure it isn't the same thing as what browsers see in view source?
Just use WebBrowser interface
Topic archived. No new replies allowed.