I am trying to open an application from my .cpp
file. I did some research and found that using CreateProcess()
would be the best option. Doing this resulted in the following code:
//Below has the purpose of starting up the server: -----------------------------------------------
LPWSTR command = (LPWSTR)"C:\\Users\\CCRT\\Documents\\UPDATED\\FINALSERVER\\FINALSERVER\\Debug\\FINALSERVER.exe";
// Start the child process.
LPSTARTUPINFOA si;
PROCESS_INFORMATION pi;
if (!CreateProcess(NULL, // No module name (use command line)
command, // Command line
NULL, // Process handle not inheritable
NULL, // Thread handle not inheritable
FALSE, // Set handle inheritance to FALSE
0, // No creation flags
NULL, // Use parent's environment block
NULL, // Use parent's starting directory
&si, // Pointer to STARTUPINFO structure
&pi) // Pointer to PROCESS_INFORMATION structure
)
{
printf("CreateProcess failed (%d).\n", GetLastError());
}
else {
std::cout << "Command success\n";
}
However, i get the following error when i try to build my solution:
cannot convert argument 9 from 'LPSTARTUPINFOA *' to 'LPSTARTUPINFOW'
The error is on the CreateProcess()
function.
I am wondering if anybody can explain the error to me and tell me how to fix it as i am so confused about what is going wrong.
CodePudding user response:
cannot convert argument 9 from 'LPSTARTUPINFOA *' to 'LPSTARTUPINFOW'
You are giving the function a pointer to a STARTUPINFOA
pointer but the function expects a STARTUPINFOW
pointer (which is what LPSTARTUPINFOW
is typedef
ined as).
The correct way is therefore to define si
like so:
STARTUPINFOW si{sizeof(STARTUPINFOW)}; // not LPSTARTUPINFOW
The sizeof(STARTUPINFOW)
part sets the cb
member of si
to the size of the structure.
Also:
LPWSTR command = (LPWSTR)"C:\\U...
is wrong. It should be
wchar_t command[] = L"C:\\U...";
// ^
The L
makes the string literal a const wchar_t[N]
(where N
is the length of the string 1), but since the command
may be altered by CreateProcessW
, you need to put it in a mutable wchar_t
array.
It's also best to be consistent. Use the "wide" functions explicitly, like CreateProcessW
, unless you plan on building for Ansi too, then be consistent and use STARTUPINFO
(without A
or W
) and use the TEXT
macro to define strings.