Solved

Memory Mapping function MapViewOfFile parameters

Posted on 2001-06-19
5
587 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
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

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

760 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

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now