Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations John Tel on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Determining current URL of Internet Explorer 2

Status
Not open for further replies.

ChrisRChamberlain

Programmer
Mar 23, 2000
3,392
GB
Hi all,

If you create an instance of Internet Explorer with

oIE = CreateObject([InternetExplorer.Application])

the current URL can be determined by the value of the property

oIE.LocationURL

If IE has been launched by some other means, how do you access the value of the equivalent property, oIE.LocationURL?

TIA

FAQ184-2483 - the answer to getting answered.​
Chris [pc2]
 
Chris,

When you say 'by some other means', do you mean from outside VFP? If so, I can't see how it would be possible. After all, there could be umpteen instances of IE sitting on your desktop. VFP could cycle through them, getting their window handles, but that wouldn't tell you anything about URLs.

Be interesting to see if someone comes up with an answer.

Mike


Mike Lewis
Edinburgh, Scotland

My Visual Foxpro web site: My Crystal Reports web site:
 
Mike

'...do you mean from outside VFP?'

Yes - it's a situation whereby if a user clicks on the print button in IE, the requirement is to 'capture' the current url of that particular instance of IE.

The workaround is to control the whole operation from within VFP whereby you do know the current URL and can obviously print under programmatic control, but users being users, bless 'em, will expect the application to work regardless of what launched IE.


FAQ184-2483 - the answer to getting answered.​
Chris [pc2]
 
Chris,

I can see why you would want to do that. I wish I could see how to do it.

If you were instantiating IE as a COM object or ActiveX control, you could trap the IE print event, but I don't know how to do that if you have no object reference to the instance of IE.

Mike


Mike Lewis
Edinburgh, Scotland

My Visual Foxpro web site: My Crystal Reports web site:
 
Here's what I could think of, it uses DDE and perhaps that isn't the best, but it is small and simple. With DDE you can control a number of aspects of IE including making it jump to an URL and such. The possibilities of multiple windows being open would have to be handled.

mchannum = DDEInitiate('IExplore','if mchannum != 1
lcUrl = DDERequest(mchannum, "0xFFFFFFFF")
lcURL = SUBSTR(lcURL, 2, AT(",", lcURL) - 3)
MESSAGEBOX(lcURL)
=DDETerminate(mchannum)
ELSE
MESSAGEBOX("NOPE!")
ENDIF


Slighthaze = NULL
craig1442@mchsi.com
"Whom computers would destroy, they must first drive mad." - Anon​
 
It should be noted that the same information can also be had from Netscape by replacing 'IExplore' with 'NETSCAPE'

Slighthaze = NULL
craig1442@mchsi.com
"Whom computers would destroy, they must first drive mad." - Anon​
 
Ok, here is the API way of getting the same info...

Code:
MESSAGEBOX(GetIEUrl())

*********************
FUNCTION GetDisplayedText(tcHWND)
*********************
	#DEFINE WM_GETTEXTLENGTH 0xE
	#DEFINE WM_GETTEXT 0xD
	
	DECLARE INTEGER SendMessage IN user32;
		INTEGER HWND,;
		INTEGER Msg,;
		INTEGER wParam,;
		STRING LPARAM

	lnLength = SendMessage(tcHWND, WM_GETTEXTLENGTH, 0, 0) + 1
	lcBuffer = REPLICATE(CHR(0), lnLength)
	SendMessage(tcHWND, WM_GETTEXT, lnLength, @lcBuffer)
	CLEAR DLLS SendMessage
	RETURN lcBuffer
ENDFUNC


*********************
FUNCTION GetIEUrl
*********************
	DECLARE INTEGER FindWindow IN user32;
		STRING lpClassName,;
		STRING lpWindowName
		
	DECLARE LONG FindWindowEx IN User32 ;
		LONG hwndParent, LONG hwndChildAfter, ;
		STRING lpszClass, STRING lpszWindow

	lnIE = FindWindow('IEFrame',NULL)
	lnWorker = FindWindowEx(lnIE, 0,'WorkerW',NULL)
	lnToolbar = FindWindowEx(lnWorker, 0,'RebarWindow32',NULL)
	lnComboboxEx = FindWindowEx(lnToolbar, 0, 'ComboboxEx32', NULL)
	lnCombo = FindWindowEx(lnComboboxEx, 0,'ComboBox',NULL)
	lnEdit = FindWindowEx(lnCombo, 0,'Edit',NULL)
	CLEAR DLLS FindWindow, FindWindowEx
	RETURN GetDisplayedText(lnEdit)
ENDFUNC

Slighthaze = NULL
craig1442@mchsi.com
"Whom computers would destroy, they must first drive mad." - Anon​
 
Thank you for the positive feedback. Just in case someone did want to enumerate through the instances of IE you could use an API call to GetClassName and check it against the known class name for IE (IEFrame) - I will provide a working example below. My one question on all this is whether this will work for an instance of IE that does not have a visible Address bar. Will have to go find a popup somewhere and test it I guess. Anyways, here is the working example for multiple instances:

Code:
GetAllIEUrls()

PROCEDURE GetAllIEUrls
	DECLARE INTEGER GetActiveWindow IN User32
	DECLARE INTEGER GetClassName IN  User32 INTEGER, STRING, INTEGER
	DECLARE INTEGER GetWindow IN User32 INTEGER, INTEGER

	#DEFINE GW_HWNDFIRST  0x0
	#DEFINE GW_HWNDNEXT   0x2
	#DEFINE GW_HWNDLAST   0x1
	#DEFINE BUFFERLENGTH 255
	LOCAL lnActiveWindowHandle, lnWindowHandle, lnInstanceCount, ;
		llFirstTime, lcClassName, lnLastWindow
	lnActiveWindowHandle = GetActiveWindow()
	STORE 0 TO lnWindowHandle, lnInstanceCount
	llFirstTime = .T.
	lcClassName = REPLICATE(CHR(0),BUFFERLENGTH)
	lnLastWindow = GetWindow(lnActiveWindowHandle, GW_HWNDLAST)
	DO WHILE lnWindowHandle != lnLastWindow
		IF llFirstTime
			lnWindowHandle = GetWindow(lnActiveWindowHandle, GW_HWNDFIRST)
			llFirstTime = .F.
		ELSE
			lnWindowHandle = GetWindow(lnWindowHandle, GW_HWNDNEXT)
		ENDIF
		GetClassName(lnWindowHandle, @lcClassName, BUFFERLENGTH)
		IF LEFT(lcClassName,7) == "IEFrame"
			lnInstanceCount = lnInstanceCount + 1
			MESSAGEBOX(GetIEUrl(lnWindowHandle))
		ENDIF
	ENDDO

	IF lnInstanceCount = 0
		MESSAGEBOX("No Instances of IE found")
	ENDIF
	CLEAR DLLS GetActiveWindow, GetClassName, GetWindow
ENDPROC

*********************
FUNCTION GetDisplayedText(tcHWND)
*********************
	DECLARE INTEGER SendMessage IN User32 INTEGER, INTEGER, INTEGER, STRING

	#DEFINE WM_GETTEXTLENGTH 0xE
	#DEFINE WM_GETTEXT 0xD
	LOCAL lnLength, lcBuffer
	lnLength = SendMessage(tcHWND, WM_GETTEXTLENGTH, 0, 0) + 1
	lcBuffer = REPLICATE(CHR(0), lnLength)
	SendMessage(tcHWND, WM_GETTEXT, lnLength, @lcBuffer)
	CLEAR DLLS SendMessage
	RETURN lcBuffer
ENDFUNC


*********************
FUNCTION GetIEUrl(tcHWND)
*********************
*!* Commented out lines as the function no longer requires
*!* them with the way we are using it
*!*	    DECLARE INTEGER FindWindow IN User32 STRING, STRING
	DECLARE LONG FindWindowEx IN User32 LONG, LONG, STRING, STRING

	LOCAL lnWorker, lnToolbar, lnComboboxEx, lnCombo, lnEdit &&,lnIE
*!*	    lnIE = FindWindow('IEFrame',NULL)
	lnWorker = FindWindowEx(tcHWND, 0,'WorkerW',NULL)
	lnToolbar = FindWindowEx(lnWorker, 0,'RebarWindow32',NULL)
	lnComboboxEx = FindWindowEx(lnToolbar, 0, 'ComboboxEx32', NULL)
	lnCombo = FindWindowEx(lnComboboxEx, 0,'ComboBox',NULL)
	lnEdit = FindWindowEx(lnCombo, 0,'Edit',NULL)
	CLEAR DLLS FindWindowEx &&, FindWindow
	RETURN GetDisplayedText(lnEdit)
ENDFUNC

Slighthaze = NULL
craig1442@mchsi.com
"Whom computers would destroy, they must first drive mad." - Anon​
 
Have just finished testing this on various IE windows that do not have a visible address bar among other things (such as most advertising popups) and it will indeed show the address or just "About:Blank"...so though the controls are invisible, they do still exist in memory.

Slighthaze = NULL
craig1442@mchsi.com
"Whom computers would destroy, they must first drive mad." - Anon​
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top