Skip to content

Commit 3fc6704

Browse files
committed
fixed a bug with UNC paths (should now support network paths properly)
1 parent ef9d0e1 commit 3fc6704

File tree

1 file changed

+29
-16
lines changed

1 file changed

+29
-16
lines changed

DeviceNameResolver.cpp

+29-16
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,16 @@ __declspec(dllexport) bool DevicePathFromFileHandleW(HANDLE hFile, wchar_t* szDe
5151
}
5252
GlobalFree(NameInformation);
5353
}
54+
if(_wcsnicmp(szDevicePath, L"\\Device\\LanmanRedirector\\", 25) == 0) // Win XP
55+
{
56+
wcscpy_s(szDevicePath, nSize / sizeof(wchar_t), L"\\\\");
57+
wcscat_s(szDevicePath, nSize / sizeof(wchar_t), &szDevicePath[25]);
58+
}
59+
else if(_wcsnicmp(szDevicePath, L"\\Device\\Mup\\", 12) == 0) // Win 7
60+
{
61+
wcscpy_s(szDevicePath, nSize / sizeof(wchar_t), L"\\\\");
62+
wcscat_s(szDevicePath, nSize / sizeof(wchar_t), &szDevicePath[12]);
63+
}
5464
return bRet;
5565
}
5666

@@ -76,29 +86,32 @@ __declspec(dllexport) bool PathFromFileHandleW(HANDLE hFile, wchar_t* szPath, si
7686
static GETFINALPATHNAMEBYHANDLEW GetFPNBHW=(GETFINALPATHNAMEBYHANDLEW)GetProcAddress(GetModuleHandleW(L"kernel32.dll"), "GetFinalPathNameByHandleW");
7787
if(GetFPNBHW && GetFPNBHW(hFile, szPath, (DWORD)(nSize/sizeof(wchar_t)), 0))
7888
{
79-
wcscpy_s(szPath, nSize, &szPath[4]); //remove "\\?\"
89+
if(_wcsnicmp(szPath, L"\\\\?\\UNC\\", 8) == 0) // Server path
90+
{
91+
wcscpy_s(szPath, nSize / sizeof(wchar_t), L"\\\\");
92+
wcscat_s(szPath, nSize / sizeof(wchar_t), &szPath[8]);
93+
}
94+
else if(_wcsnicmp(szPath, L"\\\\?\\", 4) == 0 && szPath[5]==L':') // Drive path
95+
{
96+
wcscpy_s(szPath, nSize/sizeof(wchar_t), &szPath[4]);
97+
}
8098
return true;
8199
}
82100
if(!DevicePathFromFileHandleW(hFile, szPath, nSize))
83101
return false;
84-
return DevicePathToPathW(szPath, szPath, nSize);
102+
std::wstring oldPath(szPath);
103+
if (!DevicePathToPathW(szPath, szPath, nSize))
104+
wcscpy_s(szPath, nSize / sizeof(wchar_t), oldPath.c_str());
105+
return true;
85106
}
86107

87108
__declspec(dllexport) bool PathFromFileHandleA(HANDLE hFile, char* szPath, size_t nSize)
88109
{
89-
typedef DWORD (WINAPI* GETFINALPATHNAMEBYHANDLEA) (
90-
IN HANDLE hFile,
91-
OUT char* lpszFilePath,
92-
IN DWORD cchFilePath,
93-
IN DWORD dwFlags
94-
);
95-
static GETFINALPATHNAMEBYHANDLEA GetFPNBHA=(GETFINALPATHNAMEBYHANDLEA)GetProcAddress(GetModuleHandleW(L"kernel32.dll"), "GetFinalPathNameByHandleA");
96-
if(GetFPNBHA && GetFPNBHA(hFile, szPath, (DWORD)nSize, 0))
97-
{
98-
strcpy_s(szPath, nSize, &szPath[4]); //remove "\\?\"
99-
return true;
100-
}
101-
if(!DevicePathFromFileHandleA(hFile, szPath, nSize))
110+
DynBuf newDevicePathBuf(nSize*sizeof(wchar_t));
111+
wchar_t* newDevicePath = (wchar_t*)newDevicePathBuf.GetPtr();
112+
if (!PathFromFileHandleW(hFile, newDevicePath, nSize*sizeof(wchar_t)))
102113
return false;
103-
return DevicePathToPathA(szPath, szPath, nSize);
114+
if (!WideCharToMultiByte(CP_ACP, NULL, newDevicePath, -1, szPath, (int)wcslen(newDevicePath) + 1, NULL, NULL))
115+
return false;
116+
return true;
104117
}

0 commit comments

Comments
 (0)