#include #include #include #include void PrintError(const std::string& msg) { DWORD err = GetLastError(); std::cerr << "Error: " << msg << " (Code: " << err << ")" << std::endl; } int main() { // 1. Create a temporary file to test with wchar_t tempPath[MAX_PATH]; const wchar_t *tempFileName; tempFileName = L"z:\\tempfile.tmp"; std::wcout << L"Created temp file: " << tempFileName << std::endl; // 2. Open a handle to the file // Note: FILE_FLAG_BACKUP_SEMANTICS is often used for directories, but works for files too. HANDLE hFile = CreateFileW( tempFileName, GENERIC_READ, FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL ); if (hFile == INVALID_HANDLE_VALUE) { PrintError("CreateFileW failed"); return 1; } // 3. Call GetFinalPathNameByHandleW // First call to get the required buffer size DWORD requiredSize = GetFinalPathNameByHandleW(hFile, NULL, 0, VOLUME_NAME_GUID); if (requiredSize == 0) { PrintError("GetFinalPathNameByHandleW (size check) failed"); CloseHandle(hFile); return 1; } std::vector pathBuffer(requiredSize); // Second call to get the actual path DWORD result = GetFinalPathNameByHandleW(hFile, pathBuffer.data(), requiredSize, VOLUME_NAME_GUID); if (result == 0) { PrintError("GetFinalPathNameByHandleW (retrieval) failed"); } else { std::wcout << L"Final Path (VOLUME_NAME_DOS): " << pathBuffer.data() << std::endl; } // 4. Cleanup CloseHandle(hFile); DeleteFileW(tempFileName); // Clean up the temp file std::cout << "Press Enter to exit..."; std::cin.get(); return 0; }