使用MapViewOfFile有任何限制吗? [英] Is there any limitation for using MapViewOfFile?
问题描述
我正在尝试将内存映射文件用作:
I am trying to use memory-mapped files as:
hFile = ::CreateFile(State.Path, GENERIC_READ, FILE_SHARE_READ | FILE_SHARE_WRITE,
0, OPEN_EXISTING,FILE_FLAG_SEQUENTIAL_SCAN, 0);//open the file
if(hFile !=INVALID_HANDLE_VALUE){
hMap= ::CreateFileMapping(hFile, 0, PAGE_READONLY | SEC_COMMIT, 0, 0, 0);//create Mem mapping for the file in virtual memory
if( hMap!=NULL){
base = ::MapViewOfFile(hMap, FILE_MAP_READ, 0, 0, 0);//load the mapped file into the RAM
//start to compare some bytes (values) from mspaint.exe file in Win7
if( *((BYTE *)base + 0x1C3DF0)== 0x05 )
i++;
if( *((BYTE *)base + 0x25250C)== 0x21 )
i++;
if( *((BYTE *)base + 0x25272A)== 0x97 )
i++;
if(i==3){
// the file is malicious
}
一旦文件大小以GB为单位,MapViewOfFile
函数将停止工作,应用程序将崩溃!使用MapViewOfFile
有什么限制吗?有什么建议吗?
Once the file size be in Gigabytes, the MapViewOfFile
function stop working and the application will be crashed! Is there any limitation to use MapViewOfFile
? Any suggestion?
推荐答案
您需要检查返回值!崩溃的另一个原因可以在MSDN上的MapViewOfFile备注部分中找到:
You need to check the return value! The other reason for a crash can be found in the MapViewOfFile remarks section on MSDN:
为防止发生EXCEPTION_IN_PAGE_ERROR异常,请使用结构化 异常处理,以保护任何写入或读取的代码 页面文件以外的文件的内存映射视图.
To guard against EXCEPTION_IN_PAGE_ERROR exceptions, use structured exception handling to protect any code that writes to or reads from a memory mapped view of a file other than the page file.
就其他限制而言;显然,视图必须适合您的进程的虚拟内存空间,而32位进程通常总共只有2gb.如果要使用千兆字节大小的文件,则需要映射较小的视图,而不是一次映射整个文件...
As far as other limitations go; clearly the view has to fit in the virtual memory space of your process and a 32 bit process normally only has 2gb in total. If you are working with gigabyte sized files you need to map smaller views and not the whole file at once...
这篇关于使用MapViewOfFile有任何限制吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!