简体   繁体   English

HTMLHelp编译器可从命令行完美运行,但不能从脚本或批处理文件运行

[英]HTMLHelp compiler works perfectly from command line but not run from a script or batch file

I am trying to run the windows helpfile compiler ( hhc.exe ) from a script, but it shows very unexpected behaviour. 我正在尝试从脚本运行Windows帮助文件编译器( hhc.exe ),但是它显示出非常意外的行为。

When I run it from cmd.exe with hhc pathtohelpproject.hpp , the helpfile is compiled as expected. 当我使用hhc pathtohelpproject.hpp从cmd.exe运行它时,将按预期方式编译帮助文件。 However, invoking the exact same command from python with the same working directory result in the program returning 0 and no output. 但是,从具有相同工作目录的python调用完全相同的命令会导致程序返回0并且没有输出。

But it gets even more bizarre: I've created a batch file runhhc.bat : 但这变得更加奇怪:我创建了一个批处理文件runhhc.bat

hhc "pathtohelpproject.hpp"

which I ran from the python script by invoking call runhhc.bat , start runhhc.bat and just runhhc.bat . 我通过调用call runhhc.batstart runhhc.batrunhhc.bat从python脚本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. 但是,使用start runhhc.bat ,在hhc返回后cmd实例仍处于打开状态,因此我尝试再次手动输入命令,但没有成功。 But when I entered the command in a freshly, manually opened cmd, it also didn't work! 但是,当我在新近手动打开的cmd中输入命令时,该命令也无效! In fact, it only started working once I closed the cmd opened by my script. 实际上,它只有在我关闭脚本打开的cmd后才开始起作用。

What could be the explanation for this bizarre behaviour? 这种奇怪行为的解释可能是什么? And how can I run the compiler from a script regardless? 以及如何无论如何从脚本运行编译器?

It's totally down to hhc.exe, nothing else. 完全取决于hhc.exe,仅此而已。 The trick is to look at the %ERRORLEVEL% after it runs. 诀窍是在%ERRORLEVEL%运行后查看它。 It returns "1" despite success. 尽管成功,它仍返回“ 1”。 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运行与其他内容隔离开来,则可以在自定义命令中使用此命令警告用户虚假。 HHC.exe is using HHA.dll. HHC.exe使用的是HHA.dll。 About HHA.dll info has not been published. 关于HHA.dll信息尚未发布。 Microsoft grant the HHA interface information under non-disclosure agreement (NDA) to approved help ISV's. Microsoft根据保密协议(NDA)将HHA接口信息授予已批准的帮助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

To go around this and continue you need to add if not %errorlevel% 1 exit /B 1 in a batch file. 要解决此问题并继续,您需要在批处理文件中添加if not %errorlevel% 1 exit /B 1if 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

And a python script calling this batch: 和一个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!")

在此处输入图片说明

You may be interested in calling a CHM from a python script: 您可能对从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") 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 当我从 IDLE 运行但不在命令行中运行时,Python 脚本有效吗? - Python script works when I run from IDLE but not in command line? 从命令行运行时,Python脚本有效,但从Windows Service运行时,则无效 - Python Script works when run from command line but not when run from windows service 在命令行/批处理文件/python脚本中运行java main class - Run java main class in command line/batch file/python script py安装程序。 运行 > pyinstaller myapp.spec 从脚本工作不同于从命令行 - PyInstaller. Run > pyinstaller myapp.spec from script works diffrent than from command line 从C#运行py脚本但从Windows命令行运行时,“没有名为fcntl的模块” - “No module named fcntl” when py script run from c# but works from windows command line python 脚本在命令行中完美运行,但在 repl 上测试失败,它 - python script run perfectly in command line but failed test on repl,it 无法从批处理文件运行python脚本 - unable to run python script from Batch file 直接从命令行运行 python 脚本 - run python script directly from command line 从命令行运行 Plotly 脚本 - Run Plotly script from command line Python 脚本在 Sublime Text 中运行时不起作用,但在命令行中运行 - Python script doesn't work when run in Sublime Text, but works from command line
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM