One of the recent learning of mine is to use Memory Mapped File in Delphi. These are the great ways to share data among various libraries within the same application instance.
Step 1: Define a
Record Structure and a
Pointer to the record.
type PRecordInfo = ^TRecordInfo; TRecordInfo = packed record Field1 : ShortString; Field2 : Integer; Field3 : Integer; end; //TInstanceInfoStep 2 : Variable Declaration. I prefer to do it in implementation section for many good reasons known to Delphi Programmers.
var
FMappingHandle : THandle = 0;
FRecordInfo : PRecordInfo= nil;
FMappingName : String = '';
Step 3: Manipulate Memory mapped file. Use the code snippets below to perform the operation as per your business logic.
{ To Create a Mapping File }
FMappingHandle := CreateFileMapping($FFFFFFFF, nil, PAGE_READWRITE, 0, SizeOf(TRecordInfo), @FMappingName[1]);
FRecordInfo := MapViewOfFile(FMappingHandle, FILE_MAP_ALL_ACCESS, 0, 0, SizeOf(TRecordInfo));
FRecordInfo.Field1 := 'Value 1';
FRecordInfo.Field2 := 0;
FRecordInfo.Field3 := 1;
{ To Open the Memory Map File }FMappingName := 'SomeNametoUniquelyIdentifyTheMappingFile';
FMappingHandle := OpenFileMapping(FILE_MAP_ALL_ACCESS, false, @FMappingName[1]);
{ To Read the File Contents}FRecordInfo := MapViewOfFile(FMappingHandle, FILE_MAP_ALL_ACCESS, 0, 0, SizeOf(TRecordInfo));
LVariable1 := FConfigInfo^.Field1;//Read the Contents
LVariable2 := FConfigInfo^.Field2;//Read the Contents
LVariable3 := FConfigInfo^.Field3;//Read the Contents
Enjoy working with Memory mapped files. I found them best way to share data among different modules of the application. They are quite useful if you have one EXE and multiple DLLs in an application.