Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CreateProcess with std::string

Tags:

c++

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);
like image 610
StokesMagee Avatar asked Oct 20 '25 05:10

StokesMagee


1 Answers

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.

like image 99
zneak Avatar answered Oct 22 '25 18:10

zneak