Memory Mapped Files in Delphi
TMapStream provides high level access to Windows memory mapped files. Memory mapped files provide a fast and convenient way of sharing large amounts of data between applications running concurrently. It has one property
- Position: Integer - The current position of the cursor in the memory mapped file.
The principal methods of TMapStream are listed below
-
constructor CreateEx(const AName:String; ASize,ATimeOut:Integer)
- This is the object constructor. AName is used to create a memory map - or open an existing one. It is also used to create a named event. ASize determines the maximum size of the mapping. ATimeOut is the time in milliseconds after which a call to Clear, ReadBuffer and WriteBuffer methods is deemed to have failed - typically this happens because another instance of TMapStream is using the event object. functionClear:Boolean
- Clears the data currently in the memory map. Returns False on failure.-
function CopyFrom(AStream:TStream; Count:Integer;):Boolean
- Like theTStream.CopyFrommethod. If Count is zero or greater than the size of AStream it is set toAStream.Size. The method can fail because the event object is locked or because Count is greater than the maximum map size set in the constructor. function ReadBuffer(P:Pointer; Count:Integer):Boolean
- Like theTStream.ReadBuffermethod. Sufficient memory must be allocated to P or else the application is liable to become unstable. The method can fail if the memory map is locked by another instance of TMapStream or if it would be impossible to read Count bytes from the memory map.function WriteBuffer(P:Pointer; Count:Integer):Boolean
- Like theTStream.ReadBuffermethod. If P does not point to at least Count bytes of data, garbage will be written to the memory map. The method can fail if the memory map is locked by another instance of TMapStream or if it would be impossible to write Count bytes from the memory map.
Usage: Sharing data between applications is a four step process
- Each application - they can be different applications or instances of the same application - creates a memory mapped file identified by the same name. It is advisable to use long names containing numbers and underscore characters so as to ensure that you do not accidently grab a handle to another mapping created by a wholly unrelated application. If the map already exists, we simply get a handle to it. Otherwise, Windows creates a new mapping.
- The application(s) use TMapStream methods to manipulate the memory map. In complex applications the return value from the methods should be checked since they may fail owing to mapping being in use by another application.
- When an application writes to the map it is usually with the intention of sharing data with other applications. Evidently, it is necessary to inform the other applications that data are available for reading. Windows messages are used for this purpose.
- The application must also listen in for messages telling it that data written to the map by other applications are available for reading.
The techniques required to accomplish all of the above are demonstrated in the test application, MapTest. The source code for TMapStream is available for just $9.99.Download