简体   繁体   中英

CreateProcess() Error

STARTUPINFO si;
PROCESS_INFORMATION pi;
memset(&si, 0, sizeof(si));
memset(&pi, 0, sizeof(pi));
si.cb = sizeof(si);

LPCWSTR procName =(LPCWSTR)"D:\\test dir 1\\Calc.exe";
LPWSTR procArg   =(LPWSTR)"blacknull";

if(CreateProcess(procName,procArg,0,0,0,CREATE_DEFAULT_ERROR_MODE,0,0,&si,&pi))
{
    //do some work
}

printf( "CreateProcess failed (%d).\n", GetLastError());
     system("Pause");

It keeps throwing Error(2)-> The System cannot find the file specified .

I don't know what's wrong. I also tried to use "Calc.exe" which in the same Dir. but it's not working.

You use the L prefix to make a wide character string:

L"D:\\test dir 1\\Calc.exe";

Casting a string literal to a different character width does not make a string wider.

In addition to the string type issue already pointed out, the second argument (lpCommandLine) must point to a writable buffer, not a constant string. You can do this by declaring it as follows:

WCHAR procArg[] = L"blacknull";

This is documented in MSDN : "The Unicode version of this function, CreateProcessW, can modify the contents of this string. Therefore, this parameter cannot be a pointer to read-only memory (such as a const variable or a literal string). If this parameter is a constant string, the function may cause an access violation."

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM