DelphiFAQ Home Search:

Shared memory (Delphi 32bit)

 

commentsThis article has not been rated yet. After reading, feel free to leave comments and rate it.

This shows how to use shared memory in a DLL with Delphi 32bit. Use the functions OpenMap and CloseMap.

Any two or more applications or DLLs may obtain pointers to the same physical block of memory this way.
PMapData will point to a 1000 byte buffer in this example, this buffer being initialized to #0's the first time in.
One potential problem is synchronizing access to the memory. You may accomplish this through the use of mutexes:

  • Call the function LockMap before writing (and reading?) to the memory mapped file.
  • Be sure to call UnlockMap immediately when done updating.
From: John Crane:

 var
   HMapping: THandle;
   PMapData: Pointer;
 
 const
   MAPFILESIZE = 1000;
 
 procedure OpenMap;
 var
   llInit: Boolean;
   lInt: Integer;
 begin
   HMapping := CreateFileMapping($FFFFFFFF, nil, PAGE_READWRITE,
                 0, MAPFILESIZE, pchar('MY MAP NAME GOES HERE'));
   // Check if already exists
   llInit := (GetLastError() <> ERROR_ALREADY_EXISTS);
   if (hMapping = 0) then
   begin
     ShowMessage('Can''t Create Memory Map');
     Application.Terminate;
     exit;
   end;
   PMapData := MapViewOfFile(HMapping, FILE_MAP_ALL_ACCESS, 0, 0, 0);
   if PMapData = nil then
   begin
     CloseHandle(HMapping);
     ShowMessage('Can''t View Memory Map');
     Application.Terminate;
     exit;
   end;
   if (llInit) then
   begin
     // Init block to #0 if newly created
     memset(PMapData, #0, MAPFILESIZE);
   end
 end;
 
 procedure CloseMap;
 begin
   if PMapData <> nil then
     UnMapViewOfFile(PMapData);
   if HMapping <> 0 then
     CloseHandle(HMapping);
 end;
 
 var
   HMapMutex: THandle;
 
 const
   REQUEST_TIMEOUT = 1000;
 
 function LockMap:Boolean;
 begin
   Result := true;
   HMapMutex := CreateMutex(nil, false,
                  pchar('MY MUTEX NAME GOES HERE'));
   if HMixMutex = 0 then
   begin
     ShowMessage('Can''t create map mutex');
     Result := false;
   end
   else
   begin
     if WaitForSingleObject(HMapMutex,REQUEST_TIMEOUT)
 	     = WAIT_FAILED then
 	begin
       // timeout
       ShowMessage('Can''t lock memory mapped file');
       Result := false;
     end
   end
 end;
 
 procedure UnlockMap;
 begin
   ReleaseMutex(HMixMutex);
   CloseHandle(HMixMutex);
 end;

Comments:

2007-02-04, 07:08:10
anonymous from India  
good article but leaves a lot of stuff to imagination. a working example would have been perfect
2007-06-21, 18:28:49
anonymous from Latvia  
Line: 'memset(PMapData, #0, MAPFILESIZE);' is wrong. It's not C.
In Delphi one should use FillChar(PMapData^, MAPFILESIZE, 0);
2009-07-08, 05:43:15
anonymous from United Kingdom  
Should also replace all instances of 'HMixMutex' with 'HMapMutex'.

He obviously didn't copy and paste this from a working app!! lol

Great example though - nice and simple and very powerful.

 

 

NEW: Optional: Register   Login
Email address (not necessary):

Rate as
Hide my email when showing my comment.
Please notify me once a day about new comments on this topic.
Please provide a valid email address if you select this option, or post under a registered account.
 

Show city and country
Show country only
Hide my location
You can mark text as 'quoted' by putting [quote] .. [/quote] around it.
Please type in the code:

Please do not post inappropriate pictures. Inappropriate pictures include pictures of minors and nudity.
The owner of this web site reserves the right to delete such material.

photo Add a picture: