1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
| #include <Windows.h>
#include <psapi.h>
#include <wchar.h>
WCHAR *getFilePathByHandle(HANDLE hFile, WCHAR* filePath, int filePathLen)
{
DWORD dwFileSizeHi = 0;
DWORD dwFileSizeLo = GetFileSize(hFile, &dwFileSizeHi);
HANDLE hFileMap = CreateFileMapping(hFile, NULL, PAGE_READONLY, 0, dwFileSizeLo, NULL);
filePath[0] = 0;
if (NULL != hFileMap)
{
void* pMem = MapViewOfFile(hFileMap, FILE_MAP_READ, 0, 0, 1);
if (NULL != pMem)
{
if (0 == GetMappedFileNameW(GetCurrentProcess(), pMem, filePath, filePathLen))
{
// GetLastError
}
UnmapViewOfFile(pMem);
}
CloseHandle(hFileMap);
}
return filePath;
}
wchar_t toLower(wchar_t ch)
{
if ('A' <= ch && ch <= 'Z')
ch = ch - 'A' + 'a';
return ch;
}
int isNoCaseBeginWith(wchar_t* l, wchar_t* r)
{
if (*l == 0 && *r == 0)
return 1;
if (*l == 0 || *r == 0)
return 0;
for (; 0 != *l && 0 != *r; l++, r++)
{
if (toLower(*l) != toLower(*r))
return 0;
}
return 1;
}
WCHAR* getDosFilePath(WCHAR *dosPath, int len, WCHAR* kernelPath)
{
wchar_t path[3] = L"C:";
wchar_t szBuf[MAX_PATH] = { 0 };
for (wchar_t ch = L'A'; ch <= L'Z'; ch++)
{
path[0] = ch;
QueryDosDevice(path, szBuf, MAX_PATH);
if (isNoCaseBeginWith(kernelPath, szBuf))
{
swprintf_s(dosPath, len, L"%s%s", path, kernelPath + wcslen(szBuf));
return dosPath;
}
}
dosPath[0] = 0;
return dosPath;
}
int main()
{
HANDLE hFile = CreateFileW(L"d:\\test.txt",
GENERIC_READ,
FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE,
NULL,
OPEN_EXISTING,
0x00,
NULL);
if (INVALID_HANDLE_VALUE == hFile)
{
return 0;
}
WCHAR path[1024];
getFilePathByHandle(hFile, path, 1024);
WCHAR dosPath[1024];
getDosFilePath(dosPath, 1024, path);
CloseHandle(hFile);
return 0;
} |