zoukankan      html  css  js  c++  java
  • 【转载】从文件句柄获得全路径

     1 typedef NTSTATUS (NTAPI *PFN_ZwQueryObject)( 
     2     IN HANDLE ObjectHandle, 
     3     IN ULONG ObjectInformationClass, 
     4     OUT PVOID ObjectInformation, 
     5     IN ULONG ObjectInformationLength, 
     6     OUT PULONG ReturnLength OPTIONAL 
     7     ); 
     8 
     9 PFN_ZwQueryObject g_ZwQueryObject = (PFN_ZwQueryObject)GetProcAddress(GetModuleHandle(TEXT("ntdll.dll")),"ZwQueryObject");
    10 
    11 
    12   
    13 BOOL GetPathByHandle(HANDLE hFile, LPWSTR lpBuf, DWORD nBuf) 
    14 { 
    15     ULONG m, n; 
    16     WCHAR lpPath[MAX_PATH+4]; 
    17     WCHAR lpDrive[MAX_PATH]; 
    18     WCHAR lpDevName[MAX_PATH]; 
    19     if (g_ZwQueryObject(hFile, 1, lpPath, MAX_PATH+4, &m) >= 0 && 
    20         (m = GetLogicalDriveStringsW(MAX_PATH, lpDrive)) && m < MAX_PATH) 
    21     { 
    22         WCHAR *p = lpDrive; 
    23         while (m = wcslen(p)) 
    24         { 
    25             p[m-1] = L'\0'; 
    26             n = QueryDosDeviceW(p, lpDevName, MAX_PATH); 
    27             if (n && n < MAX_PATH) 
    28             { 
    29                 n = wcslen(lpDevName); 
    30                 if (!wcsnicmp(lpPath+4, lpDevName, n)) 
    31                 { 
    32                     wcsncpy(lpBuf, p, nBuf); 
    33                     if (nBuf > 2) wcsncpy(lpBuf+2, lpPath+4+n, nBuf-2); 
    34                     return TRUE; 
    35                 } 
    36             } 
    37             p += m + 1; 
    38         } 
    39     } 
    40     return FALSE; 
    41 } 
  • 相关阅读:
    22.json&pickle&shelve
    22.BASE_DIR,os,sys
    21.time和random
    21.模块的执行以及__name__
    21.python的模块(Module)和包(Package)
    21. 对文件进行查询修改等操作
    20.装饰器和函数闭包
    19.python基础试题(三)
    19.生产者消费者模型
    19.yield和send的区别
  • 原文地址:https://www.cnblogs.com/02xiaoma/p/2759808.html
Copyright © 2011-2022 走看看