Unhandled error with CreateProcess
I was reading about the CreateProcess function in C ++ and I wanted to try it. The main idea of the code is to have my main one execute another process (notepad). Indeed, this is just basic code. When I run the program, I get:
First Chance Exception at 0x752bb763 in createprocess.exe: 0xC0000005: Location where access violation was written 0x00be57b8.
Unhandled exception at 0x752bb763 in createprocess.exe: 0xC0000005: Location where access violation was written 0x00be57b8.
When I make a breakpoint where the error occurs, I end up in tidtable.c (as far as thread access is concerned, I think). In particular, tidtable.c in CRTIMP PFLS_GETVALUE_FUNCTION __cdecl __set_flsgetvalue()
I really don't know what and how to avoid this problem. The error is thrown when CreateProcess is called (ie, it never outputs "from created").
My code:
#include "stdafx.h"
#include <stdio.h>
#include <windows.h>
#include <strsafe.h>
#include <direct.h>
#include <string.h>
#include <conio.h>
int main(VOID)
{
STARTUPINFO si;
PROCESS_INFORMATION pi;
//allocate memory
ZeroMemory(&si, sizeof(si));
si.cb = sizeof(si);
ZeroMemory(&pi, sizeof(pi));
fprintf(stderr, "This is just a test");
//create child process
if (!CreateProcess(NULL,
L"C:\\Windows\\Notepad.exe",
NULL,
NULL,
FALSE,
0,
NULL,
NULL,
&si,
&pi))
{
fprintf(stderr, "create process failed");
return -1;
}
fprintf(stderr, "out of create");
//parent waits for child to complete
WaitForSingleObject(pi.hProcess, INFINITE);
fprintf(stderr, "after wait");
printf("Child Complete");
//close handle
CloseHandle(pi.hProcess);
// CloseHandle(pi.hthread);
}
If anyone knows how to overcome this problem, your help would be appreciated.
source to share
The problem is that the second parameter of the CreateProcess function is an in / out parameter.
If you specify it as a string, as you did, it is a constant string and the function, when called, cannot write to the memory location, thus you have a memory access violation. The correct way is to call your function like this:
LPTSTR szCmdline = _tcsdup(TEXT("C:\\Windows\\Notepad.exe"));
//create child process
if (!CreateProcess(NULL,
szCmdline,
NULL,
NULL,
FALSE,
0,
NULL,
NULL,
&si,
&pi))
{
fprintf(stderr, "create process failed");
return -1;
}
You can also read this blog post .
source to share