简体   繁体   English

在C ++中执行Shell命令

[英]Execute shell command in c++

I have a question regarding executing shell commands in c++. 我有一个关于在c ++中执行shell命令的问题。 I'm building an application in winforms, vs 2008. My application has a button, when clicked should decode a binary file to a .csv file. 我正在使用Winforms和2008建立一个应用程序。我的应用程序有一个按钮,单击该按钮应将二进制文件解码为.csv文件。 I can decode files by first going to the right directory (cd Test_Copy2) and then execute a command in the command prompt (java -jar tool.jar -b x.fit x.csv). 我可以先解码到正确的目录(cd Test_Copy2),然后在命令提示符下执行命令(java -jar tool.jar -b x.fit x.csv)来解码文件。 I tried a lot of different stuff but unfortunately got none to work! 我尝试了很多不同的东西,但不幸的是没有任何东西可以工作!

I tried using: 我尝试使用:

system,  _popen,  ShellExecute(NULL, L"open", L"C:\\WINDOWS\\system32\\cmd.exe ", L"java -jar Tool.jar -b x.fit x.csv", L"C:\\Test_Copy2", SW_SHOWNORMAL)

Can anyone please provide me with an example on how to do that? 谁能为我提供一个有关如何执行此操作的示例? I dont know where I'm going wrong, most of the time the command prompt opens but no command is executed! 我不知道我要去哪里错,在大多数情况下,命令提示符会打开,但不会执行任何命令!

If you really want to run the jar in a cmd.exe instance, you need to add one of the correct command line switches to cmd.exe for it to work the way you want it to: 如果您确实想在cmd.exe实例中运行jar,则需要向cmd.exe添加正确的命令行开关之一,以使其按您希望的方式工作:

/C      Carries out the command specified by string and then terminates
/K      Carries out the command specified by string but remains

For instance, your command string should be: 例如,您的命令字符串应为:

C:\\WINDOWS\\system32\\cmd.exe /c java -jar Tool.jar -b x.fit x.csv

You can use the system() function to execute shell commands. 您可以使用system()函数执行shell命令。 For example: system("DIR") executes the DIR command in the CMD shell. 例如: system("DIR")在CMD Shell中执行DIR命令。 The default directory at the start is the directory you're .exe file is located. 开始的默认目录是.exe文件所在的目录。 'system("PAUSE")` executes the PAUSE command. 'system(“ PAUSE”)`执行PAUSE命令。 The command/s you wannt to execute should be passed as a constant string to the function. 您希望执行的命令应作为常量字符串传递给函数。

Edit: 编辑:

For you paritcular program the syntax (IMO) would be: 对于您的同类程序,语法(IMO)为:

system("java -jar Tool.jar -b x.fit x.csv")

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM