Click here to Skip to main content
15,884,176 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
Hi all! My program is for the capturing mouse behavior on the browsers(opera, IE, firefox, chrome). So I sat a global mouse hook, and now I know where the mouse pointer is , i.e. is the window under the mouse pointer a browser or not. So, when the click is caught on a browser I know the following:
1. browser window handle
2. the browser window process ID

the question is "how can I retrieve the URL in which the browser is?"

please, don't tell me "use SPY++" or something like that, because I've already tried to do that, but failed :(
Also I tried to call javascript "location.href", and... nothing usable (I know nothing in javascript.. maybe do something wrong)
and finally tried to use IWebBrowser2... but get_LocationURL returns an empty string. here is the code (for IE):

<br />
	IWebBrowser2 *pIwb;<br />
	OleInitialize( 0 );<br />
	CoCreateInstance( CLSID_InternetExplorer, NULL, CLSCTX_ALL,IID_IWebBrowser2, (void**)&pIwb );<br />
	if( pIwb )<br />
	{<br />
		VARIANT vEmpty;<br />
		VARIANT_BOOL bBusy = VARIANT_TRUE;<br />
		VariantInit( &vEmpty );<br />
<br />
		HINSTANCE hIEInstance = LoadLibrary( "atl.dll" );<br />
<br />
		AttachCtrl AtlAxAttachControl = (AttachCtrl)GetProcAddress( hIEInstance, "AtlAxAttachControl" );<br />
<br />
		HWND hWnd = ::GetForegroundWindow();<br />
		HRESULT hResult = AtlAxAttachControl( pIwb, hWnd , 0 );<br />
<br />
		CComBSTR bufferScript;<br />
		hResult = pIwb->get_LocationURL( &bufferScript );<br />
                /*hResult gets value 1 here*/<br />
<br />
		SysFreeString( bufferScript );<br />
<br />
		pIwb->Quit();<br />
		pIwb->Release();<br />
<br />
		FreeLibrary( hIEInstance );<br />
	}<br />


I hope somebody will provide me a piece of code, which will do this job
Posted
Updated 20-Jan-10 8:33am
v2

1 solution

More of a suggestion, but with the browser window handle, could you walk through the child windows till you come to the control that contains the URL in it? Identifying this may take some effort, but with that handle, a simple GetWindowText should get the URL you are after.
 
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