我试图从脚本运行windows帮助文件编译器(hhc.exe),但它显示了非常意外的行为。
当我用hhc pathtohelpproject.hpp从cmd.exe运行它时,帮助文件被按照预期进行了编译。但是,从具有相同工作目录的python中调用完全相同的命令会导致程序返回0且不输出。
但是更奇怪的是:我创建了一个批处理文件runhhc.bat
hhc "pathtohelpproject.hpp"我从python脚本中通过调用call runhhc.bat、start runhhc.bat和runhhc.bat来运行它。
所有这些都导致了相同的行为。但是,使用start runhhc.bat时,cmd实例在hhc返回后仍处于打开状态,因此我再次尝试手动输入命令,但没有成功。但是当我在一个新的,手动打开的cmd中输入命令时,它也不起作用!事实上,只有在我关闭脚本打开的cmd之后,它才开始工作。
对这种奇怪的行为有什么解释呢?无论如何,我如何从脚本运行编译器?
发布于 2016-08-27 22:19:01
这完全取决于hhc.exe,没有其他的。诀窍是在%ERRORLEVEL%运行后查看它。即使成功,它也会返回"1“。如果hhc.exe运行与其他程序隔离,则可以在自定义命令中使用它来警告用户它是虚假的。HHC.exe正在使用HHA.dll。关于HHA.dll的信息尚未发布。微软根据保密协议(NDA)将HHA接口信息授予批准的help ISV。
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要绕过此问题并继续操作,您需要在批处理文件中添加if not %errorlevel% 1 exit /B 1。
@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脚本:
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感兴趣:
# 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") https://stackoverflow.com/questions/39150947
复制相似问题