Click here to Skip to main content
15,886,831 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I'm trying to grab the URL from the address bar of the current Web Page in (Internet Explorer). This is what I have so far, but it only returns an"E".

C++
int sent=0, recvd=0;
		sent = send(sniff_socket, "GET HTTP/1.0\r\n\r\n", 16, 0);
		char buff99[2048] = {0};
		recvd = recv(sniff_socket,buff99,2048,0);
		cout << "Bytes Sent: " << sent << endl;
		myfile << "Bytes Sent: " << sent << endl;
		cout << "Bytes Received: " << recvd << endl;
		myfile << "Bytes Received: " << recvd << endl;
		cout << "Data Received: " << buff99 << endl;
		myfile << "Data Received: " << buff99 << endl;
Posted

Ok, you need a whole new approach. Easiest way would be with shell connectors as described at http://www.codeguru.com/Cpp/I-N/ieprogram/article.php/c4403[^]. This works with IE versions 5 - 10 (version 10 is pre-release with windows 8).

This code is the relevant parts of that article in a minimal console program.

Basic idea is attach to the shell windows enumerator and go through the list of windows. This is done in the main() function.
It then prints some details on each window found in the PrintBrowserInfo() function.

C++
#include <stdio.h>
#include <wchar.h>
#include <Windows.h>
#include <Exdisp.h>

//This imports the shell extionsions
//we disable warnings of multiple defines
#pragma warning(disable: 4192)
#pragma warning(disable: 4146)
#import <mshtml.tlb>
#import <shdocvw.dll>

void PrintBrowserInfo(IWebBrowser2 *pBrowser) {
	//These functions return Unicode strings.
	BSTR bstr;
	//Get the window title
	pBrowser->get_LocationName(&bstr);
	wprintf(L"Title: %s\n", bstr);
	SysFreeString(bstr);
	//Detect if this is Windows Explorer (My Computer) or Internet Explorer (the internet)
	IDispatchPtr spDisp;
	char *type = "Windows Explorer";
	if (pBrowser->get_Document(&spDisp) == S_OK && spDisp != NULL) {
		MSHTML::IHTMLDocument2Ptr spHtmlDocument(spDisp);
		if (spHtmlDocument != NULL) {
			MSHTML::IHTMLElementPtr spHtmlElement;
			spHtmlDocument->get_body(&spHtmlElement);
			if (spHtmlElement != NULL) {
				type = "Internet Explorer";
			}
		}
		spDisp.Release();
	}
	printf(" Type: %s\n", type);
	//Get the URL of the folder or web page
	pBrowser->get_LocationURL(&bstr);
	wprintf(L"  URL: %s\n\n", bstr);
	SysFreeString(bstr);
}

int main() {
	CoInitialize(NULL); //We need COM
	SHDocVw::IShellWindowsPtr spSHWinds;
	IDispatchPtr spDisp;
	//Find all explorer (Windows and Internet) and list them
	if (spSHWinds.CreateInstance(__uuidof(SHDocVw::ShellWindows)) == S_OK) {
		long nCount = spSHWinds->GetCount();
		for (long i = 0; i < nCount; i++) {
			_variant_t va(i, VT_I4);
			spDisp = spSHWinds->Item(va);
			SHDocVw::IWebBrowser2Ptr spBrowser(spDisp);
			if (spBrowser != NULL) {
				//spBrowser->AddRef();
				PrintBrowserInfo((IWebBrowser2 *)spBrowser.GetInterfacePtr());
				spBrowser.Release();
			}
		}
	} else {
		puts("Shell windows failed to initialise");
	}
	system("PAUSE");
	return 0;
}
 
Share this answer
 
v2
Comments
Member 7766180 7-Nov-11 2:48am    
This is grear! Thank you!
Check 'Connect to the current Internet Explorer window' in Automate the Active Windows Explorer or Internet Explorer Window[^]
 
Share this answer
 
Comments
Member 7766180 6-Nov-11 23:50pm    
Thank you. However; I can't seem to find anything about grabbing the URL of the current web page.
You are missing the URI part of the request. The format is
METHOD URI VERSION

the home URI is /.

For example if I send GET HTTP/1.0 to www.google.com I get a 404 not found error.
If I send GET / HTTP/1.0 I get 302 Found (redirect) because I am in Australia, and it redirects me to www.google.com.au

I was using telnet for this, ncat (part of nmap[^]) works just as well tho.

This however will not get you the address of the web page. It will return the headers and page contents.
 
Share this answer
 
Comments
Member 7766180 7-Nov-11 0:27am    
OK, confused as usual. I just want the URL that is visible in the address bar. How would I modify my code to do that? I'm only getting an "E" in data received. sent = send(sniff_socket, "GET / HTTP/1.0 ", 16, 0);
Thank you.

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