我正在尝试将内存映射文件用作:
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
有什么限制吗?有什么建议吗?
最佳答案
你需要检查返回值!崩溃的另一个原因可以在 MSDN 上的 MapViewOfFile 备注部分找到:
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.
就其他限制而言;显然, View 必须适合进程的虚拟内存空间,而 32 位进程通常总共只有 2GB。如果您正在处理千兆字节大小的文件,您需要一次映射较小的 View 而不是整个文件...
关于c++ - 使用 MapViewOfFile 有什么限制吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8025654/