0

I am trying to run the windows helpfile compiler (hhc.exe) from a script, but it shows very unexpected behaviour.

When I run it from cmd.exe with hhc pathtohelpproject.hpp, the helpfile is compiled as expected. However, invoking the exact same command from python with the same working directory result in the program returning 0 and no output.

But it gets even more bizarre: I've created a batch file runhhc.bat:

hhc "pathtohelpproject.hpp"

which I ran from the python script by invoking call runhhc.bat, start runhhc.bat and just runhhc.bat.

All of them resulted in the same behaviour. However, with start runhhc.bat the cmd instance was still open after hhc returned, so I tried entering the command manually again, without success. But when I entered the command in a freshly, manually opened cmd, it also didn't work! In fact, it only started working once I closed the cmd opened by my script.

What could be the explanation for this bizarre behaviour? And how can I run the compiler from a script regardless?

help-info.de
  • 6,695
  • 16
  • 39
  • 41
Lukas
  • 178
  • 1
  • 1
  • 11
  • Is `hhc` a batch file? if so you need `call` to run it from a batch file; anyway, you should specify full paths to `hhc` and to the `*.hpp` file... – aschipfl Aug 25 '16 at 17:08
  • hhc is an executable. I've tried both full paths and relative paths for both already, the result is the same! – Lukas Aug 25 '16 at 17:09
  • Try to find what's different in that orphaned `start` console from a normal one you manually open, for example by running `set` in both to display the environment and then compare the output (you can redirect each to a file and compare them with `comp`). – wOxxOm Aug 26 '16 at 10:03
  • You must specify the relative or absolute path to the .hhp file if compiling from a folder other than the one that contains this file. And you'll need to ensure that the HTML Help Workshop program folder is in the path, as that's where hhc.exe resides. "%programfiles%\HTML Help Workshop\hhc" d:\_temp\CHM-example.hhp "%programfiles%\HTML Help Workshop\hhc" d:\_temp\CHM-example.hhp > log.txt This command assumes that you have installed HTMLHelp Workshop to the default path, so you will have to modify this command line if you installed to a non-default path. – help-info.de Aug 26 '16 at 18:06

1 Answers1

0

It's totally down to hhc.exe, nothing else. The trick is to look at the %ERRORLEVEL% after it runs. It returns "1" despite success. This could be used in a custom command to warn the user it's spurious, if the hhc.exe run is isolated from other stuff. HHC.exe is using HHA.dll. About HHA.dll info has not been published. Microsoft grant the HHA interface information under non-disclosure agreement (NDA) to approved help ISV's.

D:\_working>"C:\Program Files (x86)\HTML Help Workshop\hhc" foobar.hhp
Microsoft HTML Help Compiler 4.74.8702

Compiling d:\_working\foobar.chm

...

Compile time: 0 minutes, 1 second
22      Topics
87      Local links
2       Internet links
0       Graphics

Created d:\_working\foobar.chm, 305,338 bytes
Compression decreased file by 53,639 bytes.

D:\_working>echo %errorlevel%
1

To go around this and continue you need to add if not %errorlevel% 1 exit /B 1 in a batch file.

@echo off
REM -----------------------------------------
REM batch file  is located in D:\_batch
REM HH project file is located in D:\_working
REM -----------------------------------------
cd ..\_working
echo '//--- HH Compiler start --------------------------------------
"C:\Program Files (x86)\HTML Help Workshop\hhc" foobar.hhp
echo '//--- HH Compiler end   --------------------------------------
echo '//--- errorlevel -------------------------------------------
echo %errorlevel%
echo '//------------------------------------------------------------
if not %errorlevel% 1 exit /B 1

And a python script calling this batch:

print ("*******************************")
print ("We compile a CHM help file  ...")
print ("*******************************")
# First import the 'ctypes' module. ctypes module provides C compatible data types and allows calling functions in DLLs or shared libraries.
import ctypes  # An included library with Python install.
# ctypes.windll.user32.MessageBoxW(0, "Open CHM", "Your title", 1) # OK only
messageBox = ctypes.windll.user32.MessageBoxA
# documentation: https://msdn.microsoft.com/en-us/library/windows/desktop/ms645505(v=vs.85).aspx
returnValue = messageBox(None,"Compile Help Module (CHM) now?","CHM and Python",1) # 1=OK Cancel, 2=Cancel, Retry, Ignore

if returnValue == 1:
    print("Pressed OK")
    # How to compile a chm file in Python?
    # ---------------------------------
    import os   
    os.system("D:/_batch/run-hhc.bat")

elif returnValue == 2: 
    print("user pressed cancel button!")

enter image description here

You may be interested in calling a CHM from a python script:

# How to open a chm file in Python?
# ---------------------------------
# os.system("hh.exe D:/UserData-QGIS-Python/Projekte/ConnectChm/CHM-example.chm")
import os 
os.system("hh.exe D:/UserData-QGIS-Python/Projekte/ConnectChm/CHM-example.chm::/garden/garden.htm") 
help-info.de
  • 6,695
  • 16
  • 39
  • 41
  • Thank you for your detailed answer, but I think you missed my actual question (did I maybe ask it badly?). I've managed to figure out that hhc.exe refuses to compile if it's run from a nested shell (aka the python script is run from a shell, and calls the subprocess with shell=True, so the batch file is run in another shell again). So I've worked around my problem by using pythonw.exe to run my script, so the subprocess call will be the first shell. Then hhc.exe runs perfectly fine with exactly the same command. – Lukas Aug 31 '16 at 08:01