?? tut13.html
字號:
<br><b><font face="Arial,Helvetica"><font color="#CCCCCC"><font size=-1>
lpName:DWORD</font></font></font></b>
<p><font face="Arial,Helvetica"><font color="#CCCCCC"><font size=-1>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.</font></font></font>
<br><font face="Arial,Helvetica"><font color="#CCCCCC"><font size=-1>You
can use NULL in lpFileMappingAttributes parameter to have Windows creates
a memory mapped file with default security attributes.</font></font></font>
<br><font face="Arial,Helvetica"><font color="#CCCCCC"><font size=-1>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.</font></font></font>
<br><font face="Arial,Helvetica"><font color="#CCCCCC"><font size=-1>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.</font></font></font>
<p><b><font face="Arial,Helvetica"><font color="#CCCCCC"><font size=-1>
mov eax,OFFSET buffer</font></font></font></b>
<br><b><font face="Arial,Helvetica"><font color="#CCCCCC"><font size=-1>
movzx edx,ofn.nFileOffset</font></font></font></b>
<br><b><font face="Arial,Helvetica"><font color="#CCCCCC"><font size=-1>
add eax,edx</font></font></font></b>
<br><b><font face="Arial,Helvetica"><font color="#CCCCCC"><font size=-1>
invoke SetWindowText,hWnd,eax</font></font></font></b>
<p><font face="Arial,Helvetica"><font color="#CCCCCC"><font size=-1>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.</font></font></font>
<p><b><font face="Arial,Helvetica"><font color="#CCCCCC"><font size=-1>
invoke EnableMenuItem,hMenu,IDM_OPEN,MF_GRAYED</font></font></font></b>
<br><b><font face="Arial,Helvetica"><font color="#CCCCCC"><font size=-1>
invoke EnableMenuItem,hMenu,IDM_SAVE,MF_ENABLED</font></font></font></b>
<p><font face="Arial,Helvetica"><font color="#CCCCCC"><font size=-1>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.</font></font></font>
<br><font face="Arial,Helvetica"><font color="#CCCCCC"><font size=-1>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:</font></font></font>
<p><b><font face="Arial,Helvetica"><font color="#CCCCCC"><font size=-1>
.ELSEIF uMsg==WM_DESTROY</font></font></font></b>
<br><b><font face="Arial,Helvetica"><font color="#CCCCCC"><font size=-1>
.if hMapFile!=0</font></font></font></b>
<br><b><font face="Arial,Helvetica"><font color="#CCCCCC"><font size=-1>
call CloseMapFile</font></font></font></b>
<br><b><font face="Arial,Helvetica"><font color="#CCCCCC"><font size=-1>
.endif</font></font></font></b>
<br><b><font face="Arial,Helvetica"><font color="#CCCCCC"><font size=-1>
invoke PostQuitMessage,NULL</font></font></font></b>
<p><font face="Arial,Helvetica"><font color="#CCCCCC"><font size=-1>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:</font></font></font>
<p><b><font face="Arial,Helvetica"><font color="#CCCCCC"><font size=-1>CloseMapFile
PROC</font></font></font></b>
<br><b><font face="Arial,Helvetica"><font color="#CCCCCC"><font size=-1>
invoke CloseHandle,hMapFile</font></font></font></b>
<br><b><font face="Arial,Helvetica"><font color="#CCCCCC"><font size=-1>
mov hMapFile,0</font></font></font></b>
<br><b><font face="Arial,Helvetica"><font color="#CCCCCC"><font size=-1>
invoke CloseHandle,hFileRead</font></font></font></b>
<br><b><font face="Arial,Helvetica"><font color="#CCCCCC"><font size=-1>
ret</font></font></font></b>
<br><b><font face="Arial,Helvetica"><font color="#CCCCCC"><font size=-1>CloseMapFile
endp</font></font></font></b>
<p><font face="Arial,Helvetica"><font color="#CCCCCC"><font size=-1>CloseMapFile
closes the memory mapped file and the actual file so that there 'll be
no resource leakage when our program exits to Windows.</font></font></font>
<br><font face="Arial,Helvetica"><font color="#CCCCCC"><font size=-1>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.</font></font></font>
<p><b><font face="Arial,Helvetica"><font color="#CCCCCC"><font size=-1>
invoke MapViewOfFile,hMapFile,FILE_MAP_READ,0,0,0</font></font></font></b>
<br><b><font face="Arial,Helvetica"><font color="#CCCCCC"><font size=-1>
mov pMemory,eax</font></font></font></b>
<p><font face="Arial,Helvetica"><font color="#CCCCCC"><font size=-1>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:</font></font></font>
<p><b><font face="Arial,Helvetica"><font color="#CCCCCC"><font size=-1>MapViewOfFile
proto hFileMappingObject:DWORD,\</font></font></font></b>
<br><b><font face="Arial,Helvetica"><font color="#CCCCCC"><font size=-1>
dwDesiredAccess:DWORD,\</font></font></font></b>
<br><b><font face="Arial,Helvetica"><font color="#CCCCCC"><font size=-1>
dwFileOffsetHigh:DWORD,\</font></font></font></b>
<br><b><font face="Arial,Helvetica"><font color="#CCCCCC"><font size=-1>
dwFileOffsetLow:DWORD,\</font></font></font></b>
<br><b><font face="Arial,Helvetica"><font color="#CCCCCC"><font size=-1>
dwNumberOfBytesToMap:DWORD</font></font></font></b>
<p><font face="Arial,Helvetica"><font color="#CCCCCC"><font size=-1><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.</font></font></font>
<br><font face="Arial,Helvetica"><font color="#CCCCCC"><font size=-1><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.</font></font></font>
<br><font face="Arial,Helvetica"><font color="#CCCCCC"><font size=-1><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.</font></font></font>
<br><font face="Arial,Helvetica"><font color="#CCCCCC"><font size=-1>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.</font></font></font>
<p><b><font face="Arial,Helvetica"><font color="#CCCCCC"><font size=-1>
invoke GetFileSize,hFileRead,NULL</font></font></font></b>
<p><font face="Arial,Helvetica"><font color="#CCCCCC"><font size=-1>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.</font></font></font>
<p><b><font face="Arial,Helvetica"><font color="#CCCCCC"><font size=-1>
invoke WriteFile,hFileWrite,pMemory,eax,ADDR SizeWritten,NULL</font></font></font></b>
<p><font face="Arial,Helvetica"><font color="#CCCCCC"><font size=-1>Write
the data that is mapped into memory into the output file.</font></font></font>
<p><b><font face="Arial,Helvetica"><font color="#CCCCCC"><font size=-1>
invoke UnmapViewOfFile,pMemory</font></font></font></b>
<p><font face="Arial,Helvetica"><font color="#CCCCCC"><font size=-1>When
we're through with the input file, unmap it from memory.</font></font></font>
<p><b><font face="Arial,Helvetica"><font color="#CCCCCC"><font size=-1>
call CloseMapFile</font></font></font></b>
<br><b><font face="Arial,Helvetica"><font color="#CCCCCC"><font size=-1>
invoke CloseHandle,hFileWrite</font></font></font></b>
<p><font face="Arial,Helvetica"><font color="#CCCCCC"><font size=-1>And
close all the files.</font></font></font>
<p><b><font face="Arial,Helvetica"><font color="#CCCCCC"><font size=-1>
invoke SetWindowText,hWnd,ADDR AppName</font></font></font></b>
<p><font face="Arial,Helvetica"><font color="#CCCCCC"><font size=-1>Restore
the original caption text.</font></font></font>
<p><b><font face="Arial,Helvetica"><font color="#CCCCCC"><font size=-1>
invoke EnableMenuItem,hMenu,IDM_OPEN,MF_ENABLED</font></font></font></b>
<br><b><font face="Arial,Helvetica"><font color="#CCCCCC"><font size=-1>
invoke EnableMenuItem,hMenu,IDM_SAVE,MF_GRAYED</font></font></font></b>
<p><font face="Arial,Helvetica"><font color="#CCCCCC"><font size=-1>Enable
the Open menu item and gray out the Save As menu item.</font></font></font>
<br>
<hr WIDTH="100%">
<center><b><font face="Arial,Helvetica"><font color="#CCCCCC"><font size=-1>[<a href="http://win32asm.cjb.net">Iczelion's
Win32 Assembly HomePage</a>]</font></font></font></b></center>
</body>
</html>
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -