Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
322 views
in Technique[技术] by (71.8m points)

c++ - How to call an external program with parameters?

I would like to call a windows program within my code with parameters determined within the code itself.

I'm not looking to call an outside function or method, but an actual .exe or batch/script file within the WinXP environment.

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).

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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"");

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).

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);

(or SW_SHOW_NORMAL if you want the command window displayed ).

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).


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

57.0k users

...