简体   繁体   English

Windows上的C程序中的系统调用问题

[英]system call issue in C program on Windows

From within a C program I am trying to build a string that will be used in a system call: 从C程序中,我试图构建一个将在系统调用中使用的字符串:

   char myCommands[128];
   ...
   /* packing myCommands string */
   ..
   system(myCommands);

The command string to execute looks like this: 要执行的命令字符串如下所示:

  setEnvVars.bat & xCmd.exe ...command-paramters...

If "...command-parameters..." does not contain any quote characters, all is well and statements succeed. 如果“ ... command-parameters ...”不包含任何引号字符,则表示一切正常,并且语句成功。

If "...command-parameters..." contains any quote characters I get this error: 如果“ ... command-parameters ...”包含任何引号字符,则会出现此错误:

  The filename, directory name, or volume label syntax is incorrect.

Example: 例:

  setEnvVars.bat & xCmd.exe -e "my params with spaces"

Another odd thing, if I put the myCommands string into a *.bat file verbatim, quotes and all it works perfectly. 另一个奇怪的事情是,如果我将myCommands字符串逐字放入* .bat文件中,则用引号引起来的所有内容都可以正常工作。

What is "system(...)" doing different? “ system(...)”有什么不同?

== OK, More details == ==好,更多详细信息==

I have a simple program that demonstrates the problem. 我有一个演示该问题的简单程序。 This version does work: 此版本可以正常工作:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(void) {
    char cmdStr[1024];
    strcpy(cmdStr, "\"C:\\Windows\\system32\\cmd.exe\" /c echo nospaces & C:\\Windows\\system32\\cmd.exe /c echo moretext");
    printf("%s\n", cmdStr);
    system(cmdStr);
}

Outputs: 输出:

"C:\Windows\system32\cmd.exe" /c echo nospaces & C:\Windows\system32\cmd.exe /c echo moretext
nospaces
moretext

This does not work: 不起作用

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(void) {
    char cmdStr[1024];
    strcpy(cmdStr, "\"C:\\Windows\\system32\\cmd.exe\" /c echo nospaces & \"C:\\Windows\\system32\\cmd.exe\" /c echo moretext");
    printf("%s\n", cmdStr);
    system(cmdStr);
}

Outputs: 输出:

"C:\Windows\system32\cmd.exe" /c echo nospaces & "C:\Windows\system32\cmd.exe\" /c echo moretext
The filename, directory name, or volume label syntax is incorrect.

I think it may relate to the "cmd.exe /S" option, but trying to introduce that option does not change the behavior. 我认为它可能与“ cmd.exe / S”选项有关,但是尝试引入该选项不会改变行为。

The quotes around the cmd.exe path are not needed because there is no space, but in my target program I am trying to allow all installation paths, which may include "C:\\Program Files" 不需要cmd.exe路径周围的引号,因为没有空间,但是在我的目标程序中,我试图允许所有安装路径,其中可能包括“ C:\\ Program Files”

(A pox on the person that that thought having spaces in path names was a good idea.) (对那个认为路径名中有空格的人是个好主意。)

(And using single quotes does not change the behavior.) (并且使用单引号不会更改行为。)

After banging my head a while, I abandoned the "system(cmdLine)" approach and went with a "CreateProcess" call (this will run under Windows). 敲了一下头后,我放弃了“ system(cmdLine)”方法,并进行了“ CreateProcess”调用(它将在Windows下运行)。

Using CreateProcess I was able to side step the whole environment variable problem, which is what was leading me to try to use the "cmd1 & cmd2" syntax. 使用CreateProcess,我可以回避整个环境变量问题,这就是导致我尝试使用“ cmd1&cmd2”语法的原因。

CreateProcess will allow you to pass a different environment to the child process. CreateProcess将允许您将不同的环境传递给子进程。 I was able to figure out how to rewrite the environment and pass that to the child. 我能够弄清楚如何重写环境并将其传递给孩子。 This neatly solved my issues. 这很好地解决了我的问题。

My code to rewrite the environment may be a bit cumbersome but it works and seems fairly robust. 我重写环境的代码可能有点麻烦,但是它可以正常工作并且看起来很健壮。

As you probably know, single quotes are for one character, and double are for multiple... You also are aware that you cannot contain double quotes inside a string, or it exits the string and adds them (depending on the situation)... 如您所知,单引号用于一个字符,双引号用于多个字符...您还知道,字符串中不能包含双引号,否则它会退出字符串并添加(取决于具体情况)。 。

Try the following and tell what happens: 请尝试以下操作,并说明会发生什么:

system("setEnvVars.bat & xCmd.exe -e 'my params with spaces'");

system("setEnvVars.bat & xCmd.exe -e \"my params with spaces\"");

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

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