Welcome to Pete Brown's 10rem.net

First time here? If you are a developer or are interested in Microsoft tools and technology, please consider subscribing to the latest posts.

You may also be interested in my blog archives, the articles section, or some of my lab projects such as the C64 emulator written in Silverlight.

(hide this)

Creating an Internet Explorer Add-in Toolbar Button using C++ and ATL: Update

Pete Brown - 27 June 2011

Earlier this year, I wrote about how to create an ATL Add-in Toolbar button for Internet Explorer. I've been using the add-in since then, but a week ago, it suddenly stopped working. Every time I'd try to use it, the LocationName, which should be the title, would come back as the URL.

While putting together the weekly roundup tonight, I decided to fix it. I tried debugging the LocationName, but 9/10 times, it came back as the URL. It wasn't a race condition or anything - I made sure to wait for the page to complete loading. Nope, it's something else I can't quite figure out. None of the pages used JavaScript to set the title, and the titles appeared in IE just fine.

So, I went about it a different way. Rather than using the IWebBrowser2::LocationName property, I decided to nav down to the document and grab the title from there. The relevant code change in the Exec method is here:

CComBSTR locationUrl;
CComBSTR locationName;

if (m_spWebBrowser->get_LocationURL(&locationUrl) != S_OK)
MessageBox(NULL, _T("Unable to get LocationURL"), _T("Error"), 0);

// Old Approach
//if (m_spWebBrowser->get_LocationName(&locationName) != S_OK)
// MessageBox(NULL, _T("Unable to get LocationName"), _T("Error"), 0);

// New approach, using the DOM
CComQIPtr<IDispatch> disp;
if (m_spWebBrowser->get_Document(&disp) != S_OK)
MessageBox(NULL, _T("Unable to get HTML Document IDispatch Object"), _T("Error"), 0);
CComQIPtr<IHTMLDocument2> htmlDoc = disp;

if (htmlDoc == NULL)
MessageBox(NULL, _T("Unable to get IHTMLDocument2"), _T("Error"), 0);

I'm having much better success with the add-in now. I have no idea what caused it to break to begin with (probably something in patch Tuesday?) but it's working now.

Keep in mind, I'm still a real n00b to modern C++ (I know, it'd be a stretch to call my code modern C++), so fire away if I did something dumb.

Updated source code attached.


Source Code and Related Media

Download /media/75391/blogurlsnaggerv02.zip
posted by Pete Brown on Monday, June 27, 2011
filed under:      

3 comments for “Creating an Internet Explorer Add-in Toolbar Button using C++ and ATL: Update”

  1. Simonsays:
    I ran your revised solution using Visual Studio 2010, and it works great. I have two questions though. First, how can I get this solution, together with its related icon, to appear in the 64-bit version of IE? Second, if I want to create something similar, how would I share it with others when there is no exe file made? Thanks.

Comment on this Post

Remember me