【问题标题】:Alternative to system() and _popen() on Windows在 Windows 上替代 system() 和 _popen()
【发布时间】:2009-02-11 01:09:16
【问题描述】:

这与:How do I read the results of a system() call in C++?

我正在尝试做完全相同的事情,只是我的程序需要将“带空格的多个参数”传递给命令。我需要命令行输出和进程的退出代码。

示例:使用 Textpad 的示例。我真正使用的应用程序在标准输出上打印内容。


string command1 = "\"C:\Program Files\TextPad 5\Textpad.exe\" C:\readme0.txt";
string command2 = "\"C:\Program Files\TextPad 5\Textpad.exe\" \"C:\read me2.txt\"";
cout << system(command1.c_str()) << endl;
cout << system(command1.c_str()) << endl;

输出:

0 'C:\Program' 未被识别为内部或外部命令, 可运行的程序或批处理文件。

1

对系统的第一次调用通过,第二次调用失败并出现上述错误。 Windows 中的 _popen 在 Windows 上的工作方式类似,因此在那里没有帮助。我可以在 Linux 上轻松做到这一点,因为我可以在参数中转义空格而无需使用引号。

另一种方法是编写大量非跨平台代码,如下所示: http://msdn.microsoft.com/en-us/library/ms682499(VS.85).aspx

但如果我想避免这种情况,在 Windows 上是否有替代 system() 和 _popen() 的方法?

谢谢!

【问题讨论】:

  • 您是否打算将示例代码中的反斜杠设为单个?在 C 字符串中,您需要使用 "\\" 让 C 转义为单个反斜杠,但可能您知道这一点,并且 StackOverflow 格式化代码刚刚弄乱了您的示例......
  • 是的,我的双反斜杠已被单反斜杠替换。

标签: winapi


【解决方案1】:

_popen() 和 system() 使用的最低级别的 Windows API 函数是 CreateProcess()。

但是 CreateProcess() 使用起来并不简单——尤其是当您想要获取进程的输出或写入进程的输入时。

CreateProcess() 肯定适用于包含空格字符的文件名(只要它们按照您的方式写在引号中)。

【讨论】:

    【解决方案2】:

    下面解决路径中的空格问题。然而,捕捉命令的输出要困难得多:

    #include <string>
    #include <cstdlib>
    using namespace std;
    
    int main() {
        string cmd = "\"c:\\program files\\notepad++\\notepad++.exe\"";
        system( cmd.c_str() );
        return 0;
    }
    

    【讨论】:

      【解决方案3】:

      一堆实用程序库已经使用了那块不可移植的代码,并用一个可移植的接口将其包装起来。有关示例,请参阅 Qt 的 QProcess。

      【讨论】:

        【解决方案4】:

        我这样做(注意 - 这是 VB.NET 代码),所以我可以将命令的输出写入我的日志文件(它包含在 RunCommand() 方法中):

         Try
            Dim myprocess As New Process()
            myprocess.StartInfo.FileName = "C:\Program Files\TextPad 5\Textpad.exe"
            myprocess.StartInfo.RedirectStandardOutput = True
            myprocess.StartInfo.UseShellExecute = False
        
            ' inArgs are the arguments on the command line to the program
            myprocess.StartInfo.WindowStyle = ProcessWindowStyle.Hidden
            myprocess.StartInfo.Arguments = "C:\readme0.txt"
        
            ' the dir to set as default when the program runs
            Then myprocess.StartInfo.WorkingDirectory = "C:\Program Files\TextPad 5\"
        
            myprocess.Start()
        
            ' grab a reader to the standard output of the program
            procReader = myprocess.StandardOutput()
        
            ' read all the output from the process
            While (Not procReader.EndOfStream)
               procLine = procReader.ReadLine()
        
               ' write the output to my log
               writeNotes(procLine)
            End While
        
            procReader.Close()
        
         Catch ex As Exception
            ' Write the error to my log
            writeErrors("Couldn't execute command "C:\Program Files\TextPad 5\Textpad.exe", ex)
         End Try
        

        【讨论】:

          【解决方案5】:

          我认为ShellExecute() 是您正在寻找的。​​p>

          【讨论】:

            【解决方案6】:

            永远不要在 Windows 中使用 system() ! 只需重定向 i/o 句柄。

            【讨论】:

              猜你喜欢
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 2019-06-07
              • 1970-01-01
              • 2012-06-13
              • 1970-01-01
              相关资源
              最近更新 更多