Do you think IIT Guwahati certified course can help you in your career?
No
Introduction
Hello readers, FileMappingBase is like a tool that helps your computer remember where it put a certain file, kind of like how you use a map to find your way around a new place. When you tell your computer to open a file, it uses FileMappingBase to quickly find the file and open it for you. It's like a shortcut that helps your computer work faster.
In this article, we are going to read about CAtlFileMappingBase in the File Handling class. CAtlFileMappingBase is a class in the ATL (Active Template Library). This class is basically used to denote a memory-mapped file.
We will cover Syntax, Constructors, Methods, Parameters, and Operators related to CAtlFileMappingBase.
CAtlFileMappingBase
CAtlFileMappingBase is a class in the ATL (Active Template Library) of the Microsoft Foundation Class (MFC) Library. This class provides basic file-mapping functionality for memory-mapped files. It is used to map a file or a portion of a file into memory, allowing multiple processes to access the same file simultaneously.
The CAtlFileMappingBase class is a template class that is used to define a specific type of mapping object. The class provides several methods to map and unmap a file, as well as methods to get information about the mapped file, such as its size and protection.
Features
Here are some of the key features of CAtlFileMappingBase class:
It allows to creation, opening, and mapping of an existing file into memory.
It provides methods to map, unmap and resize the mapped file.
It is a template class, so it can be used to define a specific type of mapping object, such as CAtlFileMapping or CAtlFileMappingReadOnly.
CAtlFileMappingBase is typically used in conjunction with other classes in the ATL library, such as CAtlFileMapping and CAtlFileMappingReadOnly, to provide a complete file mapping solution for MFC(Microsoft Foundation Class Library).
Syntax
To use CAtlFileMappingBase class, we need header file,
atltypes.h
To define the class CAtlFileMappingBase, we need to write the class keyword before CAtlFileMappingBase as below.
class CAtlFileMappingBase
We can call the constructor by
CAtlFileMappingBase::CAtlFileMappingBase
Similarly, we can call the destructor by
CAtlFileMappingBase::~CAtlFileMappingBase
Example
#include <atlbase.h>
#include <iostream>
int main()
{
// Create a CAtlFileMappingBase object
CAtlFileMappingBase fileMapping;
LPCVOID pView = NULL;
DWORD dwSize = 0;
// Map the file "test.txt" into memory
if (fileMapping.MapFile(_T("test.txt"), FILE_MAP_READ, 0, 0, 0, &pView, dwSize))
{
//Access the contents of the file as if it were in memory
std::cout << (LPCSTR)pView << std::endl;
// Release the memory-mapped view
fileMapping.Unmap();
}
else
{
// Display an error message if the file cannot be mapped
std::cout << "Error mapping file: " << GetLastError() << std::endl;
}
return 0;
}
Explanation
This program uses the CAtlFileMappingBase class to map a file called "test.txt" into memory. The file is opened with the "MapFile" method of the CAtlFileMappingBase object, which takes the file name, the desired access mode (in this case, FILE_MAP_READ), and various other parameters. If the file is successfully mapped, the contents of the file are displayed on the screen using the std::cout function, and the memory-mapped view is then released using the "Unmap" method. If there is an error, the GetLastError function displays an error message.
Public Methods
There are some methods which are having public access specifiers, so we can use these methods anywhere inside the class. The functionality of every method is different. Read below for a better understanding.
Name
Uses
CAtlFileMappingBase::MapSharedMem
It maps a shared memory object to the process's virtual address space.
CAtlFileMappingBase::GetMappingSize
This function determines the size of the shared memory region currently mapped to the process's virtual address space.
CAtlFileMappingBase::OpenMapping
It is used to open an existing shared memory object and map it to the process's virtual address space.
CAtlFileMappingBase::GetHandle
It returns the handle to the shared memory object currently mapped to the process's virtual address space. This function does not take any parameter, and it returns the handle of the shared memory object that is currently mapped.
CAtlFileMappingBase::CopyFrom
It is used to copy data from a memory buffer to the shared memory object that is currently mapped to the process's virtual address space.
CAtlFileMappingBase::GetData
It returns a pointer to the start of the memory-mapped region. This function is used to get a pointer to the start of the memory-mapped region in the shared memory object that is currently mapped to the process's virtual address space.
CAtlFileMappingBase::Unmap
It is used to unmap a shared memory object from the process's virtual address space. This function is used to release the resources allocated to the shared memory object and make it no longer accessible to the process.
CAtlFileMappingBase::MapFile
It maps a file to the process's virtual address space, creating a shared memory object in the process.
Now we read one by one each in more detail.
MapSharedMem
This method is typically used when you want to share data between multiple processes or when you want to use memory-mapped files to improve the performance of your application.
When the MapSharedMem method is called, it creates a file-mapping object and maps the specified file to shared memory. The method returns a handle to the file-mapping object, which can be used to access the mapped memory in other parts of the program.
Once the memory is mapped, the data in the file can be accessed directly in the memory without the need to read the file from the disk.
dwDesiredAccess: The type of access to the file mapping object. This parameter can be one of the following values: FILE_MAP_ALL_ACCESS, FILE_MAP_READ, and FILE_MAP_WRITE.
Parameter
The MapSharedMem method takes two parameters:
A pointer to a variable that will receive the address of the mapped memory. This variable is typically a pointer to a buffer that will hold the data from the file.
An access type, which can be either read-only or read-write, specifies the level of Access that the current process will have to the mapped memory.
GetMappingSize
The GetMappingSize method is used to get the size of the memory-mapped file. It returns the size of the memory-mapped file in bytes.
When the GetMappingSize method is called, it retrieves the size of the memory-mapped file from the file-mapping object and returns it as a DWORD (unsigned long) value. This value can be used to determine the size of the buffer that is required to hold the data from the file or to determine the size of the memory-mapped file for other purposes.
Syntax
SIZE_T GetMappingSize() throw();
Parameter
The GetMappingSize method does not take any parameters and does not modify the state of the file-mapping object. It simply returns the size of the memory-mapped file.
OpenMapping
The purpose of this function is to open a file mapping object, which allows multiple processes to share a file in memory. By using a file mapping object, the process can access the file as if it were in memory, improving performance and reducing the amount of memory used.
The OpenMapping function takes several parameters, including the name of the file to be mapped, the type of Access desired (read-only or read/write), and the size of the mapping view.
GetHandle
This function returns the handle of the file mapping object created by the CAtlFileMappingBase::OpenMapping function. This handle can access the mapped file and perform other operations on the file-mapping object.
Syntax
HANDLE GetHandle() throw ();
Parameter
The function doesn't take any parameters. It just returns the handle as it's a simple getter function.
CopyFrom
The purpose of this function is to copy data from a source buffer into the mapped file, starting at a specified offset. This can be useful for updating the file's contents without opening and closing it.
LPVOID pData: Pointer to the source buffer containing the data to be copied into the mapped file.
SIZE_T nDataLen: Size of the data to be copied in bytes.
DWORD dwDestOffsetHigh: High-order 32 bits of the offset, in bytes, within the mapped file where the data is to be copied.
DWORD dwDestOffsetLow: Low-order 32 bits of the offset, in bytes, within the mapped file where the data is to be copied.
GetData
The purpose of this function is to retrieve a pointer to the data in the mapped file. This pointer can access the file's contents as if it were in memory.
Syntax
void* GetData() const throw();
Parameter
The function takes two parameters:
DWORD dwOffsetHigh: High-order 32 bits of the offset, in bytes, within the mapped file where the data is to be retrieved.
DWORD dwOffsetLow: Low-order 32 bits of the offset, in bytes, within the mapped file where the data is to be retrieved.
Unmap
This function is used to release a memory-mapped view of a file previously mapped using the Map function. It is typically used to release resources associated with the memory-mapped view, such as file handles and memory pages, so they can be reused or closed.
Syntax
HRESULT Unmap() throw();
Parameter
The Unmap function does not take any parameters. This function releases the memory-mapped view created by the Map function on the same object. It does not require any additional information to release the view.
MapFile
This function is used to create a memory-mapped view of a file. It maps a portion of a file into memory, allowing the contents of the file to be accessed as if it were in memory.
lpFileName: A string containing the name of the file to be mapped.
dwDesiredAccess: Specifies the type of Access requested for the file mapping object. It can be one of the following values: FILE_MAP_COPY, FILE_MAP_READ, FILE_MAP_WRITE, FILE_MAP_ALL_ACCESS.
dwFileOffsetHigh: The high-order DWORD of the file offset where the view begins.
dwFileOffsetLow: The low-order DWORD of the file offset where the view is to begin.
dwNumberOfBytesToMap: The file mapping number of bytes to map to the view.
lpMapView: Pointer to the memory address to which the file view is to be mapped.
If an error occurs, the function returns a pointer to the memory-mapped view of the file or NULL.
Frequently Asked Questions
What is the use of the lpMapView parameter in the MapFile function?
The lpMapView parameter in the MapFile function of CAtlFileMappingBase class is a pointer to a variable that receives a pointer to the base address where the file is mapped in memory. It allows the user to read and write to the mapped memory as if it were a memory buffer.
Can multiple processes access the same file simultaneously using CAtlFileMappingBase?
Yes, multiple processes can access the same file simultaneously using CAtlFileMappingBase, as long as they open the file with the appropriate access rights and sharing modes. However, it is important to properly synchronize access to the file to ensure that there are no conflicts or data corruption.
What is the purpose of the CAtlFileMappingBase class?
CAtlFileMappingBase is a part of the ATL in C++ that provides a base class for creating and managing memory-mapped files, allowing for faster access to the file's contents and greater flexibility in managing the file. It provides methods for creating, opening, reading and writing to the file.
Conclusion
In this article, we have discussed the File Handling Class-CAtlFileMappingBase. It is one of the several classes provided by Active Template Library.
If you want to explore more, here are some related articles -