Simple shared memory using file maps on Windows

Share your advanced PureBasic knowledge/code with the community.
Mistrel
Addict
Addict
Posts: 3415
Joined: Sat Jun 30, 2007 8:04 pm

Simple shared memory using file maps on Windows

Post 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)
Last edited by Mistrel on Thu Jul 07, 2011 6:42 am, edited 2 times in total.
User avatar
idle
Always Here
Always Here
Posts: 5836
Joined: Fri Sep 21, 2007 5:52 am
Location: New Zealand

Re: Simple shared memory using file maps on Windows

Post by idle »

Thanks good tip
User avatar
blueznl
PureBasic Expert
PureBasic Expert
Posts: 6166
Joined: Sat May 17, 2003 11:31 am
Contact:

Re: Simple shared memory using file maps on Windows

Post 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
( PB6.00 LTS Win11 x64 Asrock AB350 Pro4 Ryzen 5 3600 32GB GTX1060 6GB)
( The path to enlightenment and the PureBasic Survival Guide right here... )
Mistrel
Addict
Addict
Posts: 3415
Joined: Sat Jun 30, 2007 8:04 pm

Re: Simple shared memory using file maps on Windows

Post by Mistrel »

Updated the code to be 64-bit compatible and added support for mapping memory > 2 GB.
Post Reply