New MSDN BHO Article - Building Browser Helper Objects with Visual Studio 2005

This article has prescriptive advice on how to build a reliable BHO the way the IE designed them to be built. This was written by 2 members of the IE development team and has an easy step-by-step guide using Visual Studio 2005. Even if you are new to COM/ATL, you will find this article valuable.

 

http://msdn.microsoft.com/library/default.asp url=/library/en-us/IETechCol/cols/dnexpie/expie_hello_bho.asp< xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />




Answer this question

New MSDN BHO Article - Building Browser Helper Objects with Visual Studio 2005

  • Mike36

    Igor Tandetnik wrote:
    > "If the page has no frames, the event is fired once after the page is
    > ready, but before any script has run."
    >
    > This statement is misleading at best. Scripts run as soon as the
    > <script> tag is encountered (except when it has DEFER attribute), which
    > naturally happens before the page is fully loaded. The timing described
    > in the article is simply impossible.
    >
    >
    > "... those that fire DownloadBegin will also fire a corresponding
    > DocumentComplete."
    >
    > DocumentComplete is not paired with DownloadBegin - DownloadComplete is.
    > When the user clicks Refresh, DownloadBegin and DownloadComplete events
    > fire, but DocumentComplete does not. And of course the user may hit Stop
    > button in the middle of download and prevent DocumentComplete from
    > firing.
    > --
    > With best wishes,
    >     Igor Tandetnik

    My explanation obviously needs some work.

    You are right, of course, that inline script is executed as the page is loaded. Even script inside of source files that have been marked DEFER will execute before the BHO because they are loaded after the download, and before it the document is complete. What I meant to say is that the DocumentComplete event occurs before script handlers for the window.onload event.

    Although my comment about the DownloadBegin event is technically correct, it can be misleading. The DownloadBegin event indicates that the page is transferring from the server; it is always followed by a corresponding DownloadComplete event. These events occur in pairs until the page and all its frames have been downloaded, then comes a DocumentComplete.

    In certain cases, DownloadComplete occurs by itself. This happens when you click the Refresh button but the document has not changed. Note that there is no DocumentComplete event in this case. It is safe to say, however, that if you receive a DownloadBegin event you will also receive a DocumentComplete because the document has been reloaded. Of course, all bets are off if Stop is clicked.

    Thanks again for your comments.



  • Mitch5713

    Sticking to the article and copy pasting the sample code lead me to the error:

    LIBID_TestBho1Lib undeclared identifier.

    Changing
    public IDispatchImpl<IHelloWorldBHO, &IID_IHelloWorldBHO, &LIBID_TestBho1Lib, /*wMajor =*/ 1, /*wMinor =*/ 0>,

    to:
    public IDispatchImpl<IHelloWorldBHO, &IID_IHelloWorldBHO, &LIBID_HelloWorldLib, /*wMajor =*/ 1, /*wMinor =*/ 0>,

    seems to do the trick.

    best regards
    Daniel

  • coolarian

    I would like to suggest that if they wanted to assist the development community to the maximum extent that they should build an example of a BHO interfacing with a ToolBar built in .Net managed code that talks with a multi-threaded singleton on the client that in turn talks with server assemblies using WCF.

    Now that would really help because that's what I am trying to build right now. I've got the BHO working invisibly now but the rest is yet to go.

    Regards,

    Gary Blakely

    www.deanblakely.com


  • Pavel Nechai

    Igor Tandetnik wrote:
    > Another bug:
    >
    > public IDispEventImpl<1, CHelloWorldBHO, &DIID_DWebBrowserEvents2, &LIBID_SHDocVw, 1, 0>
    >
    > should be
    >
    > public IDispEventImpl<1, CHelloWorldBHO, &DIID_DWebBrowserEvents2, &LIBID_SHDocVw, 1, 1>
    >
    > The version number of SHDocVw TLB is 1.1, not 1.0.
    >
    >
    >
    > STDMETHOD(OnDocumentComplete)(IDispatch *pDisp, VARIANT *pvarURL);
    >
    > OnDocumentComplete does not need to be virtual, and doesn't need to
    > return HRESULT (STDMETHOD macro makes it do both). The correct
    > declaration is
    >
    > void STDMETHODCALLTYPE OnDocumentComplete(IDispatch *pDisp, VARIANT *pvarURL);
    >
    > --
    > With best wishes,
    > Igor Tandetnik

    Great feedback. Again, you're absolutely correct.

    The OnDocumentComplete method does not need to be virtual, nor is the sink map entry (SINK_ENTRY_EX) set up to return anything other than void. Even if we tried to return a value, the ATL InvokeFromFuncInfo method throws it out, and IE ignores values returned from Invoke anyway.



  • RFDID

    Igor Tandetnik wrote:
    > The article has a serious bug. It shows this RGS fragment:
    >
    > HKLM {
    > SOFTWARE {
    > Microsoft {
    > Windows {
    > CurrentVersion {
    > Explorer {
    > 'Browser Helper Objects' {
    > ForceRemove '{D2F7E1E3-C9DC-4349-B72C-D5A708D6DD77}' = s 'HelloWorldBHO' {
    > val 'NoExplorer' = d '1'
    > }
    > }
    > }
    > }
    > }
    > }
    > }
    > }
    >
    > This MUST be
    >
    > HKLM {
    > NoRemove SOFTWARE {
    > NoRemove Microsoft {
    > NoRemove Windows {
    > NoRemove CurrentVersion {
    > NoRemove Explorer {
    > NoRemove 'Browser Helper Objects' {
    > ForceRemove '{D2F7E1E3-C9DC-4349-B72C-D5A708D6DD77}' = s 'HelloWorldBHO' {
    > val 'NoExplorer' = d '1'
    > }
    > }
    > }
    > }
    > }
    > }
    > }
    > }
    >
    > Note NoRemove modifiers. Without them, unregistering the object would
    > remove entrire HKLM\SOFTWARE hive and make the OS installation unusable.
    > --
    > With best wishes,
    > Igor Tandetnik

    Thanks for the heads-up, Igor.

    Although the behavior you speak of has not been a problem since Windows 98, it is good coding practice to indicate which keys are to be kept and which are to be removed upon uninstall. For the record, the keys will not be removed unless they are empty; so, even in the worst case scenario, the "Browser Helper Objects" might be the only key that would be removed unnecessarily, and that only if no other BHOs were installed. Not such a serious problem after all.

    In any case, I will make sure the sample is updated.



  • New MSDN BHO Article - Building Browser Helper Objects with Visual Studio 2005