在Process :: Start中传递一个字符串

时间:2014-02-27 19:59:43

标签: c++ .net string c++-cli process.start

我成功运行:

Process::Start("C:\\Users\\Demetres\\Desktop\\JoypadCodesApplication.exe");

来自Windows窗体应用程序(visual c ++),正如预期的那样,我有2个程序同时运行。我的问题是:

我可以将表示文件名的字符串传递给Process::Start方法吗?我试过了:

std::string str="C:\\Users\\Demetres\\Desktop\\JoypadCodesApplication.exe";
Process::Start("%s", str);

但失败了。这可能吗?

修改 enter image description here

2 个答案:

答案 0 :(得分:1)

Process::Start需要String^,并且您尝试将其传递给std::string。它没有variadric版本,也不知道std::string是什么。要传递std::string的值,您必须整理它:

std::string str("Some Path");
Process::Start(marshal_as<String^>(str));

答案 1 :(得分:1)

我认为你实际上需要为System::String^编组以传递参数。 您甚至可以直接从std::string封送到System::String^

///marshal_as<type>(to_marshal)
///marshal_context ctx; ctx.marshal_as<const char *>(to_marshal)
#include <msclr/marshal.h>
#include <msclr/marshal_cppstd.h>
#include <msclr/marshal_atl.h>
using namespace msclr::interop;
using namespace System::Runtime::InteropServices;

Process::Start(marshal_as<String^>(str));

但在这种情况下,您只需使用String^

String^ str = L"path to file";
Process::Start(str);

当您使用C ++ / CLI时,您需要来回编组或从一开始就使用正确的数据类型来使用它。

MSDN: Overview of Marshaling in C++