我正在尝试使用 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/