CreateProcess的未处理错误

Fav*_*ksd 5 c++ winapi createprocess unhandled-exception access-violation

我正在阅读c ++中的CreateProcess函数,我想尝试一下.代码的基本思想是让我的主要执行另一个进程(记事本).真的,这只是基本代码.当我运行程序时,我得到:

createprocess.exe中0x752bb763处的第一次机会异常:0xC0000005:访问冲突写入位置0x00be57b8.
createprocess.exe中0x752bb763处的未处理异常:0xC0000005:访问冲突写入位置0x00be57b8.

当我为发生错误的位置设置一个断点时,我会被带到tidtable.c(我想这是用于访问线程).特别是在tidtable.c中 CRTIMP PFLS_GETVALUE_FUNCTION __cdecl __set_flsgetvalue() 我真的不知道是什么或如何避免这个问题.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);

}
Run Code Online (Sandbox Code Playgroud)

如果有人知道如何克服这个问题,我们将不胜感激.

erv*_*her 17

问题是CreateProcess函数的第二个参数是in/out参数.

如果将它指定为字符串,则它是一个常量字符串,调用它时的函数无法写入内存位置,因此存在内存访问冲突.正确的方法是调用你的函数:

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;
}
Run Code Online (Sandbox Code Playgroud)

您可能还想阅读此博客文章.