Using HtmlHelp in a Delphi Active Form

I stumbled across a bug in the Delphi HtmlHelpViewer unit yesterday.  I had written an ActiveX control based on a Delphi form (active form) and I wanted to display the contents of an HtmlHelp file.  Sounded easy enough, just put HtmlHelpViewer in the uses list.  And that worked insofar as I could display the required help pages. But (isn’t there always a but) if the user didn’t display any help pages the application would hang when it tried to close.

Googling provided some hints as to what the problem might be but no solution to the problem.  It turns out there is a bug in Delphi’s HtmlHelpViewer unit.  It only calls HtmlHelp using the HH_INITIALIZE command if you try to display a help page.  Sounds reasonable, why would you want to initialize the help engine if you aren’t going to display any help, right?  Unfortunately, HtmlHelpViewer calls HtmlHelp using the HH_CLOSE_ALL command unconditionally when the application shuts down.  That means that it tries to close all open help windows without ever having initialized the Html help system.  In a normal application, this doesn’t cause any problems but if you try to do this from an ActiveX control, the application will hang.

After much messing around I finally found a workaround.  I simply put a call to HtmlHelp using the HH_INITIALIZE command in my ActiveX control’s start up code and the problem is gone.

There are references to this problem in Embarcadero’s Quality Central but they don’t seem to have done anything about it yet.

PS

This turned out to be not quite so easy as I thought.  In addition to placing an HH_INITIALIZE command in the active form’s OnCreate event handler (DO NOT put this in the initialization section of the unit because it won’t work) I also had to copy the HmtlHelpViewer to my project directory, add it to my project and make the following change:

procedure THtmlHelpViewer.SoftShutDown;
begin
if (FInitialized) then  // Only call HH_CLOSE_ALL if we are initialized
HtmlHelp(0, nil, HH_CLOSE_ALL, 0);
end;

For reasons that elude me neither fix on its own solves the problem but it you use both then it seems to work.

One Response to “Using HtmlHelp in a Delphi Active Form”

  1. flash-player Says:

    Nice!

Leave a Reply