This function seems to have a bug when reporting the size of the required buffer. When I call it to determine the required buffer size, it reports that I need 5 bytes for the path name ("D:\" + NULL + NULL), which is correct. I allocate the buffer and make the call again. It succeeds and has the correct information in the buffer. When I try to free the buffer, I get a heap corrupted exception. I have not manipulated the buffer or pointer. In my example, if I allocate 10 bytes (2*requiredlength), everything is okay. If I allocate 9 bytes, it fails. This seems to indicate that the UNICODE version of this function is not returning the correct number of bytes, which would be ANSI size * 2. As a work-around, I am doubling the length it returns and all is well.
LPTSTR buf = NULL;
DWORD rlen = 0;
GetVolumePathNamesForVolumeNameW(volumeName, buf, 0, &rlen);
if (GetLastError() == ERROR_MORE_DATA)
{
buf = (LPTSTR) malloc (rlen*2);
.
.
.