内存映射的文件访问 c++ 类

本文介绍了一个轻量级的文件信息处理类_CFileInfo_的设计与实现,该类提供了文件的基本属性读取、文件数据读取等功能,并支持内存自动释放。

因为不使用MFC 所以不能使用CFile ,自己动手写一个轻量级的,最重要的还是方便 内存的自动释放--通过析构函数。

#ifndef __CFileInfoLib__
#define  __CFileInfoLib__
#endif
class _CFileInfo_
{
public: 
	//以默认的方式打开以存在的文件
	_CFileInfo_(__in TCHAR * pszFile,__in DWORD flProtect );
    _CFileInfo_(__in TCHAR *pszFile,__in DWORD flProtect ,__in DWORD dwDesiredAccess ,__in DWORD dwShareMode ,__in LPSECURITY_ATTRIBUTES lpSecurityAttributes ,__in DWORD dwCreationDisposition ,__in DWORD dwFlagsAndAttributes,__in HANDLE hTemplateFile );
    virtual ~_CFileInfo_();
public:
    //通过偏移位置,偏移量,读取文件数据
	//dwFileOffsetHigh 组合dwFileOffsetLow,dwNumberOfBytesToMap,至少有一个 必须是64k的整倍数
	/*
LPBYTE lpdata=ins.GetFileData(0,64*1024,500);
 lpdata=ins.GetFileData(0,0,64*1024);
//下面的调用 是不合法的
lpdata=ins.GetFileData(0,200,200); 因为没有对齐 64k分配粒度
	*/
	LPBYTE GetFileData(
		__in          DWORD dwDesiredAccess,
		__in          DWORD dwFileOffsetHigh,
		__in          DWORD dwFileOffsetLow,
		__in          SIZE_T dwNumberOfBytesToMap);


	DWORD FileAttributes();
    
	unsigned __int64 FileSize();

	//创建时间,不要使用返回值内存
const	LPFILETIME CreateTime()const;
const	LPFILETIME LastWriteTime()const;
const	LPFILETIME LastAccessTime()const;
//获取文件路径,不要释放返回值
const TCHAR * GetFilePath()const;

	//获取系统的分配粒度
	DWORD GetSystemAllocationGranularity();
protected:
HANDLE m_hFile;
unsigned __int64 m_FileSize;
FILETIME m_ct;
FILETIME m_lwt;
FILETIME m_lat;
//文件路径
TCHAR *  m_pszpath;

private: 
HANDLE m_hMap;
LPVOID m_MapData;
DWORD dwAllocationGranularity;//分配粒度
};

#ifndef CFileInfo
typedef _CFileInfo_ CFileInfo;
#endif

源文件

#include "stdafx.h"

#include "strsafe.h"

#include "FileInfo.h"

#pragma comment(lib,"strsafe.lib")

_CFileInfo_::_CFileInfo_(__in TCHAR * pszFile,__in DWORD flProtect )
{
	this->m_hFile=CreateFile(pszFile,GENERIC_WRITE | GENERIC_READ,FILE_SHARE_READ,NULL,OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL,NULL);
	if(this->m_hFile==INVALID_HANDLE_VALUE) throw -6;
	DWORD dwhigh=0;
	this->m_FileSize=(unsigned __int64)GetFileSize(m_hFile,&dwhigh);
	this->m_FileSize+=(((unsigned __int64)dwhigh)<<32);
	this->m_hMap=CreateFileMapping(
		this->m_hFile,
		NULL,
		flProtect,
		/*  (DWORD)(m_FileSize>>32)*/0,
		/*(DWORD)(m_FileSize&0xFFFFFFFF)*/0,
		NULL
		);
	if(!this->m_hMap)
	{
		CloseHandle(this->m_hFile);
		throw 0;
	}
	SYSTEM_INFO sysinfo;
	GetSystemInfo(&sysinfo);
	this->dwAllocationGranularity=sysinfo.dwAllocationGranularity;

	GetFileTime(m_hFile,&m_ct,&m_lat,&m_lwt);
	size_t len=0;
	StringCbLength(pszFile,MAX_PATH*sizeof(TCHAR),&len);
	len+=sizeof(TCHAR);
	this->m_pszpath =(TCHAR*)malloc(len);
	if(!this->m_pszpath)
	{
		CloseHandle(m_hFile);
		CloseHandle(m_hMap);
		throw 0;
	}
	StringCbCopy(this->m_pszpath,len,pszFile);

}

_CFileInfo_::_CFileInfo_(__in TCHAR *pszFile,__in DWORD flProtect ,__in DWORD dwDesiredAccess ,__in DWORD dwShareMode ,__in LPSECURITY_ATTRIBUTES lpSecurityAttributes ,__in DWORD dwCreationDisposition ,__in DWORD dwFlagsAndAttributes,__in HANDLE hTemplateFile )
{
	this->m_hFile=CreateFile(pszFile,dwDesiredAccess,dwShareMode,lpSecurityAttributes,dwCreationDisposition,dwFlagsAndAttributes,hTemplateFile);
	if(this->m_hFile==INVALID_HANDLE_VALUE) throw -6;
	DWORD dwhigh=0;
	this->m_FileSize=(unsigned __int64)GetFileSize(m_hFile,&dwhigh);
	this->m_FileSize+=(((unsigned __int64)dwhigh)<<32);
	this->m_hMap=CreateFileMapping(
		this->m_hFile,
		NULL,
		flProtect,
		/*  (DWORD)(m_FileSize>>32)*/0,
		/*(DWORD)(m_FileSize&0xFFFFFFFF)*/0,
		NULL
		);
	if(!this->m_hMap)
	{
		CloseHandle(this->m_hFile);
		throw 0;
	}
	SYSTEM_INFO sysinfo;
	GetSystemInfo(&sysinfo);
	this->dwAllocationGranularity=sysinfo.dwAllocationGranularity;

	GetFileTime(m_hFile,&m_ct,&m_lat,&m_lwt);

	size_t len=0;
	StringCbLength(pszFile,MAX_PATH*sizeof(TCHAR),&len);
	len+=sizeof(TCHAR);
	this->m_pszpath =(TCHAR*)malloc(len);
	if(!this->m_pszpath)
	{
		CloseHandle(m_hFile);
		CloseHandle(m_hMap);
		throw 0;
	}
	StringCbCopy(this->m_pszpath,len,pszFile);
}

_CFileInfo_::~_CFileInfo_()
{        
	CloseHandle(this->m_hFile);
	if(this->m_MapData)
		UnmapViewOfFile(this->m_MapData);
	CloseHandle(this->m_hMap);
	if(this->m_pszpath) free(this->m_pszpath);
}

LPBYTE _CFileInfo_::GetFileData(__in DWORD dwDesiredAccess,__in DWORD dwFileOffsetHigh, __in DWORD dwFileOffsetLow, __in SIZE_T dwNumberOfBytesToMap)
{
	if(m_MapData){
		UnmapViewOfFile(m_MapData);
	}
	m_MapData=MapViewOfFile(this->m_hMap,dwDesiredAccess,dwFileOffsetHigh,dwFileOffsetLow,dwNumberOfBytesToMap);
	DWORD dw=GetLastError();
	return (LPBYTE)m_MapData;
}

unsigned __int64 _CFileInfo_::FileSize()
{
	return this->m_FileSize;
}

DWORD _CFileInfo_::GetSystemAllocationGranularity()
{
	return this->dwAllocationGranularity;
}

const LPFILETIME _CFileInfo_::CreateTime() const
{
	return (const LPFILETIME) &this->m_ct;
}

const LPFILETIME _CFileInfo_::LastAccessTime() const
{
	return(const LPFILETIME) &this->m_lat;
}

const LPFILETIME _CFileInfo_::LastWriteTime() const
{
	return(const LPFILETIME)  &this->m_lwt;
}

const TCHAR * _CFileInfo_::GetFilePath() const
{
	return (const TCHAR *)this->m_pszpath;
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值