本文介绍了为什么WriteFile写入标准输出时崩溃?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时删除!!

这里是一个使用WinAPI的WriteFile(在Microsoft Visual C ++ 2008 Express中编译)的Hello world程序:

Here's a "Hello world" program that uses WinAPI's WriteFile (compiled in Microsoft Visual C++ 2008 Express):

int _tmain(int argc, _TCHAR* argv[])
{
    wchar_t str[] = L"Hello world";

    HANDLE out = GetStdHandle(STD_OUTPUT_HANDLE);
    if(out && out!=INVALID_HANDLE_VALUE)
    {
        WriteFile(out, str, sizeof(str), NULL, NULL);
        CloseHandle(out);
    }

    return 0;
}

如果在控制台窗口中执行,它会很高兴地迎接世界。如果你尝试重定向其标准输出,但是,如

If executed in a console window, it happily greets the world. If you try to redirect its standard output, however, as in

hello.exe > output.txt

程序在WriteFile(NULL指针异常)中崩溃。

the program crashes in WriteFile (NULL pointer exception). Nonetheless, output.txt exists and contains the correct output in its entirety.

崩溃时的调用堆栈:

KernelBase.dll!_WriteFile@20()  + 0x75 bytes
kernel32.dll!_WriteFileImplementation@20()  + 0x4e bytes
srgprc2.exe!wmain(int argc=1, wchar_t * * argv=0x00483d88)  Line 15 + 0x16 bytes    C++

在0x75ce85ea(KernelBase.dll)在srgprc2.exe中的异常:0xC0000005:访问冲突写入位置0x00000000

The message: "Unhandled exception at 0x75ce85ea (KernelBase.dll) in srgprc2.exe: 0xC0000005: Access violation writing location 0x00000000."

这里发生了什么?谢谢!

What's going on here? Thanks!

推荐答案

WriteFile的第四个参数 。您传递的是NULL,不允许。

The fourth parameter to WriteFile is not optional. You are passing NULL, which is not allowed.

这篇关于为什么WriteFile写入标准输出时崩溃?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

1403页,肝出来的..

09-08 08:59