問題描述
我想使用代碼本身確定的參數(shù)在我的代碼中調(diào)用 Windows 程序.
I would like to call a windows program within my code with parameters determined within the code itself.
我不是要調(diào)用外部函數(shù)或方法,而是要調(diào)用 WinXP 環(huán)境中的實際 .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).
推薦答案
當(dāng)您調(diào)用 CreateProcess()、System() 等時,請確保將文件名字符串(包括命令程序文件名)用雙引號括起來,以防萬一文件名和/或完全限定的路徑有空格,否則文件名路徑的部分將被命令解釋器解析為單獨的參數(shù).
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().它的設(shè)置更混亂,但您可以更好地控制進(jìn)程的啟動方式(如 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 應(yīng)該在系統(tǒng) 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).
這篇關(guān)于如何調(diào)用帶有參數(shù)的外部程序?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!