CreateProcess出现未处理的错误
Unhandled Error with CreateProcess
我读到了c++中的CreateProcess函数,我想尝试一下。代码的基本思想是让我的main执行另一个进程(记事本)。真的,这只是基本的代码。当我运行程序时,我得到:
createprocess.exe中0x752bb763处的首次机会异常:0xC0000005:写入位置0x00be57b8的访问冲突。
createprocess.exe中0x752bb763处未处理的异常:0xC0000005:写入位置0x00be57b8的访问冲突。
当我确定错误发生的位置时,我会被带到tidtable.c(我想这是用于访问线程的)。具体在CRTIMP PFLS_GETVALUE_FUNCTION __cdecl __set_flsgetvalue()
的tidtable.c中我真的不知道该如何避免这个问题。该错误发生在CreateProcess调用中(即,它从不输出"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);
}
如果有人知道如何克服这个问题,我们将不胜感激。
问题是CreateProcess函数的第二个参数是输入/输出参数。
如果像以前那样将其指定为字符串,则它是一个常量字符串,并且调用它时函数无法写入内存位置,因此存在内存访问冲突。正确的方法是这样调用函数:
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的第二个参数不能是const或文字字符串,因为func试图修改字符串。将文本复制到本地数组,然后将其作为第二个参数传递。
相关文章:
- 在c++中实现LinkedList时,应出现未处理的错误
- VisualC++ 2010 有没有办法找出有关未处理异常错误的更多详细信息
- 错误 : 异常 : 引发未处理的异常: 读取访问冲突.0xDDDDDDDD临时温度
- C++ 读取文件和写入变量时未处理的异常 - getline 错误?
- 使用ACE_dll动态加载客户端C++dll,错误为未处理的异常
- C++ 正则表达式错误(未处理的异常)
- 文本框为空时,C++Windows窗体应用程序出现未处理的异常错误
- 文本框为空时C++ Windows 窗体应用程序未处理的异常错误
- CImg 错误未处理的异常堆栈溢出
- 未处理的异常错误
- opengl错误:GLTest.exe中0x0251D09F(ig4dev32.dll)处未处理的异常:0xC00000
- Coin3D(Open Inventor)中出现未处理的异常错误
- Stack Pop()函数出现未处理的异常错误
- OpenCV未处理的异常错误
- Visual Studio 2008 上的 HelloWorld OpenGL 程序中未处理的异常错误
- 未处理的异常错误 Visual Studio 2012 和 Allegro 4
- 错误 linkedlist .exe 中0x00D0345E处的未处理异常: 0xC0000005:访问违规写入位置0x
- CreateProcess出现未处理的错误
- 每当我尝试初始化Employer对象时,我的程序都会给出一个未处理的异常错误
- 文件处理错误(未处理异常)