如何根据文件句柄获取文件名?
#include<windows.h> #include<stdio.h> #include<tchar.h> #include<string.h> #include<psapi.h> #defineBUFSIZE512 BOOLGetFileNameFromHandle(HANDLEhFile) { BOOLbSuccess=FALSE; TCHARpszFilename[MAX_PATH+1]; HANDLEhFileMap; //Getthefilesize. DWORDdwFileSizeHi=0; DWORDdwFileSizeLo=GetFileSize(hFile,&dwFileSizeHi); if(dwFileSizeLo==0&&dwFileSizeHi==0) { printf("Cannotmapafilewithalengthofzero./n"); returnFALSE; } //Createafilemappingobject. hFileMap=CreateFileMapping(hFile, NULL, PAGE_READONLY, 0, 1, NULL); if(hFileMap) { //Createafilemappingtogetthefilename. void*pMem=MapViewOfFile(hFileMap,FILE_MAP_READ,0,0,1); if(pMem) { if(GetMappedFileName(GetCurrentProcess(), pMem, pszFilename, MAX_PATH)) { //Translatepathwithdevicenametodriveletters. TCHARszTemp[BUFSIZE]; szTemp[0]='/0'; if(GetLogicalDriveStrings(BUFSIZE-1,szTemp)) { TCHARszName[MAX_PATH]; TCHARszDrive[3]=TEXT(":"); BOOLbFound=FALSE; TCHAR*p=szTemp; do { //Copythedrivelettertothetemplatestring *szDrive=*p; //Lookupeachdevicename if(QueryDosDevice(szDrive,szName,BUFSIZE)) { UINTuNameLen=_tcslen(szName); if(uNameLen<MAX_PATH) { bFound=_tcsnicmp(pszFilename,szName, uNameLen)==0; if(bFound) { //ReconstructpszFilenameusingszTemp //ReplacedevicepathwithDOSpath TCHARszTempFile[MAX_PATH]; _stprintf(szTempFile, TEXT("%s%s"), szDrive, pszFilename+uNameLen); _tcsncpy(pszFilename,szTempFile,MAX_PATH); } } } //GotothenextNULLcharacter. while(*p++); }while(!bFound&&*p);//endofstring } } bSuccess=TRUE; UnmapViewOfFile(pMem); } CloseHandle(hFileMap); } printf("Filenameis%s/n",pszFilename); return(bSuccess); }