Question

I am getting an exception when I run this code:

STARTUPINFO si;
PROCESS_INFORMATION pi;

ZeroMemory(&si, sizeof(si));
si.cb = sizeof(si);
ZeroMemory(&pi, sizeof(pi));

CreateProcess(NULL, L"program.dat", NULL, NULL, FALSE, 0, NULL, NULL, &si, &pi);

// Wait until child process exits.
WaitForSingleObject( pi.hProcess, INFINITE );

// Close process and thread handles. 
CloseHandle( pi.hProcess );
CloseHandle( pi.hThread );

I get the exception on WaitForSingleObject.

Thanks :)

Was it helpful?

Solution

As clearly as much that can be stated in the documentation::

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.

Yours L"program.dat" falls under this rule. Copy the string in some WCHAR variable and pass that instead.

OTHER TIPS

I can reproduce the exception using your code. I think the problem is that the second arg to CreateProcess is an in/out one. The doc states:

The system adds a terminating null character to the command-line string to separate the file name from the arguments. This divides the original string into two strings for internal processing.

See CreateProcess function

The second arg must NOT point to read-only memory.

Using the Visual Studio debugger and stepping in assembler code, the trap is indeed caused by the _CreateProcessInternal function in Kernel32 trying to write 0x to the end of L"program.dat", which, as a string constant, is in a "non writable" section.

Use below code:

wchar_t * pCommandLine = L"program.dat";
wchar_t CommandLine[ 64 ];
wcscpy( CommandLine,  pCommandLine );
CreateProcess(NULL, CommandLine, NULL, NULL, FALSE, 0, NULL, NULL, &si, &pi);

and the exception will vanish.

As for Creating a Child Process with "program.dat" ?!? That's another story.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top