#include <stdio.h>
#include <windows.h>
#include <tchar.h>
int main()
{
TCHAR cwd[32768];
GetCurrentDirectory(sizeof(cwd) / sizeof(TCHAR) - 1, cwd);
// Build the path to NUL. Flip the flag below to see it fail.
TCHAR path[sizeof(cwd)];
#if 1
_stprintf(path, "\\\\?\\%s\\NUL", cwd);
#else
_tcscpy(path, "NUL");
#endif
// Create the file, if it doesn't exist yet
HANDLE h = CreateFile(path, GENERIC_WRITE, 0, 0,
CREATE_ALWAYS, 0, 0);
if (h != INVALID_HANDLE_VALUE) {
// Write our test string to the file. If the flag above is 1
// and you try to open this file with Explorer or cmd.exe, it
// will fail, but you *can* open it with, say, Cygwin, because
// Cygwin opens files via a full path like the one we use in the
// "1" case above, bypassing the NUL path name special case code
// inside the Windows kernel.
LPTSTR s = _T("booga, booga!\n");
DWORD nWritten, nExpected = _tcslen(s) * sizeof(TCHAR);
if (WriteFile(h, s, nExpected, &nWritten, 0)) {
_tprintf("Wrote %d bytes to %s.\n", nWritten, path);
}
CloseHandle(h);
}
else {
// This shouldn't happen, even if you flip the flag above. The
// "0" case doesn't cause an error, it just sends the written
// bytes to the bit bicket, emulating Unix's /dev/null.
LPTSTR errorText = 0;
FormatMessage(
FORMAT_MESSAGE_FROM_SYSTEM |
FORMAT_MESSAGE_ALLOCATE_BUFFER |
FORMAT_MESSAGE_IGNORE_INSERTS,
0,
GetLastError(),
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
(LPTSTR)&errorText,
0,
0);
_tprintf("Failed to create NUL: error %s\n", errorText);
}
}
Comments
0 B
|👍
/👎
0 B
|👍
/👎