如何通过一个换行符作为通过Windows的系统()函数的参数? [英] How to pass a line break as an argument via the system() function on Windows?

查看:136
本文介绍了如何通过一个换行符作为通过Windows的系统()函数的参数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

通常人们可以使用 ^ 来逃避命令行的字符。但我无法使其正常工作。

Usually one could use ^ to escape a character in the command-line. But I couldn't make it to work.

下面是一个测试程序,CommandArguments.exe,打印它得到的参数。

Here is a test program, CommandArguments.exe, which prints the arguments it got.

int main(int argc, char *argv[])
{
    int i, j;
    for (i = 0; i < argc; ++i) {
        printf("%d: %s\n", i, argv[i]);
        for (j = 0; j < strlen(argv[i]); ++j) {
            printf("  ");
            printf("%d ", argv[i][j]);
        }
        printf("\n");
    }
}

问题,LineBreakCommandArguments.exe,程序如下:

The program in question, LineBreakCommandArguments.exe, is as follow:

void main(void)
{
    system("CommandArguments ^\n"); // ^\n does not really pass a line break...
}

LineBreakCommandArguments.exe的输出:

The output of LineBreakCommandArguments.exe:

>LineBreakCommandArguments.exe
0: CommandArguments
  67   111   109   109   97   110   100   65   114   103   117   109   101   110   116   115

如上图所示,CommandArguments只拿到一个参数。线断字失踪了。

As shown above, CommandArguments got only one argument. The line-break character was missing.

所以,问题是,我应该怎么修改LineBreakCommandArguments,这样CommandArguments将获得换行符字符作为参数?

So the question is, how should I modify LineBreakCommandArguments, such that CommandArguments will get the line-break character as an argument?

请注意,我仍然想使用 系统 功能,因为我想用shell命令,比如 DIR / CD 的能力,使用时,这是不可 的CreateProcess

Note that I still would like to use the system function, since I want the ability to use shell commands like dir/cd, which are not available when using CreateProcess.

更新:

开价的原因是,我采取了一个API的编程语言( haXe的)。该API允许用户任意命令传递给shell。我的希望的人不需要任何参数使用换行符,但谁知道...无论如何,因为对于一个编程的API郎是一个潜在的大量的计划的基础,我想它以覆盖尽可能多的情况下,尽可能。如果是不可行的支持换行符的说法,我会是将其记录下来。

The reason of asking is that I'm implementing an API for a programming language (haxe). The API allows users to pass arbitrary command to the shell. I hope people wouldn't need to use line breaks in any argument, but who knows... Anyway, since an api for a programming lang is the basis for potentially a lots of programs, I want it to cover as many cases as possible. If it is not feasible to support line breaks in argument, I will document it as is.

推荐答案

OK,所以我做了一些测试,当您使用的CreateProcess它的工作。这告诉我们,C运行时的命令行分析器没有做任何事情与换行,这意味着问题必须得到相关的方式的cmd.exe (下称壳 )解析命令行。

OK, so I've done some testing and it does work when you use CreateProcess. That tells us that the C runtime's command line parser isn't doing anything with the linefeed, which means the problem must be related to the way cmd.exe ("the shell") parses command lines.

你不会找到在该来的官方消息。你可以期望的最好是找人谁反编译和反向工程了。至于我可以告诉实验,shell的解析器无条件地停止,一旦它看到一个换行处理命令行。这也是这个答案结束时达到

You're not going to find an official source on that. The best you could hope for is to find someone who has decompiled and reverse-engineered it. As far as I can tell experimentally, the shell's parser unconditionally stops processing the command line as soon as it sees a line feed. That is also the conclusion reached in this answer.

您最好的选择将是寻找一个换行符的字符串,如果你找到一个,使用CreateProcess的,而不是系统的()。为更大的一致性,使用CreateProcess的在任一情况下,但(为了支持内置壳命令)通过壳体传递命令,如果没有线路饲料:

Your best bet is going to be to search the string for a line feed and, if you find one, use CreateProcess instead of system(). For greater consistency, use CreateProcess in either case, but (in order to support built-in shell commands) pass the command through the shell if there is no line feed:

<insert-value-of-comspec-here> /s /c "<insert-command-string-here>"

更新:

在回答你的问题的更新,我会建议在您的情况,您的的治疗换行符作为一种特殊情况。假设有问题的API函数将被记录到命令传递给shell,而不是直接运行它们,程序员应该指望能够通过换行。当然,你可以preFER明确记录的事实,那完全是你的电话。 : - )

In response to the update in your question, I would recommend in your scenario that you don't treat line breaks as a special case. Assuming that the API function in question will be documented to pass commands to the shell rather than running them directly, programmers shouldn't expect to be able to pass line feeds. Of course you may prefer to document the fact explicitly, that's entirely your call. :-)

我不建议您提供的部分的方法来运行一个命令,而不是直接通过外壳,因为有优势情况下,要通过shell是不方便。这可能是两种不同的功能或选项。

I do recommend that you provide some method to run a command directly rather than via the shell, as there are edge cases where going via the shell is inconvenient. That might be either a different function or an option.

这篇关于如何通过一个换行符作为通过Windows的系统()函数的参数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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