Creating a File-Mapping Object
The first step in mapping a file is to open the file by calling the
CreateFile function. To ensure that other processes cannot write to the portion of the
file that is mapped, you should open the file with exclusive access. In
addition, the file handle should remain open until the process no longer needs the
file-mapping object. An easy way to obtain exclusive access is to specify zero in
the
fdwShareMode parameter of
CreateFile. The handle returned by
CreateFile is used by the
CreateFileMapping function to create a file-mapping object.
The
CreateFileMapping function returns a handle to the file-mapping object. This handle will be
used when
creating a file view so that you can access the shared memory. When you call
CreateFileMapping, you specify an object name, the number of bytes to be mapped from the file,
and the read/write permission for the mapped memory. The first process that
calls
CreateFileMapping creates the file-mapping object. Processes calling
CreateFileMapping for an existing object receive a handle to the existing object. You can tell
whether or not a successful call to
CreateFileMapping created or opened the file-mapping object by calling the
GetLastError function.
GetLastError returns NO_ERROR to the creating process and ERROR_ALREADY_EXISTS to
subsequent processes.
The
CreateFileMapping function fails if the access flags conflict with those specified when the
CreateFile function opened the file. For example, to read and write to the file:
- Specify the GENERIC_READ and GENERIC_WRITE values in the fdwAccess parameter of CreateFile.
- Specify the PAGE_READWRITE value in the fdwProtect parameter of CreateFileMapping.
File Mapping Size
The size of the file-mapping object is independent of the size of the file
being mapped. However, if the file-mapping object is larger than the file, the
system expands the file before
CreateFileMapping returns. If the file-mapping object is smaller than the file, the system maps
only the specified number of bytes from the file.
The
dwMaximumSizeHigh and
dwMaximumSizeLow parameters of
CreateFileMapping allow you to specify the number of bytes to be mapped from the file. Under
Windows 95,
dwMaximumSizeHigh is not used because it is not supported by the 32-bit file system. The value
should be zero.
When you do not want the size of the file to change (for example, when mapping
read-only files), call
CreateFileMapping and specify zero for both
dwMaximumSizeHigh and
dwMaximumSizeLow. Doing this creates a file-mapping object exactly the same size as the file.
Otherwise, you must calculate or estimate the size of the finished file because
file-mapping objects are static in size; once created, their size cannot
increase or decrease.
Windows NT: The size of a file-mapping object backed by a named file is limited by disk
space. The size of a file view is limited to the largest available contiguous
block of unreserved virtual memory. This is at most 2 GB minus the virtual memory
already reserved by the process.
Windows 95: The size of a file-mapping object backed by a named file is also limited by
disk space. The size of a file view is limited to the largest available
contiguous block of unreserved virtual memory in the shared address space. This is at
most 1 GB minus the virtual memory in use by other processes, such as 16-bit
Windows-based applications or Win32-based applications using file mapping.
- Software for developers
-
Delphi Components
.Net Components
Software for Android Developers
- More information resources
-
MegaDetailed.Net
Unix Manual Pages
Delphi Examples
- Databases for Amazon shops developers
-
Amazon Categories Database
Browse Nodes Database