Memory mapping files & CFILEFIND recursive search directory with cfilefind

xiaoxiao2021-04-10  353

Memory mapping file in VC

Sheriimin · 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, // File Handle LPSecurity? Attributes FilemappingAttributes, // Memory Mapping File Security Descriptor DWORD FLPROTECT, // File Map The maximum length of 32-bit DWMAXIMUMSIZELOW, / / ​​Maximum length Low 32-bit lpctstr ipname // Specify the name of this memory map file)

It is worth noting that if the parameter is oxffffffff, the file mapping object will be created in the operating system virtual memory page replacement file, not the disk file, and the size of this mapping object must be given.

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 a memory file object for mutually exclusive running files fails!", MB? OK M 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.

Recutory search directory with cfilefind

Xu Fu Yesky

We know that cfilefind does not provide features that directly travers its subdirectories, and sometimes we often traverse all files and their subdirectories in a directory. As we have to delete a directory, and there is a subdirectory in this directory because Windows does not allow deletion of non-empty directories, so we must be able to traverse all subdirectories in a directory, which can be implemented by simple recursive.

Let's start with a simple example: How to delete a directory? (Suppose we complete this feature through the DELETEDIRECTORY (LPCTSTR DIRNAME) function)

To delete a directory, we have to complete the following steps:

1. Delete all files in this directory

2. If there is a subdirectory in this directory We want to recursively call the Deledirectory (LPCTSTR DIRNAME "function to delete all files in the subdirectory

3. Call RemoveDirectory (LPCTSTR LPPATHNAME) Delete this directory

The complete implementation of the deletedirectory (LPCTSTR DirName) function is as follows:

Bool deletedIRectory (LPCTSTR DIRNAME)

{

CFILEFIND TEMPFIND; File: // Declare a cfilefind class variable for search

CHAR TEMPFILEFIND [200]; file: // Used to define search formats

Sprintf (TempFileFind, "% S //*.*", DIRNAME);

File: // The match format is *. *, all files in this directory

Bool isfinded = (bool) tempfind.FindFile (TempFileFind);

File: // Find the first file

While (isfinded)

{

ISFinded = (BOOL) TempFind.FindNextFile (); file: // Recute Search Other File IF (! TempFind.ISDOS ()) File: // If not "." Directory

{

Char FoundFileName [200];

STRCPY (FoundFileName, Tempfind.getFileName (). getBuffer (200));

IF (TempFind.Indirectory ()) file: // If it is a directory, recursively call

{File: // deletedIRectory

CHAR TEMPDIR [200];

Sprintf (Tempdir, "% S / /% S", DIRNAME, FOUNDFILENAME);

DELETEDIRECTORY (TEMPDIR);

}

Else

{File: // If it is a file, delete it directly

Char TempFileName [200];

Sprintf (TempFileName, "% S / /% S", DIRNAME, FOUNDFILENAME

Deletefile (TempFileName);

}

}

}

Tempfind.close ();

IF (! RemoveDirectory) File: // Delete Directory

{

AfxMessageBox ("Delete Directory Failed!", MB_OK);

Return False;

}

Return True;

}

Through the example above, the detailed reader has learned how to recurrent all the files and subdirectories in a directory. In fact, this can be used to make very useful tools. Let me give you a small example.

When writing programs with VC, VC generates a lot of intermediate files, which is very large, usually more than 10 times more than the code we have written. When we want to keep our source code, we have to manually delete these intermediate files, and some time you want to see the results of a project, so you compile this project, often If you do this, if you don't delete an intermediate file generated by VC, your hard drive will soon be filled with a lot of middle files. So I wrote a VC's intermediate file cleaning tool. The principle is very simple:

First, let the user specify a directory that needs to be cleaned, then traversed the directory through cfilefind, find the directory named debug and release (this is the default output directory of the VC. If you change the default setting, you must manually Delete it), then call the deletedIRectory function we have written above.

Warning: When using this tool, be sure to make sure your project is not called Debug or Release, and your useful directory name cannot be Debug or Release, otherwise, use this tool all to delete them.

In addition, this tool also statistics on the size of the source program (gives the number of lines of the entire project, the number of CPP files, the number of CPP files and total files), you can use it to easily count your own source program scale. The principle of statistical source procedure is also recursively looking for a directory, the principle and the deletedIRestory function above is consistent.

With this tool, you don't have to manually clean the middle files on the big pile on your machine. When you need to save your project catalog, you can completely clean the middle through the tool. Document, usually you don't have to take care of those annoying intermediate files.

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

New Post(0)