Do not use on any
shared computer
September 6, 2008 03:09pm pdt
 
[x]
Attachment Details

How does this work??   MapViewOfFile ()

Hi,

Can someone explain to me how this is used please?
I read several explanation but I still don't quite understand. If you could explan to me in simplest terms, greatly appreciated.
When, why and how do you use it if this makes any sense??
Thanks much.

1) CreateFileMapping ()
2) MapViewOfFile ()
=====================
========================
MapViewOfFile Function

Maps a view of a file mapping into the address space of a calling process.

To specify a suggested base address for the view, use the MapViewOfFileEx function. However, this practice is not recommended.

Syntax

LPVOID WINAPI MapViewOfFile(
  __in  HANDLE hFileMappingObject,
  __in  DWORD dwDesiredAccess,
  __in  DWORD dwFileOffsetHigh,
  __in  DWORD dwFileOffsetLow,
  __in  SIZE_T dwNumberOfBytesToMap
);
Parameters
hFileMappingObject
A handle to a file mapping object. The CreateFileMapping and OpenFileMapping functions return this handle.

dwDesiredAccess
The type of access to a file mapping object, which ensures the protection of the pages. This parameter can be one of the following values.

Value Meaning
FILE_MAP_ALL_ACCESS
 Equivalent to FILE_MAP_WRITE and FILE_MAP_READ. The object must have been created with the PAGE_READWRITE option.
 
FILE_MAP_COPY
 A copy-on-write view of the file is mapped. The object must have been created with the PAGE_WRITECOPY option.

If the file mapping object is backed by the operating system paging file, the system commits physical storage from the paging file at the time that MapViewOfFile is called. The actual physical storage is not used until a thread in the process writes to an address in the view. At that time, the system copies the original page to a new page that is backed by the paging file, maps the page into the process address space, and changes the page protection to PAGE_READWRITE. The threads in the process can access only the local copy of the data, not the original data. If the page is ever trimmed from the working set of the process, it can be written to the paging file storage that is committed when MapViewOfFile is called.

This process only allocates physical memory when a virtual address is actually written to. Changes are never written back to the original file, and are freed when the thread in your process unmaps the view.

Paging file space for the entire view is committed when copy-on-write access is specified, because the thread in the process can write to every single page. Therefore, enough physical storage space must be obtained at the time MapViewOfFile is called.
 
FILE_MAP_EXECUTE
 An executable view of the file is mapped (mapped memory can be run as code). The object must have been created with the PAGE_EXECUTE_READWRITE or PAGE_EXECUTE_READ option.

Windows Server 2003 and Windows XP:  This value is available starting with Windows XP SP2 and Windows Server 2003 SP1.
Windows 2000:  This value is not supported.
FILE_MAP_READ
 A read-only view of the file is mapped. The object must have been created with the PAGE_READWRITE or PAGE_READONLY option.
 
FILE_MAP_WRITE
 A read/write view of the file is mapped. The object must have been created with the PAGE_READWRITE option.
 

For more information, see File Mapping Security and Access Rights.

dwFileOffsetHigh
A high-order DWORD of the file offset where the view begins.

dwFileOffsetLow
A low-order DWORD of the file offset where the view is to begin. The combination of the high and low offsets must specify an offset within the file mapping. They must also match the memory allocation granularity of the system. That is, the offset must be a multiple of the allocation granularity. To obtain the memory allocation granularity of the system, use the GetSystemInfo function, which fills in the members of a SYSTEM_INFO structure.

dwNumberOfBytesToMap
The number of bytes of a file mapping to map to the view. All bytes must be within the maximum size specified by CreateFileMapping. If this parameter is 0 (zero), the mapping extends from the specified offset to the end of the file mapping.

Return Value
If the function succeeds, the return value is the starting address of the mapped view.

1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
ReadFileData(LPSTR lpFileName)
{
//HFILE hFile;
HANDLE hFile;
char ofile[_MAX_FNAME+_MAX_EXT];
 
 
 
 
//hFile = _lopen(lpFileName, OF_READ);
hFile = CreateFile (lpFileName, GENERIC_READ, 0, NULL, OPEN_EXISTING, 0, 0);
if (hFile == INVALID_HANDLE_VALUE) return FALSE;
 
FileSize = GetFileSize (hFile, NULL);
 
if (hMap != NULL) 
	CloseHandle (hMap);
if (pRead != NULL) 
	UnmapViewOfFile (pRead);
 
hMap = CreateFileMapping (hFile, NULL, PAGE_READONLY, 0, 0, NULL);   <=====
if (hMap == NULL) return FALSE;
 
pRead = (PUCHAR) MapViewOfFile (hMap, FILE_MAP_READ, 0, 0, 0);      <=====
CloseHandle (hFile);
 
}
Start your free trial to view this solution
[x]
The Solution Rating System

With so many solutions, how can you tell which solutions are most likely to help you and which ones are not? To provide you with a tool to use, we rate our solutions based on various elements that most accurately determine if a solution is a quality solution. To explain what factors affect the solution rating, here are the elements we take into consideration when formulating our solution rating.

  • The Grade of the Solution
  • The Zone Rank of the Expert Providing the Solution
  • The Number of Author and Expert Comments
  • The Number of Experts Contributing
  • The Feedback of the Community

Your Input Matters
Because of the way the system is set up, the most important variable in this equation is you. As a member of Experts Exchange, you are able to cast your vote on the quality of the solutions in regard to how complete, accurate, helpful and easy to understand each solution is. When you provide your feedback, each rating is adjusted accordingly. So, if you see a solution that has a poor rating that you think is a good solution, let us know by rating it. As you do, the rating will be adjusted and will become more accurate for other members of our site.

If you have any suggestions that you would like to make for our rating system, please ask a question in the Suggestions Zone of Community Support.

Thank you!

Question Stats
Zone: Programming
Question Asked By: dkim18
Solution Provided By: jkr
Participating Experts: 1
Solution Grade: A
Views: 0
Translate:
Loading Advertisement...
 
[+][-]Expert Comment by jkr

Rank: Genius

Expert Comment by jkr:

All comments and solutions are available to Premium Service Members only.

Start your 7-day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
 
[+][-]Expert Comment by jkr

Rank: Genius

Expert Comment by jkr:

All comments and solutions are available to Premium Service Members only.

Start your 7-day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
 
[+][-]Author Comment by dkim18
Author Comment by dkim18:

All comments and solutions are available to Premium Service Members only.

Start your 7-day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
 
[+][-]Accepted Solution by jkr

Rank: Genius

Accepted Solution by jkr:

All comments and solutions are available to Premium Service Members only.

Start your 7-day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
 
Loading Advertisement...
20080723-EE-VQP-34 / EE_QW_2_20070628