使用 MapViewOfFile 有什么限制吗?
我尝试将内存映射文件用作:
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
}
一旦文件大小达到千兆字节,MapViewOfFile
函数就会停止工作,应用程序将崩溃!使用 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
}
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
你需要检查返回值!崩溃的另一个原因可以在 MSDN 上的 MapViewOfFile 备注部分找到:
至于其他限制;显然,视图必须适合进程的虚拟内存空间,而 32 位进程通常总共只有 2GB。如果您正在处理千兆字节大小的文件,您需要映射较小的视图,而不是一次映射整个文件......
You need to check the return value! The other reason for a crash can be found in the MapViewOfFile remarks section on MSDN:
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
是否有错误 - 如果失败,该函数将返回 0。至于限制,该函数无法映射大于虚拟内存空间的最大连续空闲块的块。如果你的程序是32位的,那么你只有大约2GB的可用虚拟内存;最大的连续空闲块将会更小,因为您的程序二进制文件、DLL、主线程堆栈和堆将被分配在内存空间的不同位置。
Your application is crashing, because you're not checking
MapViewOfFile
for errors -- the function returns 0 if it fails.As for the limitations, the function cannot map a block that is larger than the largest contiguous free block of your virtual memory space. If your program is 32-bit, then you have only about 2GB of available virtual memory; the largest contiguous free block will be smaller as your program binary, DLLs, your main thread stack and your heap will be allocated at various places across the memory space.
您正在尝试一次将整个文件映射到内存中。您没有足够的虚拟内存来执行此操作。仅映射您实际需要的部分。这就是为什么 CreateFileMapping() 和 MapViewOfFile() 允许您指定映射的大小和偏移量。我使用 MMF 来访问多 GB 文件,您肯定必须将映射的大小保持在最小,只需将视图滑动到您需要的位置即可。
You are trying to map the entire file into memory at one time. You don't have enough virtual memory to do that. Map just the portion that you actually need. That is why CreateFileMapping() and MapViewOfFile() allow you to specify sizes and offsets for mapping. I use MMFs to access multi-GB files, you definately have to keep the size of the mapping to a minimum, just slide the view around where you need it.