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);
}
else
{
CComQIPtr<IHTMLDocument2> htmlDoc = disp;
if (htmlDoc == NULL)
{
MessageBox(NULL, _T("Unable to get IHTMLDocument2"), _T("Error"), 0);
}
else
{
htmlDoc->get_title(&locationName);
}
}
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.