Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Memory Mapping function MapViewOfFile parameters

Posted on 2001-06-19
5
Medium Priority
?
618 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 200 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

Enroll in September's Course of the Month

This month’s featured course covers 16 hours of training in installation, management, and deployment of VMware vSphere virtualization environments. It's free for Premium Members, Team Accounts, and Qualified Experts!

Question has a verified solution.

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

Unlike C#, C++ doesn't have native support for sealing classes (so they cannot be sub-classed). At the cost of a virtual base class pointer it is possible to implement a pseudo sealing mechanism The trick is to virtually inherit from a base class…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
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.

722 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