如何调用带参数的外部程序? [英] How to call an external program with parameters?

查看:145
本文介绍了如何调用带参数的外部程序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想我的code中调用一个Windows程序与$ C $内确定C本身的参数。

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​​ ++将是preferred语言,但如果这是更容易在其他语言做让我知道(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的(),系统()等,确保你双引号的文件名字符串(包括命令程序的文件名)的情况下,你文件名(S)和/或完全合格的路径有空格,否则文件名路径的部分将被命令间preTER作为独立参数解析。

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()。
(系统()移植到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应找到command.com或在系统PATH CMD.EXE所以不应该需要是完全合格的,但如果你想成为某些可以使用的 CSIDL_SYSTEM (不要简单地使用C:\\ WINDOWS \\ SYSTEM32 \\ CMD.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:\Windows\system32\cmd.exe).

这篇关于如何调用带参数的外部程序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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