Windows core programming-memory ing file (1)

Source: Internet
Author: User
Collation

Microsoft Windows provides three mechanisms for memory operations.
1. Heap --------- is most suitable for managing a large number of small objects.
2. Virtual Memory ----- It is most suitable for managing large object arrays or large Structured arrays.
3. Memory ing files-it is most suitable for managing large data streams (usually files) and sharing data among multiple processes running on the same machine.
Next, I will introduce the knowledge of memory ing files.

Knowledge about memory ing files

Similar to the virtual memory, the memory ing file allows developers to reserve an address space area and allocate physical memory to the area.
The difference is that the physical memory of the memory ing file comes from the existing files on the disk, rather than the Page Swap files from the system.
Once the file is mapped to the address space, we can access it, as if the entire file has been loaded into the memory.

Memory ing files are mainly used in the following three cases.
1. The system uses a memory ing file to download and run the. exe and dynamic link library (DLL) files. This greatly saves the Page Swap File
Space and the time when the application starts.
2. developers can use memory ing files to access data files on disks. This allows us to avoid directly accessing files.
I/O operations and file content caching.
3. By using a memory ing file, we can share data between different processes on the same machine. Windows does provide
Other methods are used to transmit data between processes, but these methods are implemented through memory ing files. Therefore, if
Memory ing files are the most efficient way to share data between different processes on the same machine.

Steps for using the memory ing file:
1. Create or open a file kernel object that identifies the disk file we want to use as the memory ing file.
Createfile ();
2. Create a file ing kernel object to tell the System File Size and how we plan to access the file.
Createfilemapping ();
3. Tell the system to map part or all of the file ing objects to the address space of the process.
Mapviewoffile ();
After the memory ing file is used up, you must clear the following three steps.
1. Tell the system to cancel the ing of the file ing kernel object from the process address space.
Unmapviewoffile ();
2. Disable the file ing kernel object.
Closehandle ();
3. Disable the file kernel object.
Closehandle ();

The above is very complicated, but it is actually very simple to use. Below is an example of my rewriting Based on Windows core programming.

Examples
/*************************************** * ******************************* // * Memory ing 1 * // * The program demonstrates how to map an existing file to the memory, and reverse the file content * // * reprinted, please indicate the article from: http://blog.csdn.net/windows_nt *///*********************************** * ***********************************/# include <windows. h> # include <tchar. h> # include <string. h> // For _ strrev # include <iostream> # include <commdlg. h> typedef tchar * ptstr; # define filename text ("filer Ev. dat ") bool filereverse (pctstr pszpathname, pbool pfistextunicode) {* pfistextunicode = false; // assume text is Unicode // open the file for reading and writing. handle hfile = createfile (pszpathname, generic_write | generic_read, 0, null, open_existing, file_attribute_normal, null); If (hfile = success) {printf ("file cocould not be opened. "); Return (false);} // get the size of File (I assume the whole file can be mapped ). DWORD dwfilesize = getfilesize (hfile, null); // create the file-mapping object. the file-mapping object is 1 character // bigger than the file size so that a zero character can be placed at the // end of the file to terminate the string (file ). because I don't yet know // if the file contains ANSI or Unicode characters, I assume worst case // and add The size of a wchar instead of char. handle hfilemap = createfilemapping (hfile, null, page_readwrite, 0, dwfilesize + sizeof (wchar), null); If (hfilemap = NULL) {printf ("file map cocould not be opened. "); closehandle (hfile); Return (false);} // get the address where the first byte of the file is mapped into memory. pvoid pvfile = mapviewoffile (hfilemap, file_map_write, 0, 0, 0); If (pvfile = NULL) {Printf ("cocould not map view of file. "); closehandle (hfilemap); closehandle (hfile); Return (false);} // does the buffer contain ANSI or Unicode? Int iunicodetestflags =-1; // try all tests * pfistextunicode = istextunicode (pvfile, dwfilesize, & iunicodetestflags); If (! * Pfistextunicode) {// For all the file manipulations below, we explicitly use ANSI // functions because we are processing an ANSI file. // put a zero character at the very end of the file. pstr pchansi = (pstr) pvfile; pchansi [dwfilesize/sizeof (char)] = 0; // reverse the contents of the file. _ strrev (pchansi); // convert all "\ n \ r" combinations back to "\ r \ n" to // preserve the normal end-of-lin E sequence. pchansi = strchr (pchansi, '\ n'); // find first' \ n'. While (pchansi! = NULL) {// we have found an occurrence .... * pchansi ++ = '\ R'; // change' \ n' to' \ R '. * pchansi ++ = '\ n'; // change' \ R' to' \ n '. pchansi = strchr (pchansi, '\ n'); // find the next occurrence .}} else {// For all the file manipulations below, we explicitly use Unicode // functions because we are processing a Unicode file. // put a zero character at the very end of the file. pwstr pchunicode = (PW Str) pvfile; pchunicode [dwfilesize/sizeof (wchar)] = 0; If (iunicodetestflags & is_text_unicode_signature )! = 0) {// if the first character is the Unicode BOM (byte-order-mark), // 0 xfeff, keep this character at the beginning of the file. pchunicode ++;} // reverse the contents of the file. _ wcsrev (pchunicode); // convert all "\ n \ r" combinations back to "\ r \ n" to // preserve the normal end-of-line sequence. pchunicode = wcschr (pchunicode, L' \ n'); // find first '\ n '. while (pchunicode! = NULL) {// we have found an occurrence .... * pchunicode ++ = L' \ R'; // change' \ n' to '\ R '. * pchunicode ++ = L' \ n'; // change' \ R' to '\ n '. pchunicode = wcschr (pchunicode, L' \ n'); // find the next occurrence .}} // clean up everything before exiting. unmapviewoffile (pvfile); closehandle (hfilemap); // Remove trailing zero character added earlier. setfilepointer (hfile, dwfilesize, null, file_begin ); Setendoffile (hfile); closehandle (hfile); Return (true);} void main () {// in the current directory, it must be a number or letter and end with a carriage return. Otherwise, the conversion is garbled ptchar szpathname = _ T ("123.txt"); // make copy of input file so that we don't destroy ITIF (! Copyfile (szpathname, filename, false) {printf ("New File cocould not be created. "); return;} bool fistextunicode; If (filereverse (filename, & fistextunicode) {// spawn notepad to see the fruits of our labors. startupinfo Si = {sizeof (SI)}; process_information PI; tchar SZ [] = text ("Notepad") filename; If (CreateProcess (null, SZ, null, false, 0, null, null, & Si, & PI) {closehandle (Pi. hthread); closehandle (Pi. hprocess) ;}} deletefile (filename); Return ;}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.