使用 MapViewOfFile 有什么限制吗?

发布于 2024-12-14 02:28:13 字数 844 浏览 0 评论 0原文

我尝试将内存映射文件用作:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

梦里的微风 2024-12-21 02:28:13

你需要检查返回值!崩溃的另一个原因可以在 MSDN 上的 MapViewOfFile 备注部分找到:

要防止 EXCEPTION_IN_PAGE_ERROR 异常,请使用结构化
异常处理以保护任何写入或读取的代码
除页面文件之外的文件的内存映射视图。

至于其他限制;显然,视图必须适合进程的虚拟内存空间,而 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:

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.

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...

淡墨 2024-12-21 02:28:13

您的应用程序正在崩溃,因为您没有检查 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.

╰ゝ天使的微笑 2024-12-21 02:28:13

您正在尝试一次将整个文件映射到内存中。您没有足够的虚拟内存来执行此操作。仅映射您实际需要的部分。这就是为什么 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文