怎么样通过 fopen返回的FILE句柄 找到文件名

C语言 码拜 8年前 (2016-04-25) 995次浏览
如                               题
解决方案

50

#pragma comment(lib,"psapi")
#include <windows.h>
#include <stdio.h>
#include <tchar.h>
#include <string.h>
#include <psapi.h>
#include <io.h>
#define BUFSIZE 512
BOOL GetFileNameFromHandle(HANDLE hFile) {
    TCHAR pszFilename[MAX_PATH+1];
    HANDLE hFileMap;
    BOOL bSuccess = FALSE;
    DWORD dwFileSizeHi = 0;
    DWORD dwFileSizeLo = GetFileSize(hFile, &dwFileSizeHi);
    if( dwFileSizeLo == 0 && dwFileSizeHi == 0 ) {
        _tprintf(TEXT("Cannot map a file with a length of zero.\n"));
        return FALSE;
    }
    hFileMap = CreateFileMapping(hFile,NULL,PAGE_READONLY,0,1,NULL);
    if (hFileMap) {
        // Create a file mapping to get the file name.
        void* pMem = MapViewOfFile(hFileMap, FILE_MAP_READ, 0, 0, 1);
        if (pMem) {
            if (GetMappedFileNameA (GetCurrentProcess(),pMem,pszFilename,MAX_PATH)) {
                // Translate path with device name to drive letters.
                TCHAR szTemp[BUFSIZE];
                szTemp[0] = "\0";
                if (GetLogicalDriveStrings(BUFSIZE-1, szTemp)) {
                    TCHAR szName[MAX_PATH];
                    TCHAR szDrive[3] = TEXT(" :");
                    BOOL bFound = FALSE;
                    TCHAR* p = szTemp;
                    do {
                        // Copy the drive letter to the template string
                        *szDrive = *p;
                        // Look up each device name
                        if (QueryDosDevice(szDrive, szName, MAX_PATH)) {
                            size_t uNameLen = _tcslen(szName);
                            if (uNameLen < MAX_PATH) {
                                bFound = _tcsnicmp(pszFilename, szName, uNameLen) == 0
                                      && *(pszFilename + uNameLen) == _T("\");
                                if (bFound) {
                                    // Reconstruct pszFilename using szTempFile
                                    // Replace device path with DOS path
                                    TCHAR szTempFile[MAX_PATH];
                                    sprintf(szTempFile,
                                            TEXT("%s%s"),
                                            szDrive,
                                            pszFilename+uNameLen);
                                    strcpy(pszFilename, szTempFile);
                                }
                            }
                        }
                        // Go to the next NULL character.
                        while (*p++);
                    } while (!bFound && *p); // end of string
                }
            }
            bSuccess = TRUE;
            UnmapViewOfFile(pMem);
        }
        CloseHandle(hFileMap);
    }
    _tprintf(TEXT("File name is %s\n"), pszFilename);
    return(bSuccess);
}
int _tmain(int argc, TCHAR *argv[]) {
    FILE *f;
    HANDLE hFile;
    if( argc != 2 ) {
        _tprintf(TEXT("This sample takes a file name as a parameter.\n"));
        return 0;
    }
    f=fopen(argv[1],"r");
    if (NULL==f) return 0;
    hFile=(HANDLE)_get_osfhandle(fileno(f));
    //  hFile = CreateFile(argv[1], GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, 0, NULL);
    if (hFile == INVALID_HANDLE_VALUE) {
        _tprintf(TEXT("CreateFile failed with %d\n"), GetLastError());
        return 0;
    }
    GetFileNameFromHandle( hFile );
    fclose(f);
    return 0;
}
//C:\test>test c:\windows\system32\kernel32.dll
//File name is C:\WINDOWS\system32\kernel32.dll
//

CodeBye 版权所有丨如未注明 , 均为原创丨本网站采用BY-NC-SA协议进行授权 , 转载请注明怎么样通过 fopen返回的FILE句柄 找到文件名
喜欢 (0)
[1034331897@qq.com]
分享 (0)