使用 GetFinalPathNameByHandle 获取文件句柄对应的文件名
在进程空间内,获取了 NtWriteFile 的第一个参数文件句柄,可以通过 GetFinalPathNameByHandle 函数获取文件名称。
示例代码:
#include <windows.h>
#include <iostream>
using namespace std;
int main() {
HANDLE hFile = CreateFile(TEXT('test.txt'), GENERIC_READ | GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
if (hFile == INVALID_HANDLE_VALUE) {
cout << 'Failed to create file!' << endl;
return 1;
}
WCHAR path[MAX_PATH];
DWORD dwSize = GetFinalPathNameByHandle(hFile, path, MAX_PATH, FILE_NAME_NORMALIZED);
if (dwSize == 0) {
cout << 'Failed to get file path!' << endl;
CloseHandle(hFile);
return 1;
}
wcout << 'File path: ' << path << endl;
CloseHandle(hFile);
return 0;
}
输出结果:
File path: \\?\C:\Users\...\test.txt
原文地址: https://www.cveoy.top/t/topic/mB9G 著作权归作者所有。请勿转载和采集!