Share data between processes using memory maps
Steps: 1 Call OpenFileMApping Open a named memory image file object. Get HFILEMAP. If successfully jump to step 3. Otherwise proceeding to step 2
2 Call the CreatFilemapping function Create a named memory map to get HFileMap.
3 Call MapViewOffile to get the first byte of the map to the memory LPMEMORY
4 Use this pointer to read and write shared data
5 call unmapviewoffile
6 call closehandle
; >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> >>>>>>>>>>>>>>>>>>>
; Sample Code for
By Luo Yunbin,
http://asm.yeah.net
; >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> >>>>>>>>>>>>>>>>>>>
Mmfshare.asm
; Use memory map files to process data sharing
; >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> >>>>>>>>>>>>>>>>>>>
; Compile and link using NMAKE or the following command:
Ml / c / coff mmfshare.asm
RC mmfshare.rc
; LINK / SUBSYSTEM: Windows MMFShare.obj mmfshare.res
; >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> >>>>>>>>>>>>>>>>>>>
.386
.Model flat, stdcall
Option CaseMAP: NONE
; >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> Folder definition
; >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> >>>>>>>>>>>>>>>>>>>
INCLUDE Windows.inc
INCLUDE User32.inc
INCLUDELIB USER32.LIB
INCLUDE KERNEL32.INC
IncludeLib kernel32.lib
; >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> >>>>>>>>>>>>>>>>>>>
; Equorical definition
; >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> >>>>>>>>>>>>>>>>>>>
ICO_MAIN EQU 1000
DLG_MAIN EQU 100
IDC_TXT EQU 101
IDC_INFO EQU 102
; >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> >>>>>>>>>>>>>>>>>>>
Data segment
; >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> >>>>>>>>>>>>>>>>>>>>>
Hinstance DD?
HwinMain DD?
HFILEMAP DD?
LPMEMORY DD?
.const
Szerr DB 'Unable to establish a memory sharing file', 0
SZMMFNAME DB 'MMF_SHARE_EXAMPLE', 0
; >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> >>>>>>>>>>>>>>>>>>>
Code segment
; >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> >>>>>>>>>>>>>>>>>>>
.code
; >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> >>>>>>>>>>>>>>>>>>>
_Createmmf proc
Invoke openfilemapping, file_map_read or file_map_write, 0, addr szmmfname
.IF! EAX
Invoke CreateFilemapping, -1, NULL, Page_Readwrite, 0,4096, Addr Szmmfname
.IF! EAX
JMP @f
.endif
.endif
Mov Hfilemap, EAX
Invoke MapViewoffile, EAX, File_Map_read or file_map_write, 0,0,0
.if EAX
Mov LPMemory, EAX
Mov DWORD PTR [EAX], 0
RET
.endif
Invoke Closehandle, Hfilemap
@@:
Invoke Messagebox, Hwinmain, Addr Szerr, Null, MB_okinvoke Enddialog, Hwinmain, -1
RET
_Createmmf ENDP
; >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> >>>>>>>>>>>>>>>>>>>
_Closemmf Proc
Invoke UnmapViewoffile, LPMEMory
Invoke Closehandle, HfileMap
Mov LPMemory, 0
Mov HfileMap, 0
RET
_Closemmf endp
; >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> >>>>>>>>>>>>>>>>>>>
_Procdlgmain Proc Uses EBX EDI ESI HWND, WMSG, WPARAM, LPARAM
Local @szbuffer [4096]: Byte
MOV EAX, WMSG
.IF EAX == WM_TIMER
Invoke setdlgitemtext, hwnd, IDC_INFO, LPMEMORY
.ELSEIF EAX == WM_Close
Invoke Killtimer, Hwnd, 1
INVOKE _CLOSEMMF
Invoke EndDialog, Hwinmain, 0
*********************************************************** *******************
.ELSEIF EAX == WM_INITDIALOG
Push hwnd
POP HwinMain
INVOKE LOADICON, HINSTANCE, ICO_MAIN
Invoke SendMessage, Hwnd, WM_SETICON, ICON_BIG, EAX
Invoke Senddlgitemmessage, Hwnd, IDC_TXT, EM_SETLIMITTEXT, 100, 0
Invoke _createmmf
Invoke setTimer, HWND, 1,200, NULL
*********************************************************** *******************
.ELSEIF EAX == WM_COMMAND
Mov Eax, WPARAM
.IF AX == IDC_TXT && LPMEMORY
Invoke getdlgitemtext, hwnd, idc_txt, lpmemory, 4096
.endif
*********************************************************** *******************. ELSE
Mov Eax, False
RET
.endif
Mov Eax, True
RET
_PrOCDLGMain ENDP
; >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> >>>>>>>>>>>>>>>>>>>
Start:
Invoke getModuleHandle, NULL
Mov Hinstance, EAX
Invoke Dialogboxparam, Hinstance, DLG_MAIN, NULL, OFFSET _PROCDLGMAIN, NULL
Invoke EXITPROCESS, NULL
; >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> >>>>>>>>>>>>>>>>>>>
End Start
Some descriptions of procedures:
1. The structure itself is very simple, first call the _procdlgmain subroutine to establish a timer to detect whether the user is input to the dialog box and passes the content to each process via the setdlgiteMtext function. Call the creation memory mapping file function during initialization. Next, go to the six steps mentioned above.
2. When turning off a process, it does not affect the shared data of other processes because the system maintains a counter to the process-shared mapping file object, and each time there is a process to open the mapping file object, the counter will add 1, when the counter is closed. 1. The memory map file only is truly released when the value of the counter is 0 :)