Title: using file mapping objects ( Shared Memory )
Question: How to interprocess between two applicationsin or more Delphi written applications?
Answer:
There are some situations in programming with the new software design programming languages that need communication between two or more processes (or instances of some executables).this technique is named Inter Process Communication (IPC) which is discussed in the operating system books.
There are some ways to do this. One way is the communication through sockets. This way is the main approach for programmers in Linux and one of the ways in Windows. Another way in Windows is using the messages (using PostMessage and SendMessage API functions). But the preferred way in Windows is Shared Memory which is mainly discussed in this article. I've tried all the three ways and the conclusion was the best performance for the Shared Memory model.
The idea is creating a named file-mapping object in both applications, of course with the same name. then with the SetSharedMem and GetSharedMem procedures the applications can write and read the datafrom file mapping object ( Shared Memory).
Here I describe this process:
procedure InitSharedMemory;
var
h:hwnd;
begin
h := CreateFileMapping($FFFFFFFF, // use paging file
0, // no security attributes
PAGE_READWRITE, // read/write access
0, // size: high 32-bits
100, // size: low 32-bits
'dllmemfilemap'); // name of map object
if h=0 then
exit;
lpvMem := MapViewOfFile( h, // object to map view of
FILE_MAP_WRITE, // read/write access
0, // high offset: map from
0, // low offset: beginning
0); // default: map entire file
if lpvMem=nil then
exit;
end;
This procedure must be called before any accessto the shared memory. The CreateFileMapping function creates the named file-mapping object and returns the handle of the object. The first parameter is the handle of a file to be used for the file mapping. It can be $FFFFFFFF to specify the operating system page file. The second parameter ¨C security attribute ¨C should be 0. The third parameter should be PAGE_READWRITE. the two next parameters are the maximum size of the buffer. The first be 0 and the second be 100 for the size of 100 byte ( 0 * MAXINT + 100 ).
procedure SetSharedMem(lpszBuf:LPTSTR);
var
lpszTmp : LPTSTR;
i:integer;
begin
lpszTmp := LPTSTR( lpvMem);
i:=0;
while (lpszBuf[i]chr(0)) do
begin
lpszTmp[i] := lpszBuf[i];
inc(i);
end;
lpszTmp[i] := chr(0);
end;
This procedure is responsible for sending data to the shared memory. it takes a pointer to the data and copies it in the shared area. This area can be accessed by the other applications.
procedure GetSharedMem( lpszBuf:LPTSTR; cchSize:DWORD);
var
lpszTmp:LPTSTR;
i:integer;
begin
lpszTmp := LPTSTR( lpvMem);
i:=0;
dec(cchSize);
while (lpszTmp[i]chr(0)) and(cchSize0) do
begin
lpszBuf[i] := lpszTmp[i];
inc(i);
end;
lpszBuf[i] := chr(0);
end;
This procedure copies the data from shared memory to the variable in the first parameter with the maximum size of the second parameter.
A simple example project is attached that illustrates using shared memory in Borland Delphi. It should be executed twice and they can share data with each other.
download Example:
download 1
download 2
download 3