Memory map file continues

xiaoxiao2021-03-06  41

2005

The new year has come, and I received a lot of blessings on December 31, 2004, and I am very grateful to my friends. My training also reported a paragraph. After returning the unit, you should face formal work. The big mouth sent the information yesterday that the brothers of the school have lived very well. Their BOSS also asked them to fall. Oh, I really want to go back to school ~~~ I don't mean in two days, I still have to see book learning. Then then digest the memory map file.

Steps to use the memory map files two steps:

1 Create a memory mapping file object using CreatFilemapping.

Invoke Creatfilemapping, Hfile, LpfilemappingAttributes, Flprotect, Dwmaximumsizehigh, / /

Dwmaximumsizelow, LPNAME

.if EAX

Mov Hfilemap, EAX

.endif

This function determines the purpose of the mapping file: Is the disk file established a memory map or builds a transllection between the memory on the memory page. The above functions can be implemented by setting the value of the first parameter HFile. If the HFile handle is a file that belongs to a already opened, the mapping file is established on this file. If you want to build a memory map file to be shared in the memory file, you specify the HFile as -1.

LPFILEMAPPINGATTRIBUTES: Whether memory map files can be inherited. NULL is not inherited.

FLPROTECT: Memory mapping file protection method, Note To open the file with the CreatFile function to get the HFile handle, you must specify the corresponding flag.

Flprotect, dwmaximumsizehigh / dwmaximumsize :: Specify the length of the 64-bit memory map file. Setting these two parameters to zero, the system automatically adjusts the size of the memory map file to the size of the disk file.

LPNAME: The name of the memory map file. The mapping file must be named when the process is shared.

Use OpenFilemapping to open the bundled memory mapped files that have been created for the process.

Invoke OpenFilemapping, DwdesiredAccess, BinheritHandle, LPNAME

.if EAX

Mov Hfilemap, EAX

.endif

DwdesiredAccess: Specify the protection type.

LPNAME: The name used when creating an object.

BinheritHandle: Inherited handle.

2 Create a view of the memory mapping file. It is equivalent to assigning a line-shaped address space to the memory mapping file and associates the linear address with the contents of the file. This application can access files by accessing threaded addresses.

Invoke MapViewoffile, Hfilemap, DwdesiredAccess, DWFileOffSeth, DwfileOffsetlow, /

Dwnumberofbytestomap

.if EAX

MOV IPMemory, EAX

.endif

Not much to say this function is very simple.

Cancel mapping file: invoke unmapViewoffile, LPMEMory

Close Map: Invoke CloseHandle, HfileMap

Eat :) Ponsed again! ~

Haha, I am coming ~~~ I will sleep in the afternoon, I feel comfortable :) The weather is good! But not at night, otherwise I will learn the monkey to say the last sentence: the moonlight is good, Qingquan stone is flowing ~~~~~~~~~ hiahia

Address: Write the modification of the file to the disk is called this function.

Invoke Flushviewoffile, LPMEMORY, DWFILESIZE

I don't know if your idea is chaos :) Summary now to use the memory map file to read and write the file:

1. Call CreatFile Open the file you want to map and get HFile

2. Call the CreatFileMapping function Generate a memory mapping object based on file objects created in the Creatfile function, get HFILEMAP

3. Call the MapViewOffile function to map a zone or entire area of ​​the entire file to the memory, get the first byte of the first byte of the first byte of the memory. LPMEMory

4. Use this pointer to read and write files

5. Call unmapViewoffile to release file mapping, incoming parameters are LPMEMORY

6. Call CloseHandle to turn off the memory map file, incoming parameters are HFILEMAP

7. Call the CloseHandle to close the file, the incoming function is HFile

Let's take a look at Luo Yunbin's "Windows32 Assess Program Design":

; >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> >>>>>>>>>>>>>>>>>>>

; Sample Code for

By Luo Yunbin,

http://asm.yeah.net

; >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> >>>>>>>>>>>>>>>>>>>

WordCount.asm

; File Read Write Example - Open text file for word statistics, then create result files

Read and write file operations use the memory map file function.

; >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> >>>>>>>>>>>>>>>>>>>

; Compile and link using NMAKE or the following command:

; ML / C / Coff WordCount.asm

RC WordCount.rc

; LINK / SUBSYSTEM: Windows WordCount.obj WordCount.res

; >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> >>>>>>>>>>>>>>>>>>>>>>> Folder. 386

.Model flat, stdcall

Option CaseMAP: NONE

; >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> >>>>>>>>>>>>>>>>>>>

Include file definition

; >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> >>>>>>>>>>>>>>>>>>>

INCLUDE Windows.inc

INCLUDE User32.inc

INCLUDELIB USER32.LIB

INCLUDE KERNEL32.INC

IncludeLib kernel32.lib

INCLUDE COMDLG32.INC

INCLUDELIB COMDLG32.LIB

; >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> >>>>>>>>>>>>>>>>>>>

; Equorical definition

; >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> >>>>>>>>>>>>>>>>>>>

ICO_MAIN EQU 1000

DLG_MAIN EQU 100

IDC_File EQU 101

IDC_BROWSE EQU 102word_count struct

LPletter DD 26 DUP (?)

DWCOUNT DD?

DwDepth DD?

Word_count ends

; >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> >>>>>>>>>>>>>>>>>>>

Data segment

; >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> >>>>>>>>>>>>>>>>>>>

.DATA?

Hinstance DD?

HwinMain DD?

SZFILENAME DB MAX_PATH DUP (?)

SZBuffer DB 4096 DUP (?)

STWORDCOUNT WORD_COUNT <>

DWCOUNT DD?

DWOPTION DD?

F_counting EQU 00000001H

F_filend EQU 00000002H

.const

SZFILEXT DB 'All File', 0, '*. *', 0, 0

SzloGext db '.log', 0

Szerropenfile db 'can't open the file!', 0

SzerrcreateFile DB 'Unable to establish a record file!', 0

SZFMTWORD DB '% 5d (% 3D ‰)% s', 0DH, 0AH, 0

Szsuccees DB 'is successful, please check the record file% s', 0

Szsucceescap DB 'statistics success', 0

; >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> >>>>>>>>>>>>>>>>>>>

Code segment

; >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> Folder

; >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> >>>>>>>>>>>>>>>>>>>

Traverse the tree and output the result

; >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> >>>>>>>>>>>>>>>>>>>

_WalkTree Proc_hfile, _lpwc, _lpsz

Local @dwtemp

Local @szword [52]: Byte

Pushhad

Mov ESI, _LPWC

Assume ESI

Tr word_count

.IF [ESI] .dwdepth && [ESI] .dwcount

*********************************************************** *******************

; Calculate percentage and write to log files

*********************************************************** *******************

MOV Eax, [ESI] .dwcount

MOV ECX, 1000

Mul ECX

Mov ECX, DWCOUNT

.IF ECX

Div ECX

.lse

MOV EAX, 0

.endif

Invoke WSPrintf, Addr Szbuffer, Addr Szfmtword, [ESI] .dwcount, Eax, _lpsz

Invoke lstrlen, Addr Szbuffer

MOV ECX, EAX

Invoke Writefile, _HFILE, AddR Szbuffer, ECX, Addr @ dwtemp, null

.endif

*********************************************************** *******************

If there is a lower node, please call

*********************************************************** *******************

Mov @ dwtemp, 0

.while @dwtemp <26

MOV EBX, @ dwtempmov EBX, DWORD PTR [ESI EBX * 4]

.IF EBX

Invoke lstrcpy, addr @ szword, _lpsz

Invoke lstrlen, addr @SZWORD

Lea ECX, @ Szword

Add ECX, EAX

MOV Eax, @ dwtemp

Add Al, 'A'

Mov Word PTR [ECX], AX

Invoke _walktree, _hfile, ebx, addr @szword

.endif

INC @dwtemp

.endw

*********************************************************** *******************

Release node

*********************************************************** *******************

.IF [ESI] .dwdepth

Invoke Globalfree, ESI

.endif

Popad

Assume ESI

Tr word_count

RET

_WalkTree Endp

; >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> >>>>>>>>>>>>>>>>>>>

; Establish a tree node or increase node count based on data

; >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> >>>>>>>>>>>>>>>>>>>

_Countletter Proc_dwletter

Local @dwindex

Assume EDI

Tr word_count

OR Al, 20H; Convert to lowercase letters

.IF (Al> = 'a') && (al <= 'z'); letters

*********************************************************** *******************

If it is a letter, press the EDI pointer to continue to search for the next node, find the update pointer,

Did not find a new node

*********************************************************** *******************

SUB Al, 'A'

Movzx Eax, Al

.IF DWORD PTR [EDI EAX * 4]

MOV EDI, DWORD PTR [EDI EAX * 4]

.lse

MOV EBX, [EDI] .dwdepth

EBX <50push EAX

Invoke GlobalLoc, GPTR, SIZEOF WORD_COUNT

POP ECX

.if EAX

MOV DWORD PTR [EDI ECX * 4], EAX

Mov Edi, EAX

Inc EBX

MOV [EDI] .dwdepth, EBX

.endif

.endif

.endif

.lse

*********************************************************** *******************

Not the letter indicating that a word has ended, increasing the word count

*********************************************************** *******************

INC [EDI] .dwcount

.IF [EDI] .dwdepth

Inc dwcount

.endif

Mov Edi, Offset StwordCount

.endif

Assume EDI: Nothing

RET

_Countletter Endp

; >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> >>>>>>>>>>>>>>>>>>>

_CoucentWord Proc

Local @ hfile, @ hfilemap, @ lpMemory, @ dwfilesis

Local @szlogfile [max_path]: Byte

Local @szbuffer

Invoke RTLZERMEMORY, AddR Stwordcount, Sizeof Stwordcount

*********************************************************** *******************

; open a file

*********************************************************** *******************

Invoke createfile, addr szfilename, generic_read, file_share_read, 0, /

Open_EXISTING, FILE_ATTRIBUTE_NORMAL, 0

.IF eax == invalid_handle_value

JMP_ERROR

.endif

Mov @ Hfile, EAX

Invoke getFilesize, @ hfile, null

Mov @ dwfilesize, eax

*********************************************************** *******************

Establish memory map files and process each byte

*********************************************************** *******************

Invoke createfilemapping, @ hfile, null, page_readonly, 0, 0, null

.if EAX

Mov @ HfileMap, EAX

Invoke MapViewoffile, EAX, File_Map_read, 0,0,0.if Eax

Mov @ lpmemory, EAX

JMP @f

.endif

Invoke Closehandle, @ hfilemap

.endif

JMP_ERROR

@@:

XOR EAX, EAX

Mov dwcount, EAX

Mov ESI, @ lpmemory

Mov Edi, Offset StwordCount

.while @dwfilesize

Lodsb

Dec @dwfilesize

Invoke _countletter, EAX

.endw

INVOKE _COUNTLETTER, 0

*********************************************************** *******************

Close the memory map file

*********************************************************** *******************

Invoke unmapviewoffile, @ lpmemory

Invoke Closehandle, @ hfilemap

Invoke closehandle, @ hfile

*********************************************************** *******************

Output record file

*********************************************************** *******************

Invoke lstrcpy, addr @ szlogfile, addr szfilename

Invoke lstrcat, addr @ szlogfile, addr szlogext

Invoke createfile, addr @ szlogfile, generic_write, file_share_read, /

0, create_always, file_attribute_normal, 0

.IF EAX! = INVALID_HANDLE_VALUE

Mov @ Hfile, EAX

Mov @ SZBuffer, 0

Mov Edi, Offset StwordCount

Invoke _walktree, @ hfile, edi, addr @szbuffer

Invoke closehandle, @ hfile

Invoke WSPrintf, Addr Szbuffer, Addr Szsuccees, Addr @Szlogfile

Invoke Messagebox, HwinMain, Addr Szbuffer, AddR Szsuckcescap, MB_OK or MB_ICONITIONMATION

.lse

Invoke Messagebox, Hwinmain, Addr Szerrcreatefile, NULL, MB_OK OR MB_ICONEXCLAMATION

.endif

RET

_ERROR:

Invoke Messagebox, HwinMain, Addr Szerropenfile, Null, MB_OK or MB_ICONEXCLAMATION

RET

_CountWord Endp

; >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> EDI ESI HWND, WMSG, WPARAM, LPARAM

Local @stopenfilename: OpenFileName

MOV EAX, WMSG

.IF EAX == WM_Close

.IF! (dwoption & f_counting)

Invoke EndDialog, Hwnd, Null

.endif

*********************************************************** *******************

.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_File, EM_SETLIMITTEXT, MAX_PATH, 0

*********************************************************** *******************

.ELSEIF EAX == WM_COMMAND

Mov Eax, WPARAM

.IF AX == IDC_BROWSE

*********************************************************** *******************

Invoke RTLZERMEMORY, AddR @ StopenFileName, Sizeof OpenFileName

Mov @ stopenfilename.lstructsize, sizeof @stopenfilename

Mov @ stopenfilename.flags, OFN_FILEMUSTEXIST or OFN_PATHMUSTEXIST

Push hwinmain

POP @ stopenfilename.hwndowner

Mov @ stopenfilename.lpstrfilter, Offset SzfileExt

Mov @ stopenfilename.lpstrfile, offset szfilename

Mov @ stopenfilename.nmaxfile, max_path

Invoke GetopenFileName, Addr @stopenFileName

.if EAX

Invoke setdlgitemtext, hwnd, idc_file, addr szfilename

.endif

*********************************************************** *******************

.ELSEIF AX == IDC_File

Invoke getdlgitemtext, hwnd, idc_file, addr szfilename, max_pathmov EBX, EAX

Invoke Getdlgitem, HWnd, IDOK

Invoke EnableWindow, EAX, EBX

*********************************************************** *******************

.ELSEIF AX == IDOK

Invoke getdlgitem, hwnd, idc_file

Invoke EnableWindow, EAX, FALSE

Invoke getdlgitem, hwnd, idc_browse

Invoke EnableWindow, EAX, FALSE

Invoke Getdlgitem, HWnd, IDOK

Invoke EnableWindow, EAX, FALSE

OR dwoption, f_counting

Call_countword

And dwoption, not (f_counting or f_filend)

Invoke getdlgitem, hwnd, idc_file

Invoke EnableWindow, Eax, True

Invoke getdlgitem, hwnd, idc_browse

Invoke EnableWindow, Eax, True

Invoke Getdlgitem, HWnd, IDOK

Invoke EnableWindow, Eax, True

.endif

*********************************************************** *******************

.lse

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

转载请注明原文地址:https://www.9cbs.com/read-51592.html

New Post(0)