access url from microsoft web browser control in mfc VC++ - mfc

i am using Microsoft web browser control in an MFC Application.
It displays a login page initially using
control.navigate(URL) method
After login i need to access the URL from the control.
How this can be done?
Can anyone help me out

The WebBrowser control has an event OnBeforeNavigate2 that fires before switching pages. The event handler receives the new visited URL.
If you use a CDHTMLView, you don't even have to add an event handler: Just override the virtual OnBeforeNavigate2().

The URL is sent to you in the DocumentComplete event. Check the MFCIE sample, the sample handles the event to update its address bar.
Note the url may be different from the one you passed to Navigate or get from OnBeforeNavigate2. For example if you pass microsoft.com to Navigate2, you will get www.microsoft.com back due to server side redirects.

MSDN has a section all about "Using MFC to Host a WebBrowser Control". In there it mentions using the following:
//CWebBrowser2 m_browser - to access the WebBrowser control
CString string1, string2;
string1 = m_browser.GetLocationName(); // gets the page title
string2 = m_browser.GetLocationURL(); // gets the page URL
Regarding when you try to get that information, you could check m_browser.Busy(...) or m_browser.ReadyState(...) to see if any kind of navigation/download is currently happening.
I confess to never using the control myself so don't know if there's a simple way to get a callback saying that the URL has changed, but the above link should give you most of the information you need. Hopefully :)

Related

Getting current URL of Internet Explorer 8 programatically

I'm developing a simple Internet Explorer add-on and I'm trying to get the current URL of the Address Bar of the IE 8 using the series of (example) HWND handle = FindWindowEx(parent_handle,0,"AddressDisplay Control",NULL);
in the following order:
1- IEFrame
2- WorkerW
3- ReBarWindow32
4- Address Band Root
5- Edit
5- (alternatively) AddressDisplay Control
I've determined this series of controls using the winspy++ 1.6
The code I'm trying to get the current URL is:
TCHAR currentURL[255];
::GetWindowText(handle_to_the_Edit_or_AddressDisplay_control, currentURL, 255);
However it sets nothing into the currentURL array.
This method, with the names and series of controls slightly changed, used to work in earlier versions ıf IE, I could easliy get the current URL, but for IE 8 I've unable to get it.
What should I do? Thanks
Note: I've searched many pages and posts. No luck.
After applying #Remy Lebeau's suggestion char *p = _com_util::ConvertBSTRToString(bstrURL); , ::SendMessage(hMyEditControl,WM_SETTEXT,0,(long)p); started to display correctly.
And this explains why the IE 7 way of getting the URL from the UI controls doesn't work on IE 8:
Because that is not the right way to do it, even in IE7.
Just because it could be done that way does not mean it should be done that way.
There is a reason why a BHO has direct access to the underlying WebBrowser object,
the BHO should not rely on any particular UI representation of the WebBrowser object.
So that's the accepted answer.

CEFsharp load url is holding on to the previously navigated url

We have a C++ application (using Owl UI framework) which calls a CEFsharp Chromiumbrowser control inside an owl dialog. It is working perfectly the first time and the chromium embedded control shows the page properly. It is important to mention here that the first url which is say "http//www.someurl" navigates internally to "http://www.anotherurl/opensomething" each time. When we close the Owl MDI window and then try to open it again, the CEFSharp browser control tries to go straight to "http://www.anotherurl/opensomething" instead of going to "http//www.someurl" which it is being specifically asked to navigate to. We are using Chromiumbrowser load method to load the url but just cannot make it to load the first url from the second time onward. Our CEFsharp version is 63.03 . However if we open the chromium browser control in its own window and not embed it inside the MDI dialog it works perfectly everytime. Can't figure out so far as to why this is not loading the url properly from the second time around. Thanks for any help in advance.

Microsoft Equivalent of WebkitGTK hovering-over-link signal

I need to add ability to catch event each time there is mouse hover event both on Linux and Windows. In Linux I easily connect to hovering-over-link signal. I cannot seem to find Windows equivalent. Searched google, checked the MSDN, I could not find.
Does Such a functionality available in windows? If no is there a way to achieve it?
Can you use the StatusTextChange event? This event is fired when the mouse is hovered over a link, with sText set to the link URL.
It is also fired with additional messages such as when loading a page. In these cases, sText is set to "Connecting to (URL)", "Waiting for (URL)", etc. These messages can be simply ignored by doing a basic URL validation of sText.
A more involved method would be in the DocumentComplete event to search for all anchor elements using getElementsByTagName and attach to the HTMLAnchorEvents onmouseover. This of course wouldn't work for anchor elements dynamically created after page load.

How to get complete HTML body using browser helper object (BHO) in case of DHTML/AJAX page?

I'm writing a BHO that analyze the HTML taken from the 'onDocumentComplete' event of 'DWebBrowserEvents2'. Currently it works fine, unless I have a DHTML/AJAX page, where HTML handle is delivered too soon.
For sample, I tried using it on 'http://www.google.com'. From the 'onDocumentComplete' event I can get most of the page but in the topmost link/anchors, the 'href' for maps, videos, orkut etc. is not available (normally it is javascript:void(0)).
Has anyone any ideas how to capture it when the page is fully loaded rather than just when the frame/body is loaded?
Thanks,
UPDATE
It seems there is some problem with the MSHTML API. I have posted the same question on MSDN forum and some response. Also I have detailed out my problem and findings....
http://social.msdn.microsoft.com/Forums/en-US/ieextensiondevelopment/thread/d517dbd1-df22-4dce-8ff9-0ca6786757f9
In case someone finds some way to solve this then please do share...
Thanks,
The AJAX DHTML changes mostly don't cause a further onDocumentComplete call.
You need to register for further Window or Document events such as DISPID_HTMLWINDOWEVENTS2_ONLOAD.
One method is to advise the window of a com object that you provide with the generic event sink interface.
hr = AtlAdvise(winDisp, pWinHandler, DIID_HTMLWindowEvents2, &dwCookie);
When this further event is triggered re examining the document you will find that it is updated.

After Navigate2 Method returns S_OK Stuck at READYSTATE of READYSTATE_LOADING

I am working on a MFC Document View architecture application which has multiple documents and views and a tabbed window interface.
I have been tasked with making an automatic switch to another tab on the press of the OK button in one of the other tabs. When the other tab is clicked on it uses a C++ wrapper over IWebBrowser2 to navigate to a specific web page.
When this is done manually by clicking on the tab everything is fine and the webpage within the view loads successfully.
In my first attempt at doing this the tab successfully switched in response to a call to
AfxGetMainWnd()->SendMessageToDescendants(SOME_MESSAGE, ...);
however by sending this windows message at the wrong point the application would crash once control returned because the chain of events caused the (modeless) dialog (*) that
sent the message, to no longer exist.
I then found the correct place to make the call, but now when the other tab is activated, it no longer displays the webpage as it should.
To debug this problem I added code to check the READYSTATE in both the situation where it works and the situation where it does not. When the page fails to load (despite the call to Navigate2 returning S_OK), the READYSTATE just stays at READYSTATE_LOADING.
Unfortunately now I am to many edits away from when I had it partially working.
I have added TRACE statements to the most obvious events such as OnSetFocus, CView::OnActivateView but all traces come out in the same order despite the behaviour being different
* hosted in the view