WriteFile and lpNumberOfBytesWritten
<< Back
I was developing in VS 2022 on Windows 10 and I got a snippet of code from CoPilot
that looked like this
WriteFile(hFile, lpByteArray, 512, NULL, NULL);
It caught my eyes because I have used
WriteFile many times in the past and I remember
the fourth parameter (lpNumberOfBytesWritten) is NOT an optional parameter. So I consulted
MSDN, sure enough it says
BOOL WriteFile(
[in] HANDLE hFile,
[in] LPCVOID lpBuffer,
[in] DWORD nNumberOfBytesToWrite,
[out, optional] LPDWORD lpNumberOfBytesWritten,
[in, out, optional] LPOVERLAPPED lpOverlapped
);
Still somewhat skeptical, I tried passing NULL for the fourth parameter, it worked like a charm
on my Windows 10 machine. I quickly forgot about the entire thing until I was testing ListViewExporter
on my Windows 7 and Windows XP machines. My program crashed spectacularly on both OS. Long story
short, I tracked it down to WriteFile being the culprit. Apparently I fell victim to the LT;DR problem.
At the section where MSDN explains each parameter, it has a remark for lpNumberOfBytesWritten
[out, optional] lpNumberOfBytesWritten
Windows 7: This parameter can not be NULL.