Code: Select all
Procedure SharedMemory_CreateMap(MapName.s, Size.q, Inherit=#False)
Protected hFileMap
Protected *MapView
Protected Security.SECURITY_ATTRIBUTES
Security\nLength=SizeOf(SECURITY_ATTRIBUTES)
Security\bInheritHandle=Inherit
hFileMap=CreateFileMapping_(-1,@Security,#PAGE_READWRITE,PeekL(@Size.q+SizeOf(Long)),PeekL(@Size.q),@MapName.s)
If Not hFileMap
ProcedureReturn #False
EndIf
ProcedureReturn hFileMap
EndProcedure
Procedure.s SharedMemory_GetMapString(MapName.s)
Protected hFileMap
Protected *MapView
Protected Value.s
hFileMap=OpenFileMapping_(#FILE_MAP_ALL_ACCESS,0,@MapName.s)
If Not hFileMap
ProcedureReturn ""
EndIf
*MapView=MapViewOfFile_(hFileMap,#FILE_MAP_WRITE,0,0,0)
If Not *MapView
CloseHandle_(hFileMap)
ProcedureReturn ""
EndIf
;/ Read the map value to the file
Value.s=PeekS(*MapView)
;/ Unmap the mapped view of the file from memory
UnmapViewOfFile_(*MapView)
CloseHandle_(hFileMap)
ProcedureReturn Value.s
EndProcedure
Procedure SharedMemory_SetMapString(MapName.s, Value.s)
Protected hFileMap
Protected *MapView
hFileMap=OpenFileMapping_(#FILE_MAP_ALL_ACCESS,0,@MapName.s)
If Not hFileMap
ProcedureReturn #False
EndIf
*MapView=MapViewOfFile_(hFileMap,#FILE_MAP_WRITE,0,0,0)
If Not *MapView
CloseHandle_(hFileMap)
ProcedureReturn #False
EndIf
;/ Write the map value to the file
PokeS(*MapView,Value.s)
;/ Unmap the mapped view of the file from memory
UnmapViewOfFile_(*MapView)
CloseHandle_(hFileMap)
ProcedureReturn #True
EndProcedure
Procedure SharedMemory_GetMapPtr(MapName.s)
Protected hFileMap
Protected *MapView
hFileMap=OpenFileMapping_(#FILE_MAP_ALL_ACCESS,0,@MapName.s)
If Not hFileMap
ProcedureReturn #False
EndIf
*MapView=MapViewOfFile_(hFileMap,#FILE_MAP_WRITE,0,0,0)
If Not *MapView
CloseHandle_(hFileMap)
ProcedureReturn #False
EndIf
CloseHandle_(hFileMap)
ProcedureReturn *MapView
EndProcedure
Procedure SharedMemory_CloseMapPtr(*MapPtr)
;/ Unmap the mapped view of the file from memory
If Not UnmapViewOfFile_(*MapPtr)
ProcedureReturn #False
EndIf
ProcedureReturn #True
EndProcedure
Procedure SharedMemory_CloseMap(hMap)
If Not CloseHandle_(hMap)
ProcedureReturn #True
EndIf
ProcedureReturn #False
EndProcedure
Code: Select all
MapName.s="TestMap222"
hMap=SharedMemory_CreateMap(MapName.s,256)
SharedMemory_SetMapString(MapName.s,"1")
;/ Output 1
Debug SharedMemory_GetMapString(MapName.s)
SharedMemory_SetMapString(MapName.s,"2")
;/ Output 2
Debug SharedMemory_GetMapString(MapName.s)
*MapView=SharedMemory_GetMapPtr(MapName.s)
;/ Output 2
Debug PeekS(*MapView)
SharedMemory_CloseMapPtr(*MapView)
SharedMemory_CloseMap(hMap)
;/ Output a blank string (the map is closed)
Debug SharedMemory_GetMapString(MapName.s)