| |||||
|
|
Hi all, I will introduce you to a relatively small but important topic today. File Mapping and Mapped file view IO is a way to map a file into our address space and access it's contents. There are API's provided for this purpose which load the file and map it into our processes's address space so that we can access it as if it's a simple block of memory in our process.Basically you only need three functions to use file mapping. These are CreateFileMapping(), MapViewOfFile() and UnmapViewOfFile(). To use file mapping, you first need to create a file mapping object. This is done by calling CreateFileMapping(). This creates a named file mapping object which can be used to map a given file. Notice that it's a NAMED object, so you can use this name to open and map the files from other process/places also. Look in the MSDN to know more about these API's. Next step is to actually map the file into our address space. This is accomplished by calling MapViewOfFile().As the name suggests, this function loads and maps a part (view) of given file into our address space. The return of this function is the base address where the file has been mapped. Now we can use this base address to read the contents of the file or do anything we want. You can unmap the view by calling UnmapViewOfFile().File mapped IO is very fast when you have large files to read/parse because the whole file is loaded into your own address space. Note that the LoadLibrary() function, internally uses File Mapping to load the dll's and map to our address space. If any of you are required/interested in writing a dll loader (set of API's to load/unload the dll's) then you should know that you will need to use these API's. Read about these in the MSDN and experiment with them.Exercises: 1. Read about mapped file IO and it's API's in MSDN. 2. Write a text editor application using mapped file IO which can read the file and save the changes. 3. Find out how the changes can be saved back to actual files from mapped views of them. This should be good enough for this week, we will cover some other topic next week. Thanks, -Farooque
|