{ Copyright © 1999 by Delphi 5 Developer's Guide - Xavier Pacheco and Steve Teixeira }
library ShareLib;
uses ShareMem, Windows, SysUtils, Classes; const
cMMFileName: PChar = 'SharedMapData';
{$I DLLDATA.INC}
var GlobalData: PGlobalDLLData; MapHandle: THandle;
{ GetDLLData will be the exported DLL function }
procedure GetDLLData(var AGlobalData: PGlobalDLLData); stdcall; begin { Point AGlobalData to the same memory address referred to by GlobalData. } AGlobalData := GlobalData; end;
procedure OpenSharedData; var Size: Integer; begin { Get the size of the data to be mapped. } Size := SizeOf(TGlobalDLLData);
{ Now get a memory-mapped file object. Note the first parameter passes the value $FFFFFFFF or DWord(-1) so that space is allocated from the system's paging file. This requires that a name for the memory-mapped object get passed as the last parameter. }
MapHandle := CreateFileMapping(DWord(-1), nil, PAGE_READWRITE, 0, Size, cMMFileName);
if MapHandle = 0 then RaiseLastWin32Error; { Now map the data to the calling process's address space and get a pointer to the beginning of this address } GlobalData := MapViewOfFile(MapHandle, FILE_MAP_ALL_ACCESS, 0, 0, Size); { Initialize this data } GlobalData^.S := 'ShareLib'; GlobalData^.I := 1; if GlobalData = nil then begin CloseHandle(MapHandle); RaiseLastWin32Error; end; end;
procedure CloseSharedData; { This procedure un-maps the memory-mapped file and releases the memory-mapped file handle } begin UnmapViewOfFile(GlobalData); CloseHandle(MapHandle); end;
procedure DLLEntryPoint(dwReason: DWord); begin case dwReason of DLL_PROCESS_ATTACH: OpenSharedData; DLL_PROCESS_DETACH: CloseSharedData; end; end;
exports GetDLLData;
begin { First, assign the procedure to the DLLProc variable } DllProc := @DLLEntryPoint; { Now invoke the procedure to reflect that the DLL is attaching to the process } DLLEntryPoint(DLL_PROCESS_ATTACH); end.
|