Solved

Memory Mapping function MapViewOfFile parameters

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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
Often, when implementing a feature, you won't know how certain events should be handled at the point where they occur and you'd rather defer to the user of your function or class. For example, a XML parser will extract a tag from the source code, wh…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

827 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