Lazarus

Programming => General => Topic started by: dculp on January 28, 2017, 12:52:55 pm

Title: chm help file hangs after calling Application.Initialize
Post by: dculp on January 28, 2017, 12:52:55 pm
I'm calling a chm help file with
Code: Pascal  [Select][+][-]
  1. ZResult:= HtmlHelpA(0,pchar(helpfilename),HH_DISPLAY_TOPIC,ptruint(pchar(htmltopic)));
  2.  

The chm file loads and runs as expected. If the above call is preceded by Application.Initialize then the chm file still loads. However, attempting a search in the chm Search tab then causes the chm window to hang (hourglass - "not responding"). The test program below shows this problem (see comments there). (The test program should be run with the attached Small_demo_1a.chm file.)

Notes:
1. If you start  Small_demo_1a.chm by double-clicking on it then the chm Search runs OK. Therefore, the problem doesn't seem to be with the chm file itself.
2. Application.Initialize is not needed for this test program. However, it is needed for the actual application in order to use Windows Open/Save dialogs.

Lazarus 1.6 (FPC 3.0.0)
Windows 7 Pro

Thanks for any suggestions.
Don C.

Code: Pascal  [Select][+][-]
  1. Program CHM_help_21a;
  2. {Small example/test of the html help OCX.
  3.  Marco van de Voort (C) 2009 - heavily modified}
  4.  
  5. {$mode objfpc}{$H+}
  6.  
  7. uses
  8.   Forms, Interfaces,  // Requires  LCL package (not LCLbase)
  9.         // Above are required for Application.Initialize & exception handling
  10.  
  11.   htmlhelp, { \Lazarus\fpc\3.0.0\source\packages\winunits-base\src\htmlhelp.pp }
  12.   crt;
  13.  
  14. var
  15.    HelpfileName: AnsiString = 'Small_demo_1a.chm';
  16.    htmltopic   : AnsiString = 'Subtopic1.htm'; // the extension must be htm, not html
  17.    ZResult: Integer;
  18.  
  19. begin
  20. clrscr;
  21. writeln('Test for CHM help Search.');
  22. writeln('When the CHM test file loads, access the Search tab.');
  23. writeln('Enter any search phrase and click List Topics.');
  24. writeln('The CHM file will hang due to Application.Initialize.');
  25. writeln('However, this window will remain active.');
  26. write('Press Enter to proceed ... '); readln;
  27.  
  28. Application.Initialize; // Causes CHM file to hang after executing a search in the Search tab
  29.  
  30. { Application.Initialize is needed for Dialogs.pas (e.g., -
  31.       Dialog:= TSaveDialog.Create(nil); // where var Dialog: TOpenDialog;
  32.       Dialog.Execute;
  33.   See http://forum.lazarus.freepascal.org/index.php/topic,34998.0.html }
  34.  
  35. ZResult:= HtmlHelpA(0,pchar(helpfilename),HH_DISPLAY_TOPIC,ptruint(pchar(htmltopic)));
  36.    { See \Lazarus\fpc\3.0.0\source\packages\winunits-base\src\htmlhelp.pp }
  37.  
  38. writeln;
  39. writeln('Showing topic ', htmltopic);
  40. writeln('ZResult = ', ZResult);
  41. Write('Press Enter (in console window) to exit ... '); readln;
  42. end.
  43.  

Title: Re: chm help file hangs after calling Application.Initialize
Post by: Thaddy on January 28, 2017, 02:39:22 pm
I would not use that code anymore, especially since MS deprecated its use (IONS AGO!!!).
It is not even provided in a standard Windows since Vista. It is a separate install/download.
For windows 10+ it is not even provided AT ALL....

But there is a really good CHM package in the standard distribution of FPC.
You will find it in packages/chm and it is also cross-platform.

Furthermore: that is really, really badly written code.
If you need application.initialize, call it as the first line.
You are also mixing GUI elements with a command line appilcation.

Oh well, some...

Anyway: just use the CHM package and move the application.initialize to its proper place.
And try to clean up your code.
Title: Re: chm help file hangs after calling Application.Initialize
Post by: dculp on January 28, 2017, 02:58:11 pm
I would not use that code anymore, especially since MS deprecated its use (IONS AGO!!!).
It is not even provided in a standard Windows since Vista. It is a separate install/download.
For windows 10+ it is not even provided AT ALL....

When you refer to "that code", is this the
HtmlHelpA(0,pchar(helpfilename),HH_DISPLAY_TOPIC,ptruint(pchar(htmltopic)));
or is it the
Application.Initialize?

Title: Re: chm help file hangs after calling Application.Initialize
Post by: marcov on January 28, 2017, 03:03:46 pm
I would not use that code anymore, especially since MS deprecated its use (IONS AGO!!!).
Yes, chm was deprecated in Vista, but the successor is limited and not open (requires fee/developer subscription the last time I checked), and I doubt you could name it.

So chm is still the default MS client help tech for applications

Quote
It is not even provided in a standard Windows since Vista. It is a separate install/download.
For windows 10+ it is not even provided AT ALL....

You are confusing .hlp with .chm. 

Title: Re: chm help file hangs after calling Application.Initialize
Post by: marcov on January 28, 2017, 03:26:44 pm
The chm file loads and runs as expected. If the above call is preceded by Application.Initialize then the chm file still loads. However, attempting a search in the chm Search tab then causes the chm window to hang (hourglass - "not responding"). The test program below shows this problem (see comments there). (The test program should be run with the attached Small_demo_1a.chm file.)

I can reproduce, but can't really say what causes it. It could be a bug in the MS helpviewer (due to an enormously spartan CHM), but also in lazarus. I would at the very least expand the chm to a more wellformed, or use one from an existing program, to test if it is lazarus/LCL or the chm
Title: Re: chm help file hangs after calling Application.Initialize
Post by: wp on January 28, 2017, 03:35:15 pm
For windows 10+ it is not even provided AT ALL....
This is not correct. I have Win10 and can open chm files by doubleclicking (I can't remember whether I had to install something additionally to the system, probably I had to...).  I also have some projects in which the chm files are opened correctly in a similar way as shown by the OP. And don't advertise the CHM package too much: although it has improved there are still lots of annoyances.
Title: Re: chm help file hangs after calling Application.Initialize
Post by: dculp on January 28, 2017, 04:11:08 pm
You are also mixing GUI elements with a command line appilcation.

I'm updating a character-based app from Turbo Pascal 7 so that it will run under Win7+. It will remain character-based (no panels, components, etc.). However, I want to use Windows File Open/Save dialogs. So, yes, I'm mixing GUI elements with my application. Hence, I need Application.Initialize.

But there is a really good CHM package in the standard distribution of FPC.
You will find it in packages/chm and it is also cross-platform.

I'm using HelpNDoc (commercial) and the MS HTML Help Compiler (hhc.exe) to generate the chm file. Will the FPC CHM package help me beyond this? If so, can you point me to the specific code?

Do you think there's a problem with the following?
   HtmlHelpA(0,pchar(helpfilename),HH_DISPLAY_TOPIC,ptruint(pchar(htmltopic)));
If not, what might cause the chm window to hang when Application.Initialize is called before the above? Could I need a new or different compiler directive?


Title: Re: chm help file hangs after calling Application.Initialize
Post by: dculp on January 28, 2017, 04:40:08 pm
The chm file loads and runs as expected. If the above call is preceded by Application.Initialize then the chm file still loads. However, attempting a search in the chm Search tab then causes the chm window to hang (hourglass - "not responding"). The test program below shows this problem (see comments there). (The test program should be run with the attached Small_demo_1a.chm file.)

I can reproduce, but can't really say what causes it. It could be a bug in the MS helpviewer (due to an enormously spartan CHM), but also in lazarus. I would at the very least expand the chm to a more wellformed, or use one from an existing program, to test if it is lazarus/LCL or the chm

I get the same result when I use Avast5_1033.chm (Avast antivirus file, 1/2/2017) so the problem doesn't seem to be with the chm files.
Title: Re: chm help file hangs after calling Application.Initialize
Post by: wp on January 28, 2017, 05:08:20 pm
Instead of calling HtmlHelp function from a console program I'd prefer to open the file in hh.exe by means of a separate process. The next code works fine, please copy rtl.chm from the Lazarus/Docs/chm folder to the folder of the following demo:

Code: Pascal  [Select][+][-]
  1. program project1;
  2.  
  3. {$mode objfpc}{$H+}
  4.  
  5. uses
  6.   SysUtils, Process, Classes;
  7.  
  8. const
  9.   CHMFile = 'rtl.chm';
  10.   TOPIC = 'index';
  11.  
  12. var
  13.   prc: TProcess;
  14.  
  15. begin
  16.   prc := TProcess.Create(nil);
  17.   try
  18.     prc.CommandLine := Format('hh.exe mk:@MSITStore:%s::/system/%s.html', [CHMFile, TOPIC]);
  19.     prc.Options := prc.Options + [poWaitOnExit];
  20.     prc.Execute;
  21.   finally
  22.     prc.Free;
  23.   end;
  24. end.

See this (http://wiki.freepascal.org/Executing_External_Programs#TProcess (http://wiki.freepascal.org/Executing_External_Programs#TProcess)) for more details on TProcess.

If you want to go cross-platform then you should use the Lazarus CHM viewer, it is called lhelp and is in folder comonents/chmhelp/lhelp. Load the project into Lazarus and compile it (it requires that you have installed the packages lhelpcontrolpkg.lpk and TurboPowerIpro.lpk). ATM I don't know the calling convention how to open a specific topic. But adding the chm file name as a parameter opens the specific chm file at least.

P.S.
HelpNDoc is great!
Title: Re: chm help file hangs after calling Application.Initialize
Post by: dculp on February 02, 2017, 03:02:17 am
Instead of calling HtmlHelp function from a console program I'd prefer to open the file in hh.exe by means of a separate process. The next code works fine ..

Yes, this will open the chm file. However, is there any way to link a topic in my application to a particular topic in the chm? (This is an absolute requirement for my application.) This should preferably be done without having to re-invoke the chm each time a new topic is linked. Otherwise, the chm's forward and back buttons wouldn't work and the Contents tab would revert to its default state. However, this might not be totally unacceptable.

If you want to go cross-platform then you should use the Lazarus CHM viewer, it is called lhelp and is in folder comonents/chmhelp/lhelp. Load the project into Lazarus and compile it (it requires that you have installed the packages lhelpcontrolpkg.lpk and TurboPowerIpro.lpk). ATM I don't know the calling convention how to open a specific topic. But adding the chm file name as a parameter opens the specific chm file at least.

P.S.
HelpNDoc is great!

Unfortunately, when I try to open any HelpNDoc chm (including HelpNDoc's own chm help file) I get a Lhelp error "Resource unavailable:ms-its:". This seems to be a known problem. Search "unavailable" here --
http://forum.lazarus-ide.org/index.php?topic=30663.105
http://forum.lazarus.freepascal.org/index.php?topic=30663.110;wap2
Interestingly, I tried several non-HelpNDoc chms and none have this problem. However, I'm wedded to HelpNDoc at this point.
Title: Re: chm help file hangs after calling Application.Initialize
Post by: avra on February 02, 2017, 08:31:55 am
Yes, this will open the chm file. However, is there any way to link a topic in my application to a particular topic in the chm?
Have you tried to change constant TOPIC = 'index' to something else?
Title: Re: chm help file hangs after calling Application.Initialize
Post by: dculp on February 02, 2017, 11:11:55 am
Yes, this will open the chm file. However, is there any way to link a topic in my application to a particular topic in the chm?
Have you tried to change constant TOPIC = 'index' to something else?

I don't know what topics (specific topic names) are available in rtl.chm. Instead I have tried each of the following in project1 (WP's post on 1/28) using Small_demo_1a.chm (from my original post) for the topic Subtopic1 --
   TOPIC = 'Subtopic1.html';
   TOPIC = 'Subtopic1.htm';
   TOPIC = 'Subtopic1';
   TOPIC = 'index'; // just to see effect
   TOPIC = ''; // just to see effect
In all cases when the chm loaded it reported "This page can't be displayed". This happens whether the chm initially opens to the Contents tab or the Search tab. However, once the chm has loaded this topic will display correctly by clicking on it in Contents tab (under Introduction).
Title: Re: chm help file hangs after calling Application.Initialize
Post by: balazsszekely on February 02, 2017, 11:18:25 am
I did run a few tests and apparently you need a dummy window. So create one, pass the handle to HtmlHelpA and you're good to go. More over you can hide or AlphaBlend it, so visually the result is the same as in your original project.
Title: Re: chm help file hangs after calling Application.Initialize
Post by: dculp on February 02, 2017, 11:44:50 am
I did run a few tests and apparently you need a dummy window. So create one, pass the handle to HtmlHelpA and you're good to go. More over you can hide or AlphaBlend it, so visually the result is the same as in your original project.

For the tests that you ran, are you referring to my original code (where the chm hangs upon searching) or WP's code (trying to load a specific help topic)?
How do I create a dummy window? (Note that my application is non-GUI if this makes any difference.)
How would I hide the dummy window?
Could you provide some demo code (much appreciated)?
Title: Re: chm help file hangs after calling Application.Initialize
Post by: wp on February 02, 2017, 11:56:38 am
In all cases when the chm loaded it reported "This page can't be displayed". This happens whether the chm initially opens to the Contents tab or the Search tab.
I don't know, but did you enter the topic correctly? Sometimes the extension is .html, sometimes only .htm. And in my sample there is a path "/system/" in front of the topic name. Please compile the program chmls (in fpc/packages/chm). Run it with the chm file as a parameter and it will list you all contained topics along with extension and path.

[EDIT]
I played with my demo a bit and modified it such that it accepts filename and topic as parameter:
Code: Pascal  [Select][+][-]
  1. program showchm;
  2.  
  3. {$mode objfpc}{$H+}
  4.  
  5. uses
  6.   SysUtils, Process, Classes;
  7.  
  8. var
  9.   prc: TProcess;
  10.   chm: String;
  11.   topic: String;
  12.   s: String;
  13.  
  14.   procedure Syntax;
  15.   begin
  16.     WriteLn('USAGE:    showchm chmfile [topic incl path and extension]');
  17.     WriteLn('EXAMPLE:  showchm rtl.chm    ---> shows index.html');
  18.     WriteLn('          showchm rlt.chm /x86/writeportw.html');
  19.     Halt;
  20.   end;
  21. begin
  22.   if ParamCount = 0 then
  23.     Syntax;
  24.  
  25.   chm := ParamStr(1);
  26.   if ParamCount = 1 then
  27.     topic := '/system/index.html'
  28.   else begin
  29.     topic := ParamStr(2);
  30.     if topic[1] <> '/' then topic := '/' + topic;
  31.   end;
  32.  
  33.   prc := TProcess.Create(nil);
  34.   try
  35.     s := Format('hh.exe mk:@MSITStore:%s::%s', [chm, topic]);
  36.     prc.CommandLine := s;
  37.     prc.Options := prc.Options + [poWaitOnExit];
  38.     prc.Execute;
  39.   finally
  40.     prc.Free;
  41.   end;
  42. end.  
Call it with the filename of the chm file (can contain path) and with the topic including its path and extension (see output of chmls). It works with the chm files coming with Lazarus, as well as with those created by HelpNDoc.
Title: Re: chm help file hangs after calling Application.Initialize
Post by: balazsszekely on February 02, 2017, 11:57:36 am
Quote
Note that my application is non-GUI if this makes any difference.
First I converted your application to a GUI and it worked flawlessly, then I created a form in your console application to check if it makes a difference, apparently it does. See attachment.
Title: Re: chm help file hangs after calling Application.Initialize
Post by: dculp on February 02, 2017, 01:21:30 pm
In all cases when the chm loaded it reported "This page can't be displayed". This happens whether the chm initially opens to the Contents tab or the Search tab.
I don't know, but did you enter the topic correctly? Sometimes the extension is .html, sometimes only .htm.
Yes, I tried both .html, .htm, and no extension.

Please compile the program chmls (in fpc/packages/chm). Run it with the chm file as a parameter and it will list you all contained topics along with extension and path.

chmls compiled but with 7 warnings. I copied it to the folder with rtl.chm and then ran it from a Windows cmd window --

>chmls list rtl.chm
chmls, a CHM utility. (c) 2010 Free Pascal core.

An unhandled exception occurred at $000000010001AB49:
EFOpenError: Unable to open file "rtl.chm"
  $000000010001AB49
  $000000010001A9F3
  $0000000100003015 line 273 of chmls.lpr --> Stream := TFileStream.Create(name, fmOpenRead);
  $00000001000083B1 line 1025 of chmls.lpr --> ListChm(localparams[0],Section);
  $0000000100008986
  $0000000100017663
  $0000000100017FB1
  $00000000774459CD
  $000000007757B891

Same result with or without the "list" command. Also, same result with Small_demo_1a.chm. Doesn't run without the chm extension.
I double checked to make sure that chmls.exe and the chm files are in the same folder.
Title: Re: chm help file hangs after calling Application.Initialize
Post by: wp on February 02, 2017, 01:41:49 pm
chmls belongs to fpc - I use fpc3.0, you use fpc 3.0. I checked both 32 and 64 bit versions -- no problem. The only idea that comes to my mind is that of a typo or that the rtl.chm could still be open from previous experiments. If you don't see anything obvious in the task manager reboot the computer to be sure that rtl.chm is closed, then retry.
Title: Re: chm help file hangs after calling Application.Initialize
Post by: Daniel Simoes on May 17, 2018, 06:29:27 pm
Unfortunately, when I try to open any HelpNDoc chm (including HelpNDoc's own chm help file) I get a Lhelp error "Resource unavailable:ms-its:". This seems to be a known problem. Search "unavailable" here --

I got the same problem recently ...
Since I was generating CHM files myself with HelpNDoc, I was able to investigate...

It seems to me that LHelp does not like a CHM file that contains a topic whose "Help Context" is less than "1" ...
After assigning a number to the Topic that was showing in the LHelp error message, the problem stopped
Title: Re: chm help file hangs after calling Application.Initialize
Post by: wp on May 17, 2018, 07:03:34 pm
Since I was generating CHM files myself with HelpNDoc, I was able to investigate...
Could you generate a simple CHM file (one topic only) with HelpNDoc with causes the trouble and upload it her (packed into a zip)? I don't see a reason why LHelp cannot be fixed to accept such a file.
TinyPortal © 2005-2018