WinBatch Tech Support Home

Database Search

If you can't find the information using the categories below, post a question over in our WinBatch Tech Support Forum.

TechHome

Process UDFs

Can't find the information you are looking for here? Then leave a message over on our WinBatch Tech Support Forum.

UDF for Using Memory Mapped Files for Interprocess Communication


;*******************************************************************************
;
;  Create shared memory.
;
;   The parameter is a name for the shared memory.  (Any name, assigned by you.)
;
;   Another program running on the same machine, which opens memory using
;   the same name, will be given access to the same shared memory.
;
;  The return value...
;
;      0 ... open failed.
;  Not 0 ... A handle, which you must save and pass to the
;            other shared memory functions.
;
; ------------------------------------------------------------------------------
;   The routines are written wwith a size limit of 256 characters for the memory.
;   To increase this, change the "MaxSize"  value in each of the functions.
;
;*********************************************************************************
#DefineFunction OpenSharedMemory(MemoryName)
   MaxSize = 256
   kernel32=DllLoad(StrCat(DirWindows(1),"Kernel32.DLL"))
   Handle = DllCall(Kernel32, long:"CreateFileMappingA", long:-1,long:0,long:4,long:0,long:MaxSize+2,lpstr:MemoryName)
   If Handle != 0
       View = DllCall(Kernel32, long:"MapViewOfFile",long:Handle,long:2,long:0,long:0,long:0)
       If View != 0 Then
           DllFree(Kernel32)
           Return StrCat(Handle,@TAB,View)
       End If
       DllCall(Kernel32,long:"CloseHandle",long:Handle)
   End If
   DllFree(Kernel32)
   Return 0
#EndFunction

;*********************************************************************************
;
;    Read a value from shared memory.
;
;    The parameter is the handle returned from calling "OpenSharedMemory"
;
;    The return value is a string,  the contents of the shared memory.
;
;*********************************************************************************
#DefineFunction ReadSharedMemory(Handle)
   MaxSize = 256
   View = ItemExtract(2,Handle,@TAB)
   Kernel32 = DllLoad(StrCat(DirWindows(1),"Kernel32.DLL"))
   Buffer = BinaryAlloc(MaxSize+2)
   BinaryEodSet(Buffer,MaxSize)
   ;
   ;  We're about to call a function to copy a null-delimited string into our
   ;  buffer.  We can't guarantee that the string in the file is actually
   ;  null terminated,and if not it would overflow our buffer.  So before copying,
   ;  put a NULL byte at the maximum length.
   ;
   DllCall(Kernel32,long:"lstrcpyA",long:View+MaxSize,lpstr:"")
   ;
   ;  Now copy the string into our binary buffer.
   ;
   If DllCall(Kernel32,long:"lstrcpyA",lpbinary:Buffer,long:ItemExtract(2,Handle,@TAB)) == 0
       Text = ""
   Else
       Text = BinaryPeekStr(Buffer,0,MaxSize)
   EndIf
   DllFree(Kernel32)
   BinaryFree(Buffer)
   Return Text
#EndFunction

;*********************************************************************************
;
;    Write a string to shared memory.
;
;    Parameter 1 is the handle returned from calling "OpenSharedMemory"
;
;    Parameter 2 is any string.  The contents of this string are written to
;    the shared memory.  They will overwrite the previous contents.
;
;    Strings longer than the "MaxLength" will be truncated.
;
;    Return value...
;       @TRUE ... Successful
;       @FALSE .. There was an error
;
;*********************************************************************************
#DefineFunction WriteSharedMemory(Handle,Text)
   MaxSize = 256
   If StrLen(Text) > MaxSize Then Text = StrSub(Text,1,MaxSize)
   If DllCall(StrCat(DirWindows(1),"Kernel32.DLL"),long:"lstrcpyA",long:ItemExtract(2,Handle,@TAB),lpstr:Text) == 0
       Return @FALSE
   Else
       Return @TRUE
   EndIf
#EndFunction

;*********************************************************************************
;
;    Close a shared memory block.
;
;    The parameter is the handle returned from calling "OpenSharedMemory"
;
;    Return value is always 1
;
;*********************************************************************************
#DefineFunction  CloseSharedMemory(Handle)
    Kernel32=DllLoad(StrCat(DirWindows(1),"Kernel32.DLL"))
    DllCall(Kernel32,long:"UnmapViewOfFile",long:ItemExtract(2,Handle,@TAB))
    DllCall(Kernel32,long:"CloseHandle",long:ItemExtract(1,Handle,@TAB))
    DllFree(Kernel32)
    Return 1
#EndFunction

Article ID:   W16239
File Created: 2011:01:28:09:00:12
Last Updated: 2011:01:28:09:00:12