问题描述
我想使用代码本身确定的参数在我的代码中调用 Windows 程序.
I would like to call a windows program within my code with parameters determined within the code itself.
我不是要调用外部函数或方法,而是要调用 WinXP 环境中的实际 .exe 或批处理/脚本文件.
I'm not looking to call an outside function or method, but an actual .exe or batch/script file within the WinXP environment.
C 或 C++ 将是首选语言,但如果使用任何其他语言(ASM、C#、Python 等)更容易做到这一点,请告诉我.
C or C++ would be the preferred language but if this is more easily done in any other language let me know (ASM, C#, Python, etc).
推荐答案
当您调用 CreateProcess()、System() 等时,请确保将文件名字符串(包括命令程序文件名)用双引号括起来,以防万一文件名和/或完全限定的路径有空格,否则文件名路径的部分将被命令解释器解析为单独的参数.
When you call CreateProcess(), System(), etc., make sure you double quote your file name strings (including the command program filename) in case your file name(s) and/or the fully qualified path have spaces otherwise the parts of the file name path will be parsed by the command interpreter as separate arguments.
system(""d:some path\program.exe" "d:\other path\file name.ext"");
对于 Windows,建议使用 CreateProcess().它的设置更混乱,但您可以更好地控制进程的启动方式(如 Greg Hewgill 所述).为了快速和肮脏,您还可以使用 WinExec().(system() 可移植到 UNIX).
For Windows it is recommended to use CreateProcess(). It has messier setup but you have more control on how the processes is launched (as described by Greg Hewgill). For quick and dirty you can also use WinExec(). (system() is portable to UNIX).
启动批处理文件时,您可能需要使用 cmd.exe(或 command.com)启动.
When launching batch files you may need to launch with cmd.exe (or command.com).
WinExec("cmd "d:some path\program.bat" "d:\other path\file name.ext"",SW_SHOW_MINIMIZED);
(或者 SW_SHOW_NORMAL
如果你想显示命令窗口).
(or SW_SHOW_NORMAL
if you want the command window displayed ).
Windows 应该在系统 PATH 中找到 command.com 或 cmd.exe,因此不需要完全限定,但如果您想确定您可以使用 CSIDL_SYSTEM
(不要简单地使用 C:Windowssystem32cmd.exe).
Windows should find command.com or cmd.exe in the system PATH so in shouldn't need to be fully qualified, but if you want to be certain you can compose the fully qualified filename using CSIDL_SYSTEM
(don't simply use C:Windowssystem32cmd.exe).
这篇关于如何调用带有参数的外部程序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!