📄 lion-tutorial13.htm
字号:
<br>
<b> ret</b> <br>
<b>CloseMapFile endp</b>
<p><b>end start</b> <br>
<h3> <b>Analysis:</b></h3>
<b>
invoke CreateFile,ADDR buffer,\</b> <br>
<b>
GENERIC_READ ,\</b> <br>
<b>
0,\</b> <br>
<b>
NULL,OPEN_EXISTING,FILE_ATTRIBUTE_ARCHIVE,\</b> <br>
<b>
NULL</b>
<p>When the user selects a file in the open file dialog, we call CreateFile to
open it. Note that we specify GENERIC_READ to open this file for read-only access
and dwShareMode is zero because we don't want some other process to modify the
file during our operation.
<p><b>
invoke CreateFileMapping,hFileRead,NULL,PAGE_READONLY,0,0,NULL</b>
<p>Then we call CreateFileMapping to create a memory mapped file from the opened
file. CreateFileMapping has the following syntax:
<p><b>CreateFileMapping proto hFile:DWORD,\</b> <br>
<b>
lpFileMappingAttributes:DWORD,\</b> <br>
<b>
flProtect:DWORD,\</b> <br>
<b>
dwMaximumSizeHigh:DWORD,\</b> <br>
<b>
dwMaximumSizeLow:DWORD,\</b> <br>
<b>
lpName:DWORD</b>
<p>You should know first that CreateFileMapping doesn't have to map the whole
file to memory. You can use this function to map only a part of the actual file
to memory. You specify the size of the memory mapped file in dwMaximumSizeHigh
and dwMaximumSizeLow params. If you specify the size that 's larger than the
actual file, the actual file will be expanded to the new size. If you want the
memory mapped file to be the same size as the actual file, put zeroes in both
params. <br>
You can use NULL in lpFileMappingAttributes parameter to have Windows creates
a memory mapped file with default security attributes. <br>
flProtect defines the protection desired for the memory mapped file. In our
example, we use PAGE_READONLY to allow only read operation on the memory mapped
file. Note that this attribute must not contradict the attribute used in CreateFile
else CreateFileMapping will fail. <br>
lpName points to the name of the memory mapped file. If you want to share this
file with other process, you must provide it a name. But in our example, our
process is the only one that uses this file so we ignore this parameter.
<p><b>
mov eax,OFFSET buffer</b> <br>
<b>
movzx edx,ofn.nFileOffset</b> <br>
<b>
add eax,edx</b> <br>
<b>
invoke SetWindowText,hWnd,eax</b>
<p>If CreateFileMapping is successful, we change the window caption to the name
of the opened file. The filename with full path is stored in buffer, we want
to display only the filename in the caption so we must add the value of nFileOffset
member of the OPENFILENAME structure to the address of buffer.
<p><b>
invoke EnableMenuItem,hMenu,IDM_OPEN,MF_GRAYED</b> <br>
<b>
invoke EnableMenuItem,hMenu,IDM_SAVE,MF_ENABLED</b>
<p>As a precaution, we don't want the user to open multiple files at once, so
we gray out the Open menu item and enable the Save menu item. EnableMenuItem
is used to change the attribute of menu item. <br>
After this, we wait for the user to select File/Save as menu item or close our
program. If the user chooses to close our program, we must close the memory
mapped file and the actual file like the code below:
<p><b> .ELSEIF uMsg==WM_DESTROY</b> <br>
<b> .if hMapFile!=0</b> <br>
<b> call CloseMapFile</b>
<br>
<b> .endif</b> <br>
<b> invoke PostQuitMessage,NULL</b>
<p>In the above code snippet, when the window procedure receives the WM_DESTROY
message, it checks the value of hMapFile first whether it is zero or not. If
it's not zero, it calls CloseMapFile function which contains the following code:
<p><b>CloseMapFile PROC</b> <br>
<b> invoke CloseHandle,hMapFile</b>
<br>
<b> mov hMapFile,0</b>
<br>
<b> invoke CloseHandle,hFileRead</b>
<br>
<b> ret</b> <br>
<b>CloseMapFile endp</b>
<p>CloseMapFile closes the memory mapped file and the actual file so that there
'll be no resource leakage when our program exits to Windows. <br>
If the user chooses to save that data to another file, the program presents
him with a save as dialog box. After he types in the name of the new file, the
file is created by CreateFile function.
<p><b>
invoke MapViewOfFile,hMapFile,FILE_MAP_READ,0,0,0</b> <br>
<b>
mov pMemory,eax</b>
<p>Immediately after the output file is created, we call MapViewOfFile to map
the desired portion of the memory mapped file into memory. This function has
the following syntax:
<p><b>MapViewOfFile proto hFileMappingObject:DWORD,\</b> <br>
<b>
dwDesiredAccess:DWORD,\</b> <br>
<b>
dwFileOffsetHigh:DWORD,\</b> <br>
<b>
dwFileOffsetLow:DWORD,\</b> <br>
<b>
dwNumberOfBytesToMap:DWORD</b>
<p><b>dwDesiredAccess</b> specifies what operation we want to do to the file.
In our example, we want to read the data only so we use FILE_MAP_READ. <br>
<b>dwFileOffsetHigh</b> and <b>dwFileOffsetLow</b>specify the starting file
offset of the file portion that you want to map into memory. In our case, we
want to read in the whole file so we start mapping from offset 0 onwards. <br>
<b>dwNumberOfBytesToMap</b> specifies the number of bytes to map into memory.
If you want to map the whole file (specified by CreateFileMapping), pass 0 to
MapViewOfFile. <br>
After calling MapViewOfFile, the desired portion is loaded into memory. You'll
be given the pointer to the memory block that contains the data from the file.
<p><b>
invoke GetFileSize,hFileRead,NULL</b>
<p>Find out how large the file is. The file size is returned in eax. If the file
is larger than 4 GB, the high DWORD of the file size is stored in FileSizeHighWord.
Since we don't expect to handle such large file, we can ignore it.
<p><b>
invoke WriteFile,hFileWrite,pMemory,eax,ADDR SizeWritten,NULL</b>
<p>Write the data that is mapped into memory into the output file.
<p><b>
invoke UnmapViewOfFile,pMemory</b>
<p>When we're through with the input file, unmap it from memory.
<p><b>
call CloseMapFile</b> <br>
<b>
invoke CloseHandle,hFileWrite</b>
<p>And close all the files.
<p><b>
invoke SetWindowText,hWnd,ADDR AppName</b>
<p>Restore the original caption text.
<p><b>
invoke EnableMenuItem,hMenu,IDM_OPEN,MF_ENABLED</b> <br>
<b>
invoke EnableMenuItem,hMenu,IDM_SAVE,MF_GRAYED</b>
<p>Enable the Open menu item and gray out the Save As menu item.
<hr size="1">
<div align="center"> This article come from Iczelion's asm page, Welcom to <a href="http://asm.yeah.net">http://asm.yeah.net</a></div>
</body>
</html>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -