CreateProcess 未处理的错误 [英] Unhandled Error with CreateProcess

查看:30
本文介绍了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 调用中(即,它从不输出out of create").

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆