45fan.com - 路饭网

搜索: 您的位置主页 > 网络频道 > 阅读资讯:如何根据文件句柄获取文件名?

如何根据文件句柄获取文件名?

2016-08-27 13:40:10 来源:www.45fan.com 【

如何根据文件句柄获取文件名?

#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);
}

 

本文地址:http://www.45fan.com/a/question/68441.html
Tags: 文件 获取 根据
编辑:路饭网
关于我们 | 联系我们 | 友情链接 | 网站地图 | Sitemap | App | 返回顶部