我用MapViewOfFile取数据,当地2次使用这个函数取数据,指针是在文件开头还是在上次取数据的尾部。
还是有偏移量的参数改如何设置,csdn上说是粒度的倍数,怎么知道粒度是多少。

解决方案 »

  1.   

    粒度可以通过GetSystemInfo得到
    即:
    SYSTEM_INFO::dwPageSize
      

  2.   

    内存地址空间是按页(page)进行倍数分配/对齐的, 详见Windows核心编程.
    不同操作系统的页大小是不一样的.
      

  3.   

    MapViewOfFile映射的文件位置是由参数指定的,内存分配粒度用GetSystemInfo函数来取。看MSDN中MapViewOfFile的参数说明:
    dwFileOffsetHigh 
    [in] A high-order DWORD of the file offset where the view begins. 
    dwFileOffsetLow 
    [in] A low-order DWORD of the file offset where the view begins. The combination of the high and low offsets must specify an offset within the file that matches the memory allocation granularity of the system, or the function fails. That is, the offset must be a multiple of the allocation granularity. To obtain the memory allocation granularity of the system, use the GetSystemInfo function, which fills in the members of a SYSTEM_INFO structure. 
      

  4.   

    不能移动5个字节,必须是内存分配粒度的整数倍。如果要访问偏移5字节处的数据应该直接使用View的指针。
      

  5.   

    取内存分配粒度是用GetSystemInfo函数,访问映射的数据直接用MapViewOfFile返回的指针(强制转换为自己需要的类型,例如PBYTE)。
      

  6.   

    内存映射文件映射后返回的结果,其用法就与正常的指针使用一样。至于映射从什么地方开始,是由你的mapviewoffile的操作决定的。
    LPVOID pvMapHead = MapViewOfFile(...);
    DWORD Offset = 5;
    LPVOID pvTar = (LPVOID)((char*)pvMapHead+Offset);
      

  7.   

    MapViewOfFile()返回的是系统分配粒度整倍数的指针,如果粒度是64K,你想映射的起始的位置是2*64+1K,映射长度是3K。
    那么,借用楼上的部分代码,
    DWORD dwhigh = 0;
    DWORD dwlow = 2 * 64 * 1024;
    DWORD dwNumofBytes = 3 * 1024;
    LPVOID pvMapHead = MapViewOfFile(hFile,FILE_MAP_READ,dwhigh,dwlow,dwNumofBytes);
    DWORD offset = 1024;
    LPVOID pvTar = (LPVOID)((char*)pvMapHead+Offset);