Solved

Memory Mapping function MapViewOfFile parameters

Posted on 2001-06-19
5
608 Views
Last Modified: 2008-02-20
I am learning how to memory map a large file...

I can map the entire file to a character buffer as follows just fine using the following call:


lpMMFile = (char *) MapViewOfFile (hInMap,
                                  FILE_MAP_READ,
                                  0,
                                  0,
                                  0);

Parameter 3 is the upper DWORD of the file offset
Parameter 4 is the lower DWORD
Parameter 5 is the number of bytes to transfer

When all three are set to 0, the whole file is mapped to lpMMFile. My program works just fine with these arguments.

I don't know how to use parameters 3 through 5 to select parts of the file into RAM.

Isn't a DWORD an 8 bit binary? I can't make it work.

For example, how would I select the contents starting at offset 20 and load 50 bytes into the char buffer?

My program displays (null).

0
Comment
Question by:jtm111
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
5 Comments
 
LVL 1

Expert Comment

by:ua1zcl
ID: 6208725
Minimal unit you can MapView is one page i.e. 4096 bytes
on Windows95/98 and similar systems.
0
 
LVL 7

Expert Comment

by:KangaRoo
ID: 6209145
A DWORD stands dor Double WORD and is a 32 bit integer. On Win9x systems the hihg dword of the file offset is supposed to be 0
0
 

Expert Comment

by:janiv
ID: 6209689
The maximum length of file is 64 bit number.
Windows used to support only 32 bit variable, so in
order to represent every address in the system, U must use
2 DWORDS (hence, 64 bit variable).

Generally, Parameter 3 - the high-order DWORD of offset is 0.
Parameter 4 - will be the offset of the file

and parameter 5 - will be the size that U wanna map.

Good luck
0
 
LVL 1

Accepted Solution

by:
xutao earned 50 total points
ID: 6210351
From your words, I think the problem is: you didn't give a correct start map address. When you use File Mapping object, you must always keep one thing in mind - the system's memory allocation granularity. For example, in Window 2000 it is 64K.
I suggest that before you begin to use file mapping, you should first get your system's memory allocation granularity. You can use GetSystemInfo(...) to get a point to the SYSTEM_INFO structure, then check its dwPageSize member. After that, when you need to use MapViewOfFile(...), you must set the start map offset to be a multiple of the allocation granularity. (eg. in Win2000 it should be 0, 64k, 128k...).
To your question, if you need to map offset address 20 of a file (50 bytes long), you must map the view from start address 0, give the map size at least 70 (first 20 bytes unused indeed).
Good luck.
0
 
LVL 3

Author Comment

by:jtm111
ID: 6211308
Thanks for the help everybody. I was using the DWORD correctly after all (casting hex values into DWORD), but I didn't know about the granularity issue so I was just flailing randomly. For benefit of others, I used the following code to get my offset and it seems to work:

LPSYSTEM_INFO si;
si = new SYSTEM_INFO;
GetSystemInfo (si);

DWORD dwOffsetLow = (DWORD) si->dwAllocationGranularity * (some integer multiplier);

Windows 98 also uses 65,536.

0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
Many modern programming languages support the concept of a property -- a class member that combines characteristics of both a data member and a method.  These are sometimes called "smart fields" because you can add logic that is applied automaticall…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.

617 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question