我正在使用CreateProcess,但我無法啓動一個進程我正在使用下面的代碼,但我得到錯誤「無效的訪問內存位置」,但我不知道爲什麼。 我的代碼有問題嗎?CreateProcess我無法啓動進程
#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.wShowWindow = 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());
}
[MSDN documentation](http://msdn.microsoft.com/zh-cn/library/ms682425.aspx)說:「* lpCommandLine *參數可以爲NULL。在這種情況下,該函數使用字符串指向由* lpApplicationName *作爲命令行「,所以不應該是問題的原因。 –
將其初始化爲全零的最簡單方法是:STARTUPINFO startupinfo = {0}; – Ferruccio
@Ferruccio:這將初始化爲零。但這可能不一定是許多情況下需要的。考慮假設字段「int timeout; //中止進程前的秒數。設置爲-1以永久等待」將其設置爲零將導致其立即失敗。 -1就是那裏需要的。將一切設置爲零的毯子是有風險的。 – abelenky