c++ - Fail to pass command line arguments in CreateProcess -
i'm having trouble using createprocess command line arguments. i've read posts i've found none of solutions did work.
here's have:
std::string path = "c:\\my\\path\\myfile.exe"; std::wstring stemp = std::wstring(path.begin(), path.end()); lpcwstr path_lpcwstr = stemp.c_str(); std::string params = " param1 param2 param3"; startupinfo info = { sizeof(info) }; process_information processinfo; createprocess(path_lpcwstr, lptstr(params.c_str()), null, null, true, create_new_process_group, null, null, &info, &processinfo);
the code works , myfile.exe (a qt application) opened, argc 1. i've tried specifying first parameter "c:\my\path\myfile.exe param1 param2 param3" didn't work either.
any appreciated.
solution: using createprocessa , change parameters accordingly fixed problem pointed out 1 of answers.
startupinfoa info = { sizeof(info) }; process_information processinfo; std::string path = "c:\\my\\path\\myfile.exe"; std::string params = " param1 param2 param3"; createprocessa(path.c_str(), const_cast<char *>(config.c_str()) , null, null, true, create_new_process_group, null, null, &info, &processinfo);
there 2 versions of createprocess
(and many other winapi functions too):
one takes "normal" strings in ascii/iso88591/whatever each character has 1 byte.
"abc" have numbers 97 98 99
.
the other createprocess
takes utf16 strings; each char has 2 or 4 byte there,
, "abc" have byte numbers 0 97 0 98 0 99
(utf16 bit more complicated, in case, it´s 0s added).
advantage better support internationalization, because the
old 1-byte charsets problematic languages russian, greek etc.
you´re using second version. path_lpcwstr
, ie. program path , name first parameter, correctly provided utf16 string (std::wstring
on windows , lpcwstr
etc. ...).
however, second parameter arguments new process, not utf16 in code (but one-byte charset) , avoid compiler error, casting pointer , telling compiler treat not-utf16 content utf16.
bytes of " param1 param2 param3" understood utf16 won´t give sane string without proper conversion, , start with, 2 byte 0 value terminate string, requried windows, in there. result undefined behaviour, strange things can happen.
make parameter string did path, , should fine.
Comments
Post a Comment