Hi @Massimo ,
Thank you for your questions and for highlighting the contradiction.
- On supported Windows versions, does CreateDirectory2W return NULL, INVALID_HANDLE_VALUE, or either on failure, and can this be confirmed from the implementation or an authoritative Microsoft API contract?
The current Microsoft references are inconsistent: the CreateDirectory2W documentation specifies zero on failure, whereas the CreateDirectory2A documentation specifies INVALID_HANDLE_VALUE. The example on the W page also checks INVALID_HANDLE_VALUE.
These references do not conclusively establish the actual failure sentinel for CreateDirectory2W across supported Windows versions. They therefore do not confirm whether failure returns NULL, INVALID_HANDLE_VALUE, or whether both values are permitted.
Confirmation from the API owner or a corrected, unambiguous Microsoft reference is needed to resolve the discrepancy.
- When should GetLastError be called, and when can the returned handle be considered valid?
Both references direct callers to GetLastError after failure. The GetLastError documentation advises retrieving the error immediately when the function’s return value indicates failure, before another call can overwrite it. A zero last-error value is not an independent guarantee of success.
Given the unresolved sentinel, I cannot recommend treating every nonzero result as a valid handle. As a temporary defensive precaution, you could reject both NULL and INVALID_HANDLE_VALUE before using the result, but that is not confirmation of the API’s contract and does not meet your requirement for an authoritative guarantee.
- Must the successful directory handle be closed with CloseHandle?
For a successfully returned directory handle, the published example uses CloseHandle when finished. The CloseHandle documentation also states that applications should generally close each opened handle once. Failure values must not be passed to CloseHandle.
- Will the return-value documentation and six-argument example be corrected?
These documentation differences would benefit from clarification. However, a correction or update timeline has not been confirmed.
I hope this helps and provide you the answers you need.
If this instruction is applicable to your situation, I would greatly appreciate it if you could follow the instruction here so others experiencing similar behavior can benefit from it as well.