Page 1 of 1

Simple shared memory using file maps on Windows

Posted: Wed Oct 06, 2010 11:34 am
by Mistrel
This code can be used to share data across processes. There is more to CreateFileMapping, for example, actually working with files. This library is meant only to demonstrate how to use this function to share memory.

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
Example:

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)

Re: Simple shared memory using file maps on Windows

Posted: Wed Oct 06, 2010 10:13 pm
by idle
Thanks good tip

Re: Simple shared memory using file maps on Windows

Posted: Thu Oct 07, 2010 8:04 am
by blueznl
A similar example back from the 3.9x days is here... It contains some comments which might prove helpful, but be aware those were the 3.9x days and things may need some converting to 4.xx...

http://www.purebasic.fr/english/viewtop ... it=filemap

Re: Simple shared memory using file maps on Windows

Posted: Thu Jul 07, 2011 6:35 am
by Mistrel
Updated the code to be 64-bit compatible and added support for mapping memory > 2 GB.