Memory mapping file in VC ++

zhaozj2021-02-11  203

Memory mapping file in VC

2000-01-30 · Su Liimin · Computer News

During the development of the software, sometimes you need to control some programs to make them can't run at the same time, that is, multiple programs mutually exclusive operation (also include prohibiting the same program to run multiple instances). For this issue, we use the memory map file in Visual C 6.0 to implement mutual exclusive operation between multiple programs. Memory map files can create a memory object that does not contact the disk file, map the information of the file to the address space of a process, and we can access the data in the file, just like it is in memory. At the same time, you can give a name to the memory map file object in the programming. This name is unique throughout the system. This name can be shared between multiple processes, exchange information exchange through the name sharing energy implementation, and more Mutual exclusion between the program. Let us first introduce several important letters related to memory mapping files before telling specific programming methods.

CreateFilemapping function Creates a file mapping object for the specified file, the original shape of this function is as follows: Handle CreateFileMapping (Handle Hfile, // Document Handle LPSecurity? Attributes FilemappingAttributes, // Safety Descriptor DWord Flprotect, Safety Descriptor DWord Flprotect, // File Map The maximum length of the maximum length of 32-bit DWMaxImumsizerow, // The maximum length of the low 32-bit lpctstr ipname // Specifies the name of this memory mapped file) It is worth noting that the parameter is OxffffFFF, which will be in the operating system virtual Memory page replacement files create file mapping objects instead of using disk files while you must give the size of this mapping object. The Nao Vuewiffile function maps the view of the file to a process address space, returns the memory pointer of the LPVOID type. With it, you can directly access the information in the file view.

LPVOID MAP VIEWLFFILE (HANDLE HFILEMAPPINGOBUCT, // mapping file object handle DWORD DWDESIREDACCESS, // access mode DWORD DWFILEOFFSETHIGH, // file offset addresses the upper 32 bits DWORD DWFILEOFFSETHIGH, // file offset address of the lower 32-bit DWORD DWNUMBEROFBYTESTOMAP // Map view size)

In Visual C 6.0, we generate dialog-based applications in Visual C 6.0. In the initialization phase of the program, add the following code at the beginning of the cwinapp bornin's initial function:

(// Create a memory mapping file object, MU_TEXRUNNING is its name, all programs that need to be mutually exclusive // ​​use this name (these codes are common for programs that need to be mutually exclusive) Handle HMAP = CreateFileMapping ((Handle) 0xffffffffnull , Page_Readwrlte, 0,128, "MU_TEXRUNNING") If (hmap == null) // If the creation fails ("AFXMessageBox (" Create the memory file object for mutually exclusive running files! ", MB? OK MB ?? IclnStop;

Return false; // Exit this procedure)

// If this is already existing, it shows that other programs that have been mutually exclusive are running.

ELSE IF (getLastError () == Er_ROR_ALREADY_EXISTS)

(Lpvoid ipmen = mappviewoffile (hmap, file_map_write, 0,0,0); cstring str = (char *) ipmem; // Get description information that has been running

UnmapViewoffile (LPMEM); // Release mapping map

CloseHandle (HMAP); // Close this object

AFXMessageBox (STR, MB_OK MB_ICONSTOP); Display related description information

Return false; // Exit this procedure)

Else // After the above inspection, this is the first mutual exclusive program

(LPVOID IP_MEM = MapViewOffile (HMAP, File_Map_Write, 0, 0, 0);

// Here you can write the description information of the program run, the above error prompt is this letter information

STRCPY ((char *) LPMEM, "XXX program is running!");

UnmapViewoffile (LPMEM); // Release mapping map)

// The following can continue the original code of the function initin? Stance.

AFXENABLECONTROL_COMTAINER ();

// When the program is running, you have to remember that CHANDIE (HMAP) is turned off, // You can call before the initInstance function last ReturnFalse.

CloseHandle (HMAP); // Close the internal deployment file object handle return false;) The above program is debugged in Visual C 6. Other non-dialog type programs can add similar code in their respective initialization and termination phases, just if the handle of memory map files may be used in different functions, then define them as a member variable of CWINAPP's class or It is a global variable.

转载请注明原文地址:https://www.9cbs.com/read-4511.html

New Post(0)