您的位置:首页 > 其它

使用文件映射获取打开的文件名

2013-08-14 10:39 155 查看
#include "stdafx.h"
#include <Windows.h>
#include <stdio.h>
#include <tchar.h>
#include <cstring>
#include <psapi.h>
#include <psapi.h>
#include <strsafe.h>
#pragma comment(lib, "psapi.lib")

LPCTSTR GetFileNameFromHandle(HANDLE hFile)
{
static TCHAR pszFilename[MAX_PATH+1];
memset(pszFilename, 0, sizeof(pszFilename));

HANDLE hFileMap = NULL;
void* pMapMem = NULL;

do
{
// Get the file size.
DWORD dwFileSizeHi = 0;
DWORD dwFileSizeLo = GetFileSize(hFile, &dwFileSizeHi);

if( dwFileSizeLo == 0 && dwFileSizeHi == 0 )
{
_tprintf( _T("Cannot map a file with a length of zero.\n") );
break;
}

// Create a file mapping object.
DWORD dwMapSize = min(((ULONGLONG)dwFileSizeHi + (ULONGLONG)dwFileSizeLo), 64);
hFileMap = CreateFileMapping(hFile,
NULL,
PAGE_READONLY,
0,
dwMapSize,
NULL);
if (hFileMap == NULL)
{
_tprintf(_T("CreateFileMapping failed with : %ld\n"), GetLastError());
break;
}

// Create a file mapping to get the file name.
pMapMem = MapViewOfFile(hFileMap, FILE_MAP_READ, 0, 0, 1);
if (pMapMem == NULL)
{
_tprintf( _T("MapViewOfFile failed with : %ld\n"), GetLastError());
break;
}

if (!GetMappedFileName (GetCurrentProcess(), pMapMem, pszFilename, MAX_PATH))
{
_tprintf( _T("GetCurrentProcess failed with : %ld\n"), GetLastError());
break;
}

// Translate path with device name to drive letters.
const int BUFSIZE = 512;

TCHAR szTemp[BUFSIZE + 1];
szTemp[0] = '\0';

if (!GetLogicalDriveStrings(BUFSIZE, szTemp))
{
_tprintf( _T("GetLogicalDriveStrings failed with : %ld\n"), GetLastError());
break;
}

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, BUFSIZE))
{
UINT uNameLen = _tcslen(szName);

if (uNameLen < MAX_PATH)
{
bFound = _tcsnicmp(pszFilename, szName, uNameLen) == 0;

if (bFound)
{
// Reconstruct pszFilename using szTempFile
// Replace device path with DOS path
TCHAR szTempFile[MAX_PATH];
StringCchPrintf(szTempFile,
MAX_PATH,
TEXT("%s%s"),
szDrive,
pszFilename+uNameLen);
StringCchCopyN(pszFilename, MAX_PATH+1, szTempFile, _tcslen(szTempFile));

_tprintf(TEXT("File name is %s\n"), pszFilename);

break;
}
}
}

// Go to the next NULL character.
while (*p++);
} while (!bFound && *p); // end of string

}while(0);

if(pMapMem)
{
UnmapViewOfFile(pMapMem);
pMapMem = NULL;
}

if(hFileMap)
{
CloseHandle(hFileMap);
hFileMap = NULL;
}

return(pszFilename);
}


//搜索C:\\*.txt的文件做测试
int Test()
{
HANDLE hFile;
HANDLE hFind;
WIN32_FIND_DATA wfd = {0};
TCHAR szPath[MAX_PATH] = { _T("c:\\") };
TCHAR szFile[MAX_PATH] = {0};
_tcscpy_s(szFile, szPath);
_tcscat_s(szFile, _T("*.txt"));
hFind = FindFirstFile(szFile, &wfd);
if (hFind == INVALID_HANDLE_VALUE)
{
_tprintf( _T("can not find a file") );
return 1;
}

do
{
_tprintf( _T("find %s at current dir\n"), wfd.cFileName);

_tcscpy_s(szFile, szPath);
_tcscat_s(szFile, wfd.cFileName);

hFile = CreateFile(szFile,
GENERIC_READ,
0,
NULL,
OPEN_EXISTING,
FILE_ATTRIBUTE_NORMAL,
NULL);
if (hFile == INVALID_HANDLE_VALUE)
{
_tprintf( _T("create file error, %d"), GetLastError());
}
else
{
GetFileNameFromHandle(hFile);
CloseHandle(hFile);
}
}while(FindNextFile(hFind, &wfd));

FindClose(hFind);

system("pause");
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐