Passing a string in Process::Start
-
24-12-2019 - |
Вопрос
I run successfully:
Process::Start("C:\\Users\\Demetres\\Desktop\\JoypadCodesApplication.exe");
from a Windows Forms application (visual c++) and as expected, I got 2 programs running simultaneously. My questions is:
Can I pass a string -indicating the file name- to the Process::Start
method? I tried:
std::string str="C:\\Users\\Demetres\\Desktop\\JoypadCodesApplication.exe";
Process::Start("%s", str);
but failed. Is this possible?
EDIT:
Решение
I think you actually need to marshal to a System::String^
for passing an argument.
You can even marshal directly from a std::string
to a 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));
But in this case, you can just use a String^
instead:
String^ str = L"path to file";
Process::Start(str);
When you are working with C++/CLI, you either need to marshal back and forth or use the right data type from the start for how you want to use it.
Другие советы
Process::Start
expects a String^
and you are attempting to pass it a std::string
. It does not have a variadric version, and does not know what a std::string
is. To pass the value from a std::string
, you must marshal it:
std::string str("Some Path");
Process::Start(marshal_as<String^>(str));