如何从文件的句柄得到文件名,用API函数.

来源:百度知道 编辑:UC知道 时间:2024/05/16 14:33:17
查了一个MSDN,并没有找到相应的函数.如果仅仅知道一个打开文件的句柄能否知道它的文件名呢?
CFile并不是Windows自带的API方法,它很可能在内部保存了文件的名称.GetFileName方法处理的是字串,并不是从句柄得到的.很可能根本没有这样的API,或者说,文件句柄(内部是一个文件对象)根本没有文件路径信息,实际上也就根本无法获得.

方法一:CFile::GetFileName,实用 方便
msdn实例

try
{
// try to open the file
CFile sysFile(_T("C:\\WINDOWS\\SYSTEM.INI"), CFile::modeRead);

// print out path name and title information
_tprintf(_T("Path is : \"%s\"\n"), (LPCTSTR) sysFile.GetFilePath());
_tprintf(_T("Name is : \"%s\"\n"), (LPCTSTR) sysFile.GetFileName());
_tprintf(_T("Title is: \"%s\"\n"), (LPCTSTR) sysFile.GetFileTitle());

// close the file handle
sysFile.Close();
}
catch (CFileException* pEx)
{
// if an error occurs, just make a message box
pEx->ReportError();
pEx->Delete();
}

Output

Path is : "C:\WINDOWS\SYSTEM.INI"
Name is : "SYSTEM.INI"
Title is: "