Using memory image implementation process global variables

zhaozj2021-02-16  46

Using memory image implementation process global variables

When programming, use global variables in the same program to bring us a lot of convenience, most programs are almost inseparable from the global variable. So about different programs? How do we share data between different programs (processes)? In fact, you may have noticed that the use of global variables between different processes is still very necessary in some occasions, this is a quite useful technology, based on a special case, we really need to share the different programs we have developed. data. For example, some software you will find that you don't have an executable after installation, and there is more than a process in the runtime task bar, but you find different processes in use, just like a single running program. Let's take a closer study:

To share global variables between different processes, we should first think that we can read data through different processes in the same storage area in memory, where we are like a public case, we call such a area called memory image file. We can easily create such a region in memory through the Windows API and read it easily. Here you need to use two critical API functions: CreateFileMapping and MapViewOffile, the former creates a memory image area in memory, it requires some parameters including read and write permissions, unique area name, and data size (these parameters will be In the comments of the following code, more parameter descriptions are given to the WIN SDK's help file or view the MSDN; the latter returns a pointer to this area to call in the program. Below we will explain through a simple example, this example consists of two engineering, a string that is written to the memory image area ('how are you "by a pressing button (' how are you ', through one EDIT read, if the number of characters in the Edit is greater than 11 programs will be wrong), the other is written through a button, written into an Edit. Below is part of the code (in order to save space, plus the program is very simple, some will not post the code you read is not posted):

We first declare a handle of the memory area to be created and a pointer variable to write data to this area in the first engineering (window name setValue).

VAR

......

Hfilemapping: hwnd; // Point to the handle of the memory area

SetString: Pchar; // Here you should use standard Windows strings instead of delphi unique string type

This way we can write the corresponding code in the press of the message:

Procedure TFORM1.BUTTON1CLICK (Sender: TOBJECT);

Begin

Hfilemapping: = CREATEFILEMAPPING ($ fffffff, nil, page_readwrite, 0 ,11, 'mapstring ");

// $ ffffffFff refer to the use of virtual files, pay attention to the same name in the two projects MapString

IF hfilemapping = 0 THEN

Raise Exception.create ('Error Creating Map File!');

SetString: = MapViewoffile (HFileMapping, File_Map_Write, 0,0,11);

Strcopy (setstring, pchar (edit1.text));

// Note that the copy function should be used instead of directly assigning setString: = pchar (edit1.text)

END;

Of course, we can also write a process of release memory images in this master engineering:

Procedure TFORM1.BUTTON2CLICK (Sender: TOBJECT); Begin

UnmapViewoffile (setString);

IF hfilemapping <> 0 THEN

CloseHandle (HFileMapping);

END;

Next is the second engineer clicked event, it is almost different from the first project, but we have modified the read permissions of the memory image to read only:

VAR

......

Getstring: pchar;

Hmapfilemaping: hwnd;

Procedure TFORM1.BUTTON1CLICK (Sender: TOBJECT);

Begin

HmapFilemaping: = CreateFilemapping ($ fffffff, nil, page_readonly, 0 ,11, 'mapstring ");

// Note again must be consistent with the unique name (MapString) and the first project

IF hmapfilemaping = 0 THEN

Raise Exception.create ('Error');

GetString: = MapViewoffile (hmapfilemaping, file_map_read, 0,0,11);

Edit1.text: = String (getString);

END;

Now we can test, you can see the test situation by chalmise:

Of course, the above procedure is just an example, there is no meaning, but in order to introduce this technology, it will play a role of tile jade, and then you can more strictly control the shared variables. You can also use this technique to implement some strange effects. About some API functions, most of them are very complicated, and they have not fully met. It is recommended that you have more than the WIN SDK help in Delphi in programming, which is quite detailed, you can help you learn more.

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

New Post(0)