2Way App Communication

BlitzMax Forums/BlitzMax Programming/2Way App Communication

You can also use shared memory through a dll.


Or Budman's shared memory code without a DLL:


' Simple program to demonstrate sharing memory between process
' compile as console program and run one or more times the first run sets the 
' shared memory
' additonal runs read it

SuperStrict
Extern "Win32"
    Function CloseHandle:Int(Handle:Int)
	Function MapViewOfFile:Byte Ptr(hFileMappingObject: Int,dwDesiredAccess: Int, dwFileOffsetHigh:Int, dwFileOffsetLow:Int, dwNumberOfBytesToMap:Int)
	Function UnmapViewOfFile:Int(lpBaseAddress: Byte Ptr)
	Function CreateFileMappingA:Int(hFile: Int, lpFileMappingAttributes: Byte Ptr,flProtect:Int, dwMaximumSizeHigh:Int, dwMaximumSizeLow:Int, lpName$z)
	Function GetLastError:Int()
End Extern

Const PAGE_READWRITE : Int=4
Const FILE_MAP_WRITE : Int=2

Type TSharedMemory 
	Field _Handle : Int
	Field _Name   : String
	Field _Size   : Int
    Field _Owner  : Int
	Field _Data   : Byte Ptr

   
	Method Delete()
		Close
	End Method
	Method Open:Int(Name: String, Size: Int)
		Close()
   		_Name = Name
    	_Size = Size
       ' CreateFileMapping, when called with $FFFFFFFF For the hanlde value, creates a region of shared memory }
   		_Handle = CreateFileMappingA($FFFFFFFF, Null, PAGE_READWRITE, 0,_Size,_Name)
    	If _Handle = 0 Then Return False
        _Owner = GetLastError() = 0
       ' We still need To map a pointer To the handle of the shared memory region 
        _Data= MapViewOfFile(_Handle, FILE_MAP_WRITE, 0, 0, _Size)
    	If Not _Data
			Close
			Return False
		End If
		Return True
	End Method 
	Method Close()
  		If _Data
	    	UnmapViewOfFile(_Data);
			_Data=Null
		End If
		If _Handle 
			CloseHandle(_Handle)
			_Handle=0
		End If 		
	End Method
End Type

Print "Test Share Mem"
Local mem:TSharedMemory = New TSharedMemory 


mem.Open("TESTMEM",30)

If mem._Owner
	MemClear mem._Data,30
	Local title:String="Start Blank!"
	Local pTitle:Byte Ptr=title.toCString()
	MemCopy mem._Data,pTitle,title.length+1
	MemFree pTitle
End If 
	
Repeat
If mem._Owner
	
	Print "Getting Title (Owner)"
	Local title : String = string.FromCString(mem._Data)		
	Print "The Title is "+title
	GCCollect()
Else
	Print "Setting Title (Guest)"
	MemClear mem._Data,30
	SeedRnd MilliSecs() 

	Local title:String=Rand(0,100000)
	Local pTitle:Byte Ptr=title.toCString()
	MemCopy mem._Data,pTitle,title.length+1
	MemFree pTitle
	GCCollect()
	mem.close
	End
'	Print "Getting Title"
'	Local title : String = string.FromCString(mem._Data)		
'	Print "The Title is "+title
End If
'Input
'Delay 2000

Forever
mem.Close