delphi3000.com - the free delphi knowledge platform
delphi3000.com - the free delphi knowledge platform
500 Users Online NOW
Have a look at your member-status

connecting people's knowledge


  - Recent ArticlesRSS feed for Recent Articles on delphi3000.com
  - List of All Articles
  - Top Viewed Articles
  - Articles (+Attachem.)
  - Articles Of Interest
  - Categories
  - Top Uploader
  - Search
  - Index

  - My Home
  - Submit an Article
  - My Articles
  - My Personal Data
  - My Bookmarks
  - Activities
  - Login/Logout

  - Sign Up
  - Why Sign Up
  - Newsletter

  - Press
  - Advertise

  - Contact
  - Feedback





Community
Borland
ClubeDelphi
Dr. Bob
UK-BUG
Delphi Meetings
Planeta Delphi



Loremo - the 1.5 liter car coming in 2009




Startblatt.de






Share this article with friendsShare this article with friends
Rate this articleRate this article - to keep the quality of delphi3000.com !
Comment this article or read through previous comments (2)


Memory shared in a Dynamic-Link LibraryComponent available for this articleFormat this article printer-friendly!Bookmark function is only available for registered users!
Product:
Delphi 3.x (or higher)
Category:
Win API
Skill Level:
Scoring:
Last Update:
09/27/2002
Search Keys:
delphi delphi3000 article borland vcl code-snippet Shared-Memory DLL Dynamic-Link-Library CreateFileMapping MapViewOfFile UnmapViewOfFile
Times Scored:
5
Visits:
3621
Uploader: Bertrand Goetzmann
Company:
Reference: www.object-everywhere.com
Component Download: http://objecteverywhere.chez.tiscali.fr/SMem.zip
 
Question/Problem/Abstract:
How several processes can share memory to cooperate ?
Answer:



The unit bellow shows how use a file-mapping object to set up memory that can be shared by processes that load the DLL.
The shared DLL memory persists only as long as the DLL is loaded.
This unit is the translation of the DLLSHMEM.C example from the Win32 Developer's Reference help file (see DLLSHMEM)

The SetSharedMem and GetSharedMem functions must be exported in a dll Delphi project.
If we can't create the file-mapped shared memory ExitCode variable is set to 1 or 2.
Tested with Delphi 6, under Windows 2000.

unit SharedMem;

// Using Shared Memory in a Dynamic-Link Library
//
// This unit shows how use a file-mapping object to set up memory that can be shared by processes that load the DLL.
// The shared DLL memory persists only as long as the DLL is loaded.
//
// This unit is the translation of the DLLSHMEM.C example from the W32 Developer's Reference help file (see DLLSHMEM)
//
// The SetSharedMem and GetSharedMem functions must be exported in a dll Delphi project.
// If we can't create the file-mapped shared memory ExitCode variable is set to 1 or 2.
// Tested with Delphi 6, under Windows 2000.
//
// Written by Bertrand Goetzmann (http://www.object-everywhere.com)
// Keywords : Shared-Memory DLL CreateFileMapping MapViewOfFile UnmapViewOfFile

interface

procedure SetSharedMem(lpszBuf: PChar);
procedure GetSharedMem(lpszBuf: PChar; cchSize: Integer);


implementation

uses Windows, SysUtils;

// Using Shared Memory in a Dynamic-Link Library
// The DLL sets up shared memory using a named file-mapping object.

const
  SHMEMSIZE = 4096;

var
  hMapObject: THandle;
  lpvMem: Pointer; // pointer to shared memory


procedure Init;
var
  fInit: Boolean;
begin
  hMapObject := 0; // handle to file mapping


  // The DLL is loading due to process
  // initialization or a call to LoadLibrary.


  // Create a named file mapping object.

  hMapObject := CreateFileMapping(
    $FFFFFFFF, // use paging file
    nil,                // no security attributes
    PAGE_READWRITE,      // read/write access
    0,                   // size: high 32-bits
    SHMEMSIZE,           // size: low 32-bits
    'dllmemfilemap');    // name of map object

  if hMapObject = 0 then
  begin
    ExitCode := 1;
    Exit;
  end;

  // The first process to attach initializes memory.

  fInit := GetLastError <> ERROR_ALREADY_EXISTS;

  // Get a pointer to the file-mapped shared memory.

  lpvMem := MapViewOfFile(
    hMapObject,     // 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
  begin
    ExitCode := 2;
    Exit;
  end;

  // Initialize memory if this is the first process.

  if fInit then
    FillChar(lpvMem^, SHMEMSIZE, 0);
end;

procedure Terminate;
begin
  // Unmap shared memory from the process's address space.

  if lpvMem <> nil then
    UnmapViewOfFile(lpvMem);

  // Close the process's handle to the file-mapping object.

  if hMapObject <> 0 then
    CloseHandle(hMapObject);
end;


// SetSharedMem sets the contents of shared memory.

procedure SetSharedMem(lpszBuf: PChar);
begin
  Move(lpszBuf^, lpvMem^, StrLen(lpszBuf));
end;


// GetSharedMem gets the contents of shared memory.

procedure GetSharedMem(lpszBuf: PChar; cchSize: Integer);
begin
  Move(lpvMem^, lpszBuf^, cchSize);
end;

initialization
  Init;

finalization
  Terminate;

end.





Please rate this article!
Skill level:
BeginnerExpert

Useful:
No!Very!

Overall rating:
PoorExcellent



Comments to this article
Write a new comment
Download site
    Fred Hill (Sep 26 2002 7:53PM)

The download site gives you a forbidden msg
Respond

RE: Download site
Bertrand Goetzmann (Sep 27 2002 10:00AM)

Sorry, you can now use the following link :
http://objecteverywhere.chez.tiscali.fr/SMem.zip
Respond














 
Sign up to consume product discounts for Bronze memberships !

read more


  Visit our Sponsor

 

  Community Ad of
L. Rosenstein
 
   














 







     
  Copyright © 2000 - 2007 delphi3000.com - All rights reserved. Terms of use. || Privacy
delphi3000.com is a service by bluestep.com IT-Services GmbH (Vienna)