I am trying to CreateProcess with std::string, I have searched almost everywhere to figure out how to convert a std::string to LPSTR
I am new to C++
I'm making a GUI, and when I click a button, I want to launch a program based on the path that I input, and the the 32-bit or 64-bit check box I put.
For the client directory I change System::String ^ to std::string
std::string path = this->getClientDirectory(); // Get the directory that the user has set
// Get the EXE bit that the user has ticked.
std::string exe;
if (this->isClient32Ticked)
exe = "client_x32.exe";
else
exe = "client_x64.exe";
//add the exe name to the end of the string.
path.append("\\");
path.append(exe);
CreateProcess(NULL, path, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL);
You either need to use the c_str
method with CreateProcessA
...
CreateProcessA(NULL, path.c_str(), NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL);
Or make path
a std::wstring
and use data
with CreateProcess
(or CreateProcessW
), and live in a distant-enough future that std::basic_string::data
has a non-const
version (this should happen with C++17, but MSVC hasn't caught up to it yet).
CreateProcessW(NULL, path.data(), NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL);
The W version of CreateProcess may modify the command parameter in-place, meaning that you shouldn't pass a "read-only" version of it. Strangely enough, the A version doesn't.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With