I have been spending 2 hours trying to get my path string read from a .ini file to work with the CreateProcess
function, which expects a LPCWSTR
. For some reason, no matter how I do it, it just will not work.
I have the following code, which I've taken from another SO answer, and amended for my use, however CreateProcess
still doesn't start the process.
Can anybody help?
std::ifstream file(file_path.c_str());
settings new_settings;
if(file.is_open() && !file.fail())
{
std::string key;
char sign = '=';
std::string value;
std::string line;
while(!file.eof())
{
std::getline(file, key, sign);
std::getline(file, value);
//fill in settings struct with `value` variable. Struct contains only `std::string` types.
}
}
file.close();
PROCESS_INFORMATION proc_info;
STARTUPINFO start_info;
memset(&start_info, 0, sizeof(start_info));
start_info->cb = sizeof(STARTUPINFO);
std::string path = "C:\\Mydir\\myexe.exe";
int len;
int slength = (int)path.length() + 1;
len = MultiByteToWideChar(CP_ACP, 0, path.c_str(), slength, 0, 0);
wchar_t* buf = new wchar_t[len];
MultiByteToWideChar(CP_ACP, 0, path.c_str(), slength, buf, len);
std::wstring wpath(buf);
delete[] buf;
LPCWSTR p_path = wpath.c_str();
created = CreateProcess(p_path, lpPort, 0,0, FALSE, CREATE_NEW_CONSOLE,NULL,NULL,&start_info ,&proc_info);
DWORD dwErr = GetLastError(); //Returns 0x7b (Invalid Path Or FileName)