CreateProcess的未处理错误 [英] Unhandled Error with CreateProcess
问题描述
我正在阅读有关c ++中的CreateProcess函数的信息,我想尝试一下。该代码的基本思想是让我的主体执行另一个进程(记事本)。真的,这只是基本代码。运行程序时,我得到:
I was reading about CreateProcess function in c++ and I wanted to try it. Basic idea of the code is to have my main execute another process (notepad). Really, it’s just the basic code. When I run the program, I get:
createprocess.exe中0x752bb763处的第一次机会异常:0xC0000005:访问冲突写入位置0x00be57b8。
createprocess.exe中0x752bb763处未处理的异常:0xC0000005:访问冲突写入位置0x00be57b8。
First-chance exception at 0x752bb763 in createprocess.exe: 0xC0000005: Access violation writing location 0x00be57b8.
Unhandled exception at 0x752bb763 in createprocess.exe: 0xC0000005: Access violation writing location 0x00be57b8.
一个发生错误的断点,我进入了tidtable.c(我想这是用于访问线程的)。
在tidtable.c中,具体位于 CRTIMP PFLS_GETVALUE_FUNCTION __cdecl __set_flsgetvalue()
我真的不知道该如何避免这种问题。该错误发生在CreateProcess调用中(即,它永远不会输出超出创建范围)。
When I make a break point for where the error occurs, I get taken to tidtable.c (which is for accessing threads, I guess).
Specifically in tidtable.c at CRTIMP PFLS_GETVALUE_FUNCTION __cdecl __set_flsgetvalue()
I really don’t know what or how to avoid this problem. The error occurs with the CreateProcess call (ie, it never outputs the "out of create").
我的代码是:
#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.
推荐答案
问题是 CreateProcess 函数是输入/输出参数。
The problem is that the second parameter of the CreateProcess function is an in/out parameter.
如果像您一样将其指定为字符串,则它是一个常量字符串,并且调用该函数时无法将其写入内存位置,因此您遇到了内存访问冲突。正确的方法是这样调用函数:
If you specify it as a string like you did, it is a constant string and the function when it is 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;
}
您可能还想阅读此博客文章。
这篇关于CreateProcess的未处理错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!