MapViewOfFile返回什么? [英] What does MapViewOfFile return?

查看:330
本文介绍了MapViewOfFile返回什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可能重复:
为什么加载程序不会在所需位置加载

Possible Duplicate:
Why won't the loader load at the desired location

"MapViewOfFile",此功能是否将文件映射到虚拟内存并返回映射内存的基址?如果是,则以下代码应输出0X400000,因为默认情况下,exe加载在此位置,但输出为0X360000.为什么?

"MapViewOfFile", does this function map a file into the virtual memory and return the base address of the mapped memory?? If yes, then the following code should output 0X400000, beacuse by default, exe's are loaded at this location, but the output is 0X360000. Why??

#include<iostream>
#include<Windows.h>
#include<stdio.h>
#include<WinNT.h>


int main()
{


HANDLE  hFile,hFileMapping;
LPVOID lpFileBase;


if((hFile = CreateFile(TEXT("c:\\linked list.exe"),GENERIC_READ,FILE_SHARE_READ,NULL,OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL,0)) == INVALID_HANDLE_VALUE)
    std::cout<<"unable to open";

if((hFileMapping = CreateFileMapping(hFile,NULL,PAGE_READONLY,0,0,NULL)) == 0)
{
    CloseHandle(hFile);
    std::cout<<"unable to open for mapping";
}

if((lpFileBase = MapViewOfFile(hFileMapping,FILE_MAP_READ,0,0,0))== 0)
{
    CloseHandle(hFile);
    CloseHandle(hFileMapping);
    std::cout<<"couldn't map view of file";
}

printf("%x\n",lpFileBase);

}

推荐答案

您研究的0X400000与普通文件映射无关.

The 0X400000 you researched has nothing to do with normal file mapping.

您可以想象 MapViewOfFile作为您要打开的文件的malloc + memcpy,仅此而已(相反,malloc可以使用平板的内存映射).因此,MapViewOfFile通常只选择一个地址,使其可以连续地在内存中容纳文件视图的字节.

You can imagine MapViewOfFile as a malloc+memcpy of the file you are opening, nothing more (under the hood it is the reverse: malloc can use a slab'ed memory mapping). So MapViewOfFile normally just chooses an address where it can fit the file view's bytes continuously in memory.

您可能想要的(因为您正在尝试映射.exe)是使用它创建一个新进程

What you probably want (since you are trying to map an .exe) is to create a new Process with it CreateProcess.

如果您确实需要将文件映射到特定地址,则可以使用

If you really need the file to be mapped at a specific address you can use MapViewOfFileEx, but there are no guarantees.

这篇关于MapViewOfFile返回什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆