2Way App Communication

BlitzMax Forums/BlitzMax Programming/2Way App Communication

Hi :)
I'm going to create an application, which needs to have 2 executables running at the same time, and I need both of them to be able to write and recieve from the other one.

I've used search and found PUB.FreeProcess, but all the examples are only a 1 way communication.

Anyone care to explain how to do this?
If it's even possible, but it should be :P

Thanks

Look for Mutex, Shared Memory and Messaging.

You can use sockets to do it.
You can also use shared memory through a dll.
sockets is the most common though.

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


Aight, Thanks a bunch mates, I'll go for the Sockets method tho, seems easiest.

Thanks again :)

Or Budman's shared memory code without a DLL:
But then has a huge potential (as far as I can see) for concurrency issues.

Use Sockets.

If you're on Linux, use d-bus.
Lots of choices :)

Write usefull data into the shared memory and you don't have any problems.

ie write the "target" as first byte in and only that app reads out the data that needs it.

As the memory itself will handle concurrent accesses, you won't get any of the regular lockup issues.

It's not a democratic decision to be made, but I also vote for sockets.

As the memory itself will handle concurrent accesses,
No it won't.

Thanks for the replys! I'll go with the Sockets anyway, seems easiest and will give me an extra feature, to use the two applications on 2 different computers, creating more stability.

By the way,
Does anyone know how to execute an .exe file without the .exe file closing when I close the application that opened the file?

Thanks again :)