Everyone knows that an application in Window 9x cannot directly access another program's memory address, because Win9X provides its own virtual space for each run, however, it also makes two There is no way to exchange data between a program, but this is not to say that the program will not be data transmission. Win9X provides us with a few special methods to implement this purpose, such as the WINDOWS message WM_COPYDATA and storage impact files. .
Use the message WM_COPYDATA to transfer the memory address of the data buffer that needs to be passed, before, we cannot send the address of the memory block from an application to another, because the address in a virtual storage space is meaningless in another address space . However, the WM_COPYDATA message returns some special operations in the background, so we can get the correct address in the recipient. This message needs to be passed to pass the handle of the window being sent, and a pointer to the TcopyDataStruct structure. This structure is as follows:
TagcopyDataStruct = Packed Record
DWDATA: DWORD;
// This is an additional 32-bit parameter;
CBDATA: DWORD;
/ / We will pass the size of the data buffer;
LPDATA: POINTER;
/ / Pointer to the data buffer.
END;
TcopyDataStruct = Tag CopyDataStruct;
An example, the SendData program contains a text box that sends the content in the text box to the GetData program in its onchange event.
In the SendcopyData program, the processing function of the onchange event of the text box is as follows:
Procedure TFORM1.InputEditchange (Sender: TOBJECT);
VAR
CDS: TcopyDataStruct;
SS: PCHAR;
TARGETHANDLE: THANDLE;
Begin
Cds.cbdata: = Length (InputEt.Text) 1;
GetMem (s, cds.cbdata); // Apply for a CBDATA size buffer and assign the needle to SS;
Strcopy (SS, PCHAR (INPUTEDIT.TEXT);
CDS.LPDATA: = SS; / / Set buffer pointer
TargetHandle: = FindWindow ('tform1', 'getcopydata'); // Get the "getCopyData" window handle with the API function
if targethandle = 0 THEN
Begin
ShowMessage ('Don''t Find Target Window ");
EXIT;
END;
SendMessage (TargetHandle, WM_CopyData, Handle, Integer)); // Send a target window with the CDS structure pointer;
// TargetHandle makes the target window handle;
// Handle Send the window handle of the message;
FreeMem (SS);
END;
In the getCopyData program, accept WM_COPYDATA messages through a custom message handler:
public
Procedure getCopyData (Var Msg: twmcopydata); message wm_copydata;
Procedure TFORM1.GETCOPYDATA (VAR MSG: TwMcopyData);
Begin
With msg.copydatastruct ^ do
Begin
Form1.edit1.text: = pchar (lpdata);
END;
END;
The TWMCopyData structure is defined as follows: TWMCopyData = Record
Msg: cardinal;
From: hwnd; // Send the handle of the window of the message
CopyDataStruct: pcopyDataStruct; // That is, the pointer of the CDS structure in the SendCopyData program
Result: longint;
END;
In addition to passing data with WM_CopyData, Windows also provides file_mapping memory sharing technology to implement program data sharing. Use two programs similar to the above example window
In the mapWrite program:
Private
Hmapfile: Thanale;
MapFilePointer: Pointer;
Procedure TFORM1.FormCreate (Sender: TOBJECT);
Begin
// use the API function to create an image file
Hmapfile: = CreateFilemapping
$ Fffffffffff, // Specify shared memory
NIL,
Page_readwrite, // Sharing method
0,
1000, // Share memory size
'Mymappedfile'); / / The name of the image file
If hmapfile <> 0 Then // If the image file is established
/ / The MapViewOffile function returns a pointer to the program memory space to the program memory space to the shared memory block.
MapFilePointer: = MapView Offile (Hmapfile, File-Map-all-access, 0,0,0)
Else
ShowMessage ('can''t create mapfile ";
IF mapfilepointer = nil dam
ShowMessage ('MapFilePointer = nil');
END;
Procedure TFORM1.EDIT1CIGE (Sender: TOBJECT);
Begin
Strcopy (PchapFilePointer), PCHAR (Edit1.Text)); // In the text box, the text box is in the shared memory
END;
Use the API function OpenFilemapping in your mapRead program to open the mapped image files in the MapWrite program.
Procedure TFORM1.FormCreate (Sender: TOBJECT);
Begin
Hmapfile: = OpenFilemapping (file_map_read, true, 'mymappedfile'); // Get the handle of the 'mymappedfile' image file
IF HMAPFILE <> 0 THEN
MapFilePointer: = MapViewoffile (hmapfile, file_map_all_access, 0,0,0)
Else
Begin
ShowMessage ('can not open mapp file');
Timer1.enabled: = FALSE;
END;
END;
/ / The data is read from the shared memory and displayed
Procedure TFORM1.TIMER1TIMER (Sender: TOBJECT);
VAR
SS: STRING;
Begin
SS: = pchar (mapfilepointer);
Edit1.text: = SS;
END;
Of course, we will release the shared memory handle.
UNMAPVIEWOFFILE (MapFilePointer);
CloseHandle (hmapfile);
(Xi'an HSZJ)