Solved

Memory Mapping function MapViewOfFile parameters

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
c++ how to tell if the progra is ctl or mfc atl ect 6 64
SendMessage fails while PostMessage works 2 107
how to understand recursion 12 218
C++ Language error 28 189
Introduction This article is the first in a series of articles about the C/C++ Visual Studio Express debugger.  It provides a quick start guide in using the debugger. Part 2 focuses on additional topics in breakpoints.  Lastly, Part 3 focuses on th…
C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (http://www.experts-exchange.com/Programming/Languages/CPP/A_3912-Object-Properties-in-C.ht…
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 clear a vector as well as how to detect empty vectors in C++.

911 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

17 Experts available now in Live!

Get 1:1 Help Now