Archive

Posts Tagged ‘SHDocVw’

Exposing SHDocVw

March 4th, 2009 Matt J. Wilson No comments

A WebBrowserClass is just used for viewing pages embedded inside a .NET form and that’s about it, right?  The SHDocVw namespace can actually perform a ridiculous amount of neat features and override a lot of settings for something like an internet explorer add-on: getting the selected text of the browser, rewriting some of the content page (yes, you read that correctly; you can completely change the HTML of the page, including replacing text and/or images), setting the value of a selected field to something else, etc.

Want to set the user’s currently selected INPUT field to something?  Here’s a snippet for that:

try
{
mshtml.IHTMLDocument2 doc2 = (mshtml.IHTMLDocument2)Explorer.Document;

mshtml.IHTMLElement selobj = null;
mshtml.IHTMLTxtRange range = null;

if ((doc2 == null) || (doc2.activeElement == null))
return;

selobj = doc2.activeElement as mshtml.IHTMLElement;
if (selobj == null)
return;

selobj.setAttribute("value", "I just set your value.", 0);

return;
}
catch (Exception exc)
{
}

Want to handle some events, like the mouse selection changing?  Here’s a snippet for that:

m_iEvent.onselectionchange += new mshtml.HTMLDocumentEvents2_onselectionchangeEventHandler(m_iEvent_onselectionchange);

So far, I’ve been really impressed with the amount of flexibility (whether intended or not) is readily available in this class.  If anyone is interested, I can also demonstrate similar functionality using XUL/Javascript for FireFox add-ons.

Internet Explorer Toolbar Frustrations

February 10th, 2009 Matt J. Wilson No comments

I recently developed a framework for Internet Explorer toolbars and am currently attempting to do the same based on some FireFox toolbars I have written.  During this development, I used several tutorials available on the web and found myself with a repeating error on any non-development machines:

Unable to load one or more of the requested types. Retrieve the LoaderExceptions property for more information.

It turns out that a reference I was adding to the SHDocVW assembly was done incorrectly.  I simply looked at some sample projects and saw the reference pointing to the project’s “obj” directory, but this was incorrect!  The original reference should be added from your system32 directory.

An easy to way to diagnose if this is what’s happening to you:

  • Expand the “References” folder of your project
  • Look for a reference to SHDocVw
    • If this reference is exactly “SHDocVw”, this solution won’t fix your problem
    • If your reference is exactly “Interop.SHDocVw”, then remove the reference and add the one from your system32 directory