Presented below the SharedMemory class uses FileMapping API calls to allocate a block of memory accessible from any FoxPro application that can instantiate this class. All applications share the name of this memory buffer, for example "MYAPPSHAREDMEMORY".

To test this class, start two or more VFP executables and run this code in each session:
LOCAL oForm As TForm
oForm = CREATEOBJECT("Tform")
oForm.Show(1)
* end of main
DEFINE CLASS Tform As Form
Width=460
Height=200
Caption="Using shared memory to exchange " +;
"data between processes"
smObject=NULL
ADD OBJECT txtSharedMemory As EditBox WITH;
Left=5, Top=10, Width=450, Height=120
ADD OBJECT cmdReadFromMemory As CommandButton WITH;
Left=40, Top=150, Width=190, Height=27,;
Caption="Read From Shared Memory"
ADD OBJECT cmdWriteToMemory As CommandButton WITH;
Left=230, Top=150, Width=190, Height=27,;
Caption="Write To Shared Memory"
PROCEDURE Init
THIS.smObject = CREATEOBJECT("SharedMemory",;
"MYAPPSHAREDMEMORY", 1024)
PROCEDURE cmdReadFromMemory.Click
ThisForm.txtSharedMemory.Value =;
ThisForm.smObject.ReadFromSharedMemory()
PROCEDURE cmdWriteToMemory.Click
LOCAL cBuffer
cBuffer = PADR(ThisForm.txtSharedMemory.Value,;
ThisForm.smObject.GetSharedMemorySize())
ThisForm.smObject.WriteToSharedMemory(m.cBuffer)
ENDDEFINE
See also:
Using a Semaphore object to prevent VFP application from running more than one instance
Using Windows Clipboard for passing data records between VFP applications
|