The documentation says...
[out] lpBuffer
A pointer to the buffer that receives the current directory string.
This null-terminated string specifies the absolute path to the current
directory.
To determine the required buffer size, set this parameter to NULL and
the nBufferLength parameter to 0.
You're getting the required buffer size, not the path (NPath
is NULL) and you're passing MAX_PATH
to nBufferLength.
Wikipedia says...
C++11 corrects this by introducing a new keyword to serve as a
distinguished null pointer constant: nullptr. It is of type nullptr_t,
which is implicitly convertible and comparable to any pointer type or
pointer-to-member type. It is not implicitly convertible or comparable
to integral types, except for bool.
There's no way to deference a nullptr
and NULL
(make an access violation exception).
The code should be this:
LPTSTR NewPath;
DWORD a = GetCurrentDirectory(0, NULL);
NewPath = new TCHAR[a];
GetCurrentDirectory(a, NPath);
HANDLE hNewFile = CreateFile(NewPath, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);