问题描述
我想使用代码本身确定的参数在我的代码中调用 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).
这篇关于如何调用带有参数的外部程序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!


大气响应式网络建站服务公司织梦模板
高端大气html5设计公司网站源码
织梦dede网页模板下载素材销售下载站平台(带会员中心带筛选)
财税代理公司注册代理记账网站织梦模板(带手机端)
成人高考自考在职研究生教育机构网站源码(带手机端)
高端HTML5响应式企业集团通用类网站织梦模板(自适应手机端)