c - 第一次机会异常 : KernelBase. dll

标签 c visual-studio-2010 winapi access-violation

我正在尝试使用 VS2012 Pro 构建并运行这段程序。

#include "stdafx.h"

int _tmain(int argc, _TCHAR* argv[])
{
    STARTUPINFO si;
    PROCESS_INFORMATION pi;

    //allocate memory
    ZeroMemory(&si, sizeof(si));
    si.cb = sizeof(si);
    ZeroMemory(&pi, sizeof(pi));

    //create child process
    if (!CreateProcess(NULL,    //use command line
             L"mspaint.exe",    //command line
             NULL,    //don't inherit process handle
             NULL,    //don't inherit thread handle
             FALSE,    //disable handle inheritance
             0,        //no creation flags
             NULL,    //use parent's environment block
             NULL,    //use parent's existing directory
             &si,
             &pi))
    {
        fprintf(stderr, "Create Process Failed");
        return -1;
    }
    WaitForSingleObject(pi.hProcess, INFINITE);
    CloseHandle(pi.hProcess);
    CloseHandle(pi.hThread);

    return 0;
}

我收到此错误:

'ConsoleApplication1.exe' (Win32): Loaded 'G:\workspace\VS2012\ConsoleApplication1\Debug\ConsoleApplication1.exe'. Symbols loaded.

'ConsoleApplication1.exe' (Win32): Loaded 'C:\Windows\SysWOW64\ntdll.dll'. Cannot find or open the PDB file.

'ConsoleApplication1.exe' (Win32): Loaded 'C:\Windows\SysWOW64\kernel32.dll'. Cannot find or open the PDB file.

'ConsoleApplication1.exe' (Win32): Loaded 'C:\Windows\SysWOW64\KernelBase.dll'. Cannot find or open the PDB file.

'ConsoleApplication1.exe' (Win32): Loaded 'C:\Windows\SysWOW64\msvcr110d.dll'. Symbols loaded.

First-chance exception at 0x763919E3 (KernelBase.dll) in ConsoleApplication1.exe: 0xC0000005: Access violation writing location 0x00CB586E.

Unhandled exception at 0x763919E3 (KernelBase.dll) in ConsoleApplication1.exe: 0xC0000005: Access violation writing location 0x00CB586E.

The program '[6992] ConsoleApplication1.exe' has exited with code 0 (0x0).

请注意,我构建它时没有出现任何错误(文件 stdafx.h 包含我需要的所有 header )。

我搜索了这个错误,但找不到任何解决方案,而且这只是一个简单的程序,所以我不明白这里有什么问题:(。

请帮助我。

最佳答案

来自 CreateProcessW 文档,特别是关于第二个参数:

The Unicode version of this function, CreateProcessW, can modify the contents of this string. Therefore, this parameter cannot be a pointer to read-only memory (such as a const variable or a literal string). If this parameter is a constant string, the function may cause an access violation

因此,在回答您的问题时,不要将常量字符串文字作为第二个参数传递给该函数,否则您将得到 AV。

关于c - 第一次机会异常 : KernelBase. dll,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14890184/

相关文章:

c# - 在同一解决方案中的 C# 项目和 C++ 项目之间进行交互

visual-studio-2010 - VS2010如何设置_ITERATOR_DEBUG_LEVEL?

c++ - 如何修复 stb_image 库的 alpha

windows - 为什么我要使用无限超时的 Sleep()?

c - scanf 格式字符串中的非空白字符

c - 如何比较两个字符串(两者)都存储在 char 双指针中?

c - GCC 对宏中使用的 __builtin 函数的优化

c - AT&T 汇编 + C 函数。使用 Scanf 进行字符串输入

winapi - 富编辑控件 : Prevent Rich Formatting?

c++ - 如何在使用 Windows API 下载文件时创建进度条?