Program only runs when not run from a script or batch file, but works perfectly when typed in by hand in CMD
<p>I am trying to run the windows helpfile compiler (<code>hhc.exe</code>) from a script, but it shows very unexpected behaviour.</p>

<p>When I run it from cmd.exe with <code>hhc pathtohelpproject.hpp</code>, 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.</p>

<p>But it gets even more bizarre: I've created a batch file <code>runhhc.bat</code>:</p>

<pre><code>hhc "pathtohelpproject.hpp"
</code></pre>

<p>which I ran from the python script by invoking <code>call runhhc.bat</code>, <code>start runhhc.bat</code> and just <code>runhhc.bat</code>. </p>

<p>All of them resulted in the same behaviour. However, with <code>start runhhc.bat</code> 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.</p>

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

<p>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.</p>

<pre><code>D:\_working&gt;"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&gt;echo %errorlevel%
1
</code></pre>

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

<pre class="lang-batch prettyprint-override"><code>@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
</code></pre>

<p>And a python script calling this batch:</p>

<pre class="lang-python prettyprint-override"><code>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!")
</code></pre>

<p><a href="http://i.stack.imgur.com/0xyJ9.png" rel="nofollow"><img src="http://i.stack.imgur.com/0xyJ9.png" alt="enter image description here"></a></p>

<p>You may be interested in calling a CHM from a python script:</p>

<pre class="lang-python prettyprint-override"><code># 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") 
</code></pre>

python windows batch-file cmd chm