【问题标题】:boost.process batch script is not able to to run another programboost.process 批处理脚本无法运行另一个程序
【发布时间】:2013-01-05 19:43:24
【问题描述】:

我正在通过cmd.exe /CBoost.process 调用批处理脚本

  boost::process::context context;
  context.stdout_behavior = boost::process::capture_stream();

#ifdef WIN32
  arguments.push_front("/c");
  arguments.push_front(_script);
  boost::process::child external_process = boost::process::launch("cmd.exe", arguments, context);
#else
  boost::process::child external_process = boost::process::launch(_script, arguments, context);      
#endif      
  boost::process::status status_code = external_process.wait();

如果我直接调用批处理文件,即使使用 cmd.exe /C 它也会调用外部可执行文件。

但是当通过我的程序调用批处理脚本时,它根本不会调用外部程序。 外部可执行文件生成一些文件。还将一些文本打印到标准输出。我既没有看到那些文本,也没有看到要生成的任何文件。

我什至尝试用notepad.exe 替换目标可执行文件,然后出现一个消息框Application failed to initialize properly (0xc0150004) click on OK to terminate application

【问题讨论】:

    标签: c++ winapi boost process batch-file


    【解决方案1】:

    我发现boost::process::context 不是跨平台的。我必须在 Windows 上使用boost::process::win32_contextsilence_stream 可能有问题,或者这可能是我这边的问题。

    //I've a list of argv in arguments
    
    #ifdef WIN32
      arguments.pop_front();
      arguments.push_front(_script);
      arguments.push_front("/C");
      arguments.push_front("script.bat");
    
      boost::process::win32_context win32_context;
      win32_context.stdout_behavior = boost::process::capture_stream();
      win32_context.environment = boost::process::self::get_environment(); 
    
      boost::process::child external_process = boost::process::win32_launch(std::string("C:/Windows/System32/cmd.exe"), arguments, win32_context);
    #else
      boost::process::context context;
      context.stdout_behavior = boost::process::silence_stream();
    
      boost::process::child external_process = boost::process::launch(_script, arguments, context);
    #endif
    
      boost::process::status status_code = external_process.wait();
    

    它没有得到PATH,所以应用程序找不到dllwin32_context.environment = boost::process::self::get_environment() 修复了这个问题。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-12-03
      • 2014-08-21
      • 2015-07-14
      • 1970-01-01
      • 1970-01-01
      • 2011-06-15
      • 1970-01-01
      相关资源
      最近更新 更多