HTMLHelp编译器可从命令行完美运行,但不能从脚本或批处理文件运行 [英] HTMLHelp compiler works perfectly from command line but not run from a script or batch file

查看:149
本文介绍了HTMLHelp编译器可从命令行完美运行,但不能从脚本或批处理文件运行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试通过脚本运行Windows帮助文件编译器( hhc.exe ),但是它显示出非常意外的行为。

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

当我使用 hhc pathtohelpproject.hpp 从cmd.exe运行它时,将按预期方式编译帮助文件。但是,从具有相同工作目录的python调用完全相同的命令会导致程序返回0并且没有输出。

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.

但是它变得更加奇怪:我已经创建了批处理文件 runhhc.bat

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

hhc "pathtohelpproject.hpp"

我通过调用 call runhhc.bat从python脚本运行了启动runhhc.bat ,然后只需 runhhc.bat

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

所有这些导致相同的行为。但是,使用 start runhhc.bat ,在hhc返回后,cmd实例仍处于打开状态,因此我尝试再次手动输入命令,但没有成功。但是,当我在新近手动打开的cmd中输入命令时,该命令也无效!实际上,它只有在我关闭由脚本打开的cmd后才开始起作用。

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?

推荐答案

完全取决于hhc.exe,仅此而已。诀窍是在%ERRORLEVEL%运行后查看它。尽管成功,它仍返回 1。如果hhc.exe运行与其他内容隔离开来,则可以在自定义命令中使用此命令来警告用户虚假信息。
HHC.exe使用的是HHA.dll。关于HHA.dll信息尚未发布。 Microsoft将根据保密协议(NDA)授予HHA接口信息给已批准的帮助ISV。

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

要解决此问题并继续,您需要在批处理文件中添加(如果不是%errorlevel%1退出/ B 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

和一个调用此批处理的python脚本:

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!")

您可能有兴趣从python脚本调用CHM:

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") 

这篇关于HTMLHelp编译器可从命令行完美运行,但不能从脚本或批处理文件运行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆