12

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

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

The call stack on crash:

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++

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

What's going on here? Thanks!

user38329
  • 679
  • 4
  • 17

2 Answers2

18

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

vcsjones
  • 128,004
  • 28
  • 283
  • 274
Raymond Chen
  • 42,606
  • 11
  • 86
  • 125
  • 3
    D'OH! "This parameter can be NULL only when the lpOverlapped parameter is not NULL." http://msdn.microsoft.com/en-us/library/windows/desktop/aa365747%28v=vs.85%29.aspx – Mordachai Nov 19 '11 at 17:18
  • Thank you. The "__out_opt" in the prototype confused me. – user38329 Nov 19 '11 at 17:20
  • I'm also having the same problem as OP mentioned in the question. And as far as I remember, I also passed NULL for that parameter. So...Thanks for your great answer. and yes... +1 :) – Farhad Reza Jan 05 '17 at 09:40
0

4th parameter (which tells us how much bytes were actually written) is expecting pointer to DWORD value (a.k.a unsigned int) when you pass NULL to that parameter it attempts to write DWORD to null pointer which causes exception, not only it is mandatory to pass pointer to that argument but also you should always check its value after write because there is probability although slim that WriteFile will write less data than you provided.