Sharing array() between exe
Posted: Tue Jul 26, 2016 11:33 am
Hello at all
Today it's not christmas but nearly
KCC dare to show to you, his code for sharing arrays between exe
Little KCC need your MASTER advice for know if it's a good way for do that (Do not strike too hard on the head
)
My goal ?? create a little library, the more simple possible, for do the job
Thanks to BISONTE i have learning the FileMapping
And Thanks to LUNASOLE, i have a magical MACRO for simplify the sending of the array to the procedure
KCC advice before read.....take an aspirin

Have a good day
Today it's not christmas but nearly
KCC dare to show to you, his code for sharing arrays between exe
Little KCC need your MASTER advice for know if it's a good way for do that (Do not strike too hard on the head
My goal ?? create a little library, the more simple possible, for do the job
Thanks to BISONTE i have learning the FileMapping
And Thanks to LUNASOLE, i have a magical MACRO for simplify the sending of the array to the procedure
KCC advice before read.....take an aspirin

Code: Select all
; Array Shared by KCC
; Thanks to BISONTE for the FileMapping solution and LUNASOLE for his splendid MACRO
Structure Arrays
Name.s
Value.l
Size.l
EndStructure
Global Var.Arrays
Macro DQuote
"
EndMacro
Macro ShareArray_Init(v)
Var\Name = DQuote#@v#DQuote
Var\Value = @v
Var\Size = ArraySize(v)
EndMacro
Procedure ShareArray_Write()
If FindString(Var\Name, "$") Or FindString(Var\Name, ".s", #PB_String_NoCase)
Dim ArrayTemp.s(Var\Size)
CopyMemory(Var\Value, @ArrayTemp(), (Var\Size + 1) * SizeOf(Long))
For i = 1 To Var\Size
SentenceArray$ + ArrayTemp(i) + "|"
Next
LenData = Len(SentenceArray$)
Type = #PB_String
Else
LenData = (Var\Size + 1) * SizeOf(Long)
Type = #PB_Long
EndIf
hFileMap = CreateFileMapping_(#INVALID_HANDLE_VALUE ,#Null, #PAGE_READWRITE, 0, LenData, Var\Name)
If Not hFileMap
MessageRequester("Error", "Impossible to create shared memory")
ProcedureReturn #False
EndIf
If GetLastError_() <> #ERROR_ALREADY_EXISTS
hFileMap = OpenFileMapping_(#FILE_MAP_ALL_ACCESS, 0, Var\Name)
If hFileMap
*PtrDatas = MapViewOfFile_(hFileMap, #FILE_MAP_WRITE, 0, 0, 0)
Select Type
Case #PB_String
PokeS(*PtrDatas, SentenceArray$)
FreeArray(ArrayTemp())
Case #PB_Long
CopyMemory(Var\Value, *PtrDatas, LenData)
EndSelect
EndIf
EndIf
ProcedureReturn hFileMap
EndProcedure
Procedure ShareArray_Read()
hFileMap = OpenFileMapping_(#FILE_MAP_ALL_ACCESS, 0, Var\Name)
If hFileMap
*PtrDatas = MapViewOfFile_(hFileMap, #FILE_MAP_WRITE, 0, 0, 0)
If *PtrDatas
ProcedureReturn *PtrDatas
EndIf
EndIf
EndProcedure
Procedure ShareArray_Close(hFileMap)
CloseHandle_(hFileMap)
Var\Name = ""
Var\Value = 0
Var\Size = 0
EndProcedure
; ******************************************************************
; TEST (Run one time...or two time for have another EXE)
; ******************************************************************
; Loading array of string
Dim ArrayString.s(10)
For i = 1 To 10
ArrayString(i) = "Kcc n° " + Trim(Str(i))
Next
; Manage of sharing
ShareArray_Init(ArrayString.s())
hFileMap = ShareArray_Write()
*PtrArray = ShareArray_Read()
SentenceArray$ = PeekS(*PtrArray)
; Read return of sharing
For i = 1 To Var\Size
Debug StringField(SentenceArray$, i, "|")
Next
ShareArray_Close(hFileMap)
; Loading array of Long
Dim TabloLong.l(10)
For i = 1 To 10
TabloLong(i) = i
Next
; Manage sharing of array long
ShareArray_Init(TabloLong.l())
hFileMap = ShareArray_Write()
*PtrArray = ShareArray_Read()
; Read return of sharing
Dim ArrayTemp.l(Var\Size)
CopyMemory(*PtrArray, @ArrayTemp(), (Var\Size + 1) * SizeOf(Long))
For i = 1 To Var\Size
Debug ArrayTemp(i)
Next
ShareArray_Close(hFileMap)
FreeArray(ArrayTemp())
Repeat : Delay(1) :Until GetAsyncKeyState_(#VK_ESCAPE)