@@ -532,24 +532,34 @@ ArchGetFileName(FILE *file)
532532 }
533533 return result;
534534#elif defined (ARCH_OS_WINDOWS)
535- static constexpr DWORD bufSize =
536- sizeof (FILE_NAME_INFO) + sizeof (WCHAR) * 4096 ;
537- HANDLE hfile = _FileToWinHANDLE (file);
538- auto fileNameInfo = reinterpret_cast <PFILE_NAME_INFO>(malloc (bufSize));
539535 string result;
540- if (GetFileInformationByHandleEx (
541- hfile, FileNameInfo, static_cast <void *>(fileNameInfo), bufSize)) {
536+ WCHAR filePath[MAX_PATH];
537+ HANDLE hfile = _FileToWinHANDLE (file);
538+ if (GetFinalPathNameByHandleW (hfile, filePath, MAX_PATH, VOLUME_NAME_DOS)) {
542539 size_t outSize = WideCharToMultiByte (
543- CP_UTF8, 0 , fileNameInfo-> FileName ,
544- fileNameInfo-> FileNameLength / sizeof (WCHAR ),
540+ CP_UTF8, 0 , filePath ,
541+ wcslen (filePath ),
545542 NULL , 0 , NULL , NULL );
546543 result.resize (outSize);
547544 WideCharToMultiByte (
548- CP_UTF8, 0 , fileNameInfo-> FileName ,
549- fileNameInfo-> FileNameLength / sizeof (WCHAR) ,
545+ CP_UTF8, 0 , filePath ,
546+ - 1 ,
550547 &result.front (), outSize, NULL , NULL );
548+
549+ if (result.length () > 4 )
550+ {
551+ // It needs to strip the path prefix as
552+ // the path returned is DOS device path, and the
553+ // syntax is one of:
554+ // \\.\C:\Test\Foo.txt
555+ // \\?\C:\Test\Foo.txt
556+ if (result.compare (0 , 4 , " \\\\ ?\\ " ) == 0 ||
557+ result.compare (0 , 4 , " \\\\ .\\ " ) == 0 )
558+ {
559+ result.erase (0 , 4 );
560+ }
561+ }
551562 }
552- free (fileNameInfo);
553563 return result;
554564#else
555565#error Unknown system architecture
0 commit comments