Getting current URL of Internet Explorer 8 programatically - c++

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.

Related

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.

How to add multiple tray icons in the same application (winapi)?

It is possible for an application to have more than one tray icon? (kind of like thunderbird does: one for the main application and another when you receive an email and there's an envelope in the notification area)
I tried using ShellNotify_Icon twice in a row but simply return FALSE the second time.
I've also tried using two different NOTIFYICONDATA structures but still nothing. It just adds one.
I haven't been able to find anything in the documentation or on google saying if this is possible or not.
Any ideas?

Safety of using \ApprovedExtensionsMigration\ for Auto-BHO enabling?

I'm in the process of looking into auto-enabling an IE9 toolbar so the user does not need to click the enable button, and I noticed another company used the following method.
Delete the registry key HKCU\Software\Microsoft\Internet Explorer\ApprovedExtensionsMigration\
Create a new key under HKCU\Software\Microsoft\Internet Explorer\ApprovedExtensionsMigration\
Add my toolbar's CLSID string value HKCU\Software\Microsoft\Internet Explorer\ApprovedExtensionsMigration{XXXXXXX....-XXXX}
x. Internet Explorer 9 automatically enables my toolbar and proceeds as if the user had clicked enable.
I have not found any documentation for this method, but I believe the way this works is Internet Explorer 9 believes the toolbar is going from IE8->IE9, and enables the toolbar. Should this method be considered unsafe for use (too hacky and will instantly get flagged by anti-virus), or is this considered a valid way of auto-enabling a toolbar?
Also, does anyone know if this method works in Internet Explorer 10?
Can't find any resources about this method. Viruses will use this method for sure (see http://www.threatexpert.com/report.aspx?md5=bde1312b225ad987b57b1dbef0885817 which is an identified virus that do exactly that), but since this registry is deleted (or at least the Time Stamp value is changed) in legitimate scenarios, it is possible that anti-viruses will not freak out, as they don't want to create false positives.
Regardless, what you are trying to do feels bad.
Also you should consider other users on the machine (not currently logged in).

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

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 :)

How to practically customize IE context menu?

I need to add one menu item in IE context menu. It is similar with Google customized context menu "Search Google for xxx" when you right click on IE.
I did a research and found that overriding IDocHostUIHandler::ShowContextMenu in a IE BHO can customized IE context menu. The sample project can be found in Popup blocker project published in codeproject. It works well and is easy to implement. However this approach has a problem. The problem is it will conflict with other add-ons' context menu customization per MSDN.
In MSDN Internet Explorer Center forum, there are some discussions about this topic. However there is not a proper implementation posted.
If anybody has experience on this, please share your idea. Thanks!
See Adding Entries to the Standard Context Menu on MSDN.
For some sample code, download and extract the Web Accessories for Internet Explorer 5 (the mechanism still works in IE versions up to and including 8). Look at ie5wa.inf and *.html for examples of various IE context menu extensions.
Note that the abive is achieved without COM, purely via one registry setting and a file containing a blob of script (Javascript, JScript, VBScript etc.) The script (JScript, VBScript) may in turn work with COM objects, should your menu action logic reside in such an object, e.g.
create a HKCU\Software\Microsoft\Internet Explorer\MenuExt\My &Context Menu" registry key (&` precedes the menu accelerator letter, if any)
set the default value for the key to point to a file containing the script code implementing the context menu action(s), possibly interacting with a COM object (must support IDispatch IIRC; also read up on the My Computer Security Zone)
create a binary contexts value (0x02 for images, 0x10 for selections, etc.) which regulates when the new context menu should be displayed
I customized my IE in order to navigate to a selected text. You can read the full story and get detailed source code here.
The hearth of my solution is built around the following javascript code and the Registry key [HKEY_CURRENT_USER\Software\Microsoft\Internet Explorer\MenuExt]:
var parentwin = external.menuArguments;
var doc = parentwin.document;
var sel = doc.selection;
var rng = sel.createRange();
var str = new String(rng.text);
if(0 < str.length)
{
if (str.indexOf("http")!=0)
window.open("http://"+str, "_blank");
else
window.open(str, "_blank");
}