MSDN docs state:
lpFilename [out]
A pointer to a buffer that receives the fully qualified path of the module. If the length of the path is less than the size that the nSize parameter specifies, the function succeeds and the path is returned as a null-terminated string.
If the length of the path exceeds the size that the nSize parameter specifies, the function succeeds and the string is truncated to nSize characters including the terminating null character.
Windows XP: The string is truncated to nSize characters and is not null-terminated.
This is ambiguous. Am I to interpret this that the string is never null-terminated on Windows XP? Or is it only not null-terminated when the string gets truncated?
I would appreciate it if someone knows of a better-worded reference, or is running Windows XP somewhere and can simply test the behavior.
I think you need to read that line in the context of the para above:
And then use a combination of the return value and a call to GetLastError() to determine whether you have a complete path.
If you have a complete path (
ERROR_SUCCESS
) ANDreturn-value == nSize
then you ought not assume that it's null-terminated.In my view, this argues for never assuming that it's null-terminated. The interface is broken. The buffer you send to the function should be one char bigger than nSize. Then you can null-terminate.
As of c++11,
std::basic_string<TCHAR>
is guaranteed to append a trailing zero, so something like this should do nicely: