您的位置:首页 > 编程语言 > C语言/C++

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

2012-02-17 00:33 295 查看
因为不使用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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: