CreateProcess 方法以错误结束 [英] CreateProcess method ends up with an error
问题描述
我的以下代码有问题:
int main(int argc, char **argv) {
PROCESS_INFORMATION pi;
STARTUPINFO si;
printf("Process %d reporting for duty
",GetCurrentProcessId());
GetStartupInfo(&si);
CreateProcess(NULL,"notepad.exe", NULL,NULL,FALSE,DETACHED_PROCESS, NULL,NULL, &si, &pi);
printf("New Process ID: %d
",pi.dwProcessId);
return(0);
}
在运行时,我在调试时运行它,它在 CreateProcess 方法上崩溃,并显示以下错误消息:Tests.exe 中 0x7c82f29c 处的未处理异常:0xC0000005:访问冲突写入位置 0x00415760."强>什么意思???
And on the runing time,I ran this while debuggin and it crashes on the CreateProcess method,with this error message:" Unhandled exception at 0x7c82f29c in Tests.exe: 0xC0000005: Access violation writing location 0x00415760." What does it means???
推荐答案
32 位可执行文件的基地址总是 0x00400000
.无法写入的地址,根据异常是0x00415760
.这意味着您的代码几乎肯定会尝试写入可执行映像的只读部分.例如,当您尝试写入字符串文字时,就会发生这种情况.
32 bit executables invariably have a base address of 0x00400000
. The address that cannot be written to, according to the exception is 0x00415760
. Which means that your code is almost certainly trying to write to a read-only part of the executable image. That happens, for example, when you try to write to string literals.
现在,CreateProcess的第二个参数code>
必须是可修改的内存(它被声明为 LPTSTR
).但是您正在传递一个字符串文字.将 "notepad.exe"
放入可修改的缓冲区中以解决您的问题.
Now, the second parameter to CreateProcess
must be modifiable memory (it is declared as LPTSTR
). But you are passing a string literal. Put "notepad.exe"
in a modifiable buffer to solve your problem.
char CommandLine[] = "notepad.exe";
CreateProcess(NULL, CommandLine, ...
这篇关于CreateProcess 方法以错误结束的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!