So I'm trying to create a process using string variables.
STARTUPINFO si;
PROCESS_INFORMATION pi;
ZeroMemory(&si, sizeof(si));
ZeroMemory(&pi, sizeof(pi));
si.cb = sizeof(si);
std::string cmd_line = game_path + " " + std::string(game_params);
std::cout << cmd_line << "\n";
if (!CreateProcess(NULL, LPTSTR(cmd_line.c_str()), NULL, NULL, FALSE, 0, NULL, NULL, &si, &pi)) {
std::cout << "fail\n";
return false;
}
The output from cout cmd_line is the correct path and parameters:
C:\\Program Files (x86)\\My_Game\\Game.exe -test -admin
The createprocess call is returning false, but I'm not sure exactly why. I'm pretty new to this so any help and advice would be great.
int _tmain(int argc, _TCHAR* argv[])
{
STARTUPINFO si;
PROCESS_INFORMATION pi;
ZeroMemory(&si, sizeof(si));
ZeroMemory(&pi, sizeof(pi));
si.cb = sizeof(si);
std::string game_path = "C:\\Windows\\system32\\calc.exe";
std::string game_params = "-test -admin";
std::string cmd_line = game_path + " " + std::string(game_params);
std::cout<<cmd_line << "\n";
TCHAR tszCmdLine[1024] = {0};
mbstowcs(tszCmdLine, cmd_line.c_str(), 1024);
_tprintf(tszCmdLine);
if (!CreateProcess(NULL, tszCmdLine, NULL, NULL, FALSE, 0, NULL, NULL, &si, &pi)) {
std::cout<<"fail\n"<<GetLastError();
return false;
}
}
const char*
cannot be parameter of CreateProcess()
. You have to make char*
string to tchar*
string using mbstowcs()
I've test your code.
The result of GetLastError()
is 2.
That is The system cannot find the file specified.
Use MultiByteToWideChar
to convert char*
string to TCHAR*
string.
MultiByteToWideChar(LC_ALL, 0, cmd_line.c_str(), -1, str_command, MAX_PATH);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.