I am using CreateProcess, but I can't start a process I am using the following code but I am getting the error "Invalid access to memory location" but I don't know why. Is there any problem with my code?
#include <Windows.h>
#include <stdio.h>
//#include "common.h"
int main(void)
{
DWORD creation_flags = DEBUG_PROCESS;
STARTUPINFO startupinfo;
PROCESS_INFORMATION process_information;
char *path_to_exe = "D:\\dbg\\calc.exe";
startupinfo.dwFlags = 0x1;
startupinfo.wShowWindo开发者_JS百科w = 0x0;
startupinfo.cb = sizeof(startupinfo);
if(CreateProcess( path_to_exe,
NULL,
NULL,
NULL,
NULL,
creation_flags,
NULL,
NULL,
&startupinfo,
&process_information)){
printf("We have successfully launched the process!\n");
printf("[*] PID: %d\n", process_information.dwProcessId);
}
else
printf("[*] Error: %d.\n", GetLastError());
}
You have only filled in 3 fields of the startupinfo
Structure.
The remaining fields are filled with garbage, and some of that garbage is likely leading to bad problems.
You should fully initialize the structure, explicitly putting NULL, 0 and other "empty" values where you don't want to specify anything.
Try zeroing the startup info structure. Some of it's members (e.g. lpTitle) are used even if you don't set an explicit flag.
Also beware that CreateProcess may temporarily write to the application name string, so you may want to avoid passing a read-only string literal. This only happens with the unicode version of the function though, at least on recent versions of Windows.
精彩评论