Muokkaa

Jaa


Creating a File View

To map the data from a file to the virtual memory of a process, you must create a view of the file. The MapViewOfFile and MapViewOfFileEx functions use the file mapping object handle returned by CreateFileMapping to create a view of the file or a portion of the file in the process's virtual address space. These functions fail if the access flags conflict with those specified when CreateFileMapping created the file mapping object.

The MapViewOfFile function returns a pointer to the file view. By dereferencing a pointer in the range of addresses specified in MapViewOfFile, an application can read data from the file and write data to the file. Writing to the file view results in changes to the file mapping object. The actual writing to the file on disk is handled by the system. Data is not actually transferred at the time the file mapping object is written to. Instead, much of the file input and output (I/O) is cached to improve general system performance. Applications can override this behavior by calling the FlushViewOfFile function to force the system to perform disk transactions immediately.

The MapViewOfFileEx function works exactly like the MapViewOfFile function except that it allows a process to specify the base address of the view of the file in the process's virtual address space in the lpvBase parameter. If there is not enough space at the specified address, the call fails. Therefore, if you must map a file to the same address in multiple processes, the processes should negotiate an appropriate address: The lpvBase parameter must be an integral multiple of the system memory allocation granularity or the call fails. To obtain the system's memory allocation granularity, use the GetSystemInfo function, which fills in the members of a SYSTEM_INFO structure.

An application can create multiple file views from the same file mapping object. A file view can be a different size than the file mapping object from which it is derived, but it must be smaller than the file mapping object. The offset specified by the dwOffsetHigh and dwOffsetLow parameters of MapViewOfFile must be a multiple of the allocation granularity of the system.

Creating a View Within a File