Simple IPC
Posted: Tue Jan 29, 2008 11:13 am
A simple IPC mechanism. IPCInit() is used only if you need to access the channels from a restricted account (not admin).
Code: Select all
;Simple IPC by Inf0Byt3
;29 Jan 2008
;Free for any use but don't blame me if something bad happens
;Credits appreciated but not necessary
#MaxString = 1024
Global SD.SECURITY_DESCRIPTOR
Global SA.SECURITY_ATTRIBUTES
Global Initiated.b = 0
Procedure.l IPCInit()
If InitializeSecurityDescriptor_(@SD, #SECURITY_DESCRIPTOR_REVISION);
SetSecurityDescriptorDacl_(@SD, #True, #Null, #False)
SA\nLength = SizeOf(SECURITY_ATTRIBUTES);
SA\bInheritHandle = #False;
SA\lpSecurityDescriptor = @SD;
Initiated = 1
ProcedureReturn 1
Else
Initiated = 0
ProcedureReturn 0
EndIf
EndProcedure
Procedure.l IPCSendString(MapFileName.s, String.s)
Protected ReturnValue = #False
If Initiated = 1
MapFileHandle = CreateFileMapping_($FFFFFFFF,0,#PAGE_READWRITE,0,#MaxString,MapFileName)
Else
MapFileHandle = CreateFileMapping_($FFFFFFFF,@SD,#PAGE_READWRITE,0,#MaxString,MapFileName)
EndIf
If MapFileHandle
*MapFilePointer = MapViewOfFile_(MapFileHandle,#FILE_MAP_ALL_ACCESS,#Null,#Null,#MaxString)
If *MapFilePointer <> #Null
PokeS(*MapFilePointer,String,#MaxString)
UnmapViewOfFile_(*MapFilePointer)
ReturnValue = MapFileHandle
EndIf
EndIf
ProcedureReturn ReturnValue
EndProcedure
Procedure.s IPCReceiveString(MapFileName.s)
Protected ReturnValue.s = ""
MapFileHandle = OpenFileMapping_(#FILE_MAP_ALL_ACCESS,0,MapFileName)
If MapFileHandle
*MapFilePointer = MapViewOfFile_(MapFileHandle,#FILE_MAP_ALL_ACCESS,#Null,#Null,#Null)
If *MapFilePointer <> #Null
ReturnValue = PeekS(*MapFilePointer,#MaxString)
UnmapViewOfFile_(MapFileHandle)
CloseHandle_(MapFileHandle)
EndIf
EndIf
ProcedureReturn ReturnValue
EndProcedure
Procedure IPCCloseMap(MapFileHandle.l)
If MapFileHandle > 0
CloseHandle_(MapFileHandle)
EndIf
EndProcedure
For Try = 0 To 9
Handle = IPCSendString("A_MAP_FILE"+Str(Try), "This is a test"+Str(Try))
Debug IPCReceiveString("A_MAP_FILE"+Str(Try))
IPCCloseMap(Handle)
Debug "---------------------------------------------------"
Next