?? tut13.html
字號:
<!doctype html public "-//w3c//dtd html 4.0 transitional//en">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<meta name="Author" content="Iczelion">
<meta name="GENERATOR" content="Mozilla/4.51 [en] (Win95; I) [Netscape]">
<title>Iczelion's Win32 Assembly Tutorial 13: Memory Mapped Files</title>
</head>
<body text="#FFFFFF" bgcolor="#000000" link="#FFFF00" vlink="#C0C0C0" alink="#C0FFC0">
<center>
<h1>
<font face="Arial,Helvetica"><font color="#999900">Tutorial 13: Memory
Mapped Files</font></font></h1></center>
<font face="Arial,Helvetica"><font color="#CCCCCC"><font size=-1>I'll show
you what memory mapped files are and how to use them to your advantages.
Using a memory mapped file is quite easy as you'll see in this tutorial.</font></font></font>
<p><font face="Arial,Helvetica"><font color="#CCCCCC"><font size=-1>Download
the example <a href="files/tut13.zip">here</a>.</font></font></font>
<h3>
<font face="Arial,Helvetica"><font color="#CCCCCC"><font size=+0>Theory:</font></font></font></h3>
<font face="Arial,Helvetica"><font color="#CCCCCC"><font size=-1>If you
examine the example in the previous tutorial closely, you'll find that
it has a serious shortcoming: what if the file you want to read is larger
than the allocated memory block? or what if the string you want to search
for is cut off in half at the end of the memory block? The traditional
answer for the first question is that you should repeatedly read in the
data from the file until the end of file is encountered. The answer to
the second question is that you should prepare for the special case at
the end of the memory block. This is called a boundary value problem. It
presents headaches to programmers and causes innumerable bugs.</font></font></font>
<br><font face="Arial,Helvetica"><font color="#CCCCCC"><font size=-1>It
would be nice if we can allocate a very large block of memory, enough to
store the whole file but our program would be a resource hog. File mapping
to the rescue. By using file mapping, you can think of the whole file as
being already loaded into memory and you can use a memory pointer to read
or write data from the file. As easy as that. No need to use memory API
functions and separate File I/O API functions anymore, they are one and
the same under file mapping. File mapping is also used as a means to share
data between processes. Using file mapping in this way, there's no actual
file involved. It's more like a reserved memory block that every process
can *see*. But sharing data between processes is a delicate subject, not
to be treated lightly. You have to implement process and thread synchronization
else your applications will crash in very short order.</font></font></font>
<br><font face="Arial,Helvetica"><font color="#CCCCCC"><font size=-1>We
will not touch the subject of file mapping as a means to create a shared
memory region in this tutorial. We'll concentrate on how to use file mapping
as a means to "map" a file into memory. In fact, the PE loader uses file
mapping to load executable files into memory. It is very convenient since
only the necessary portions can be selectively read from the file on the
disk. Under Win32, you should use file mapping as much as possible.</font></font></font>
<br><font face="Arial,Helvetica"><font color="#CCCCCC"><font size=-1>There
are some limitation to file mapping though. Once you create a memory mapped
file, its size cannot be changed during that session. So file mapping is
great for read-only files or file operations that don't affect the file
size. That doesn't mean that you cannot use file mapping if you want to
increase the file size. You can estimate the new size and create the memory
mapped file based on the new size and the file will grow to that size.
It's just inconvenient, that's all.</font></font></font>
<br><font face="Arial,Helvetica"><font color="#CCCCCC"><font size=-1>Enough
for the explanation. Let's dive into implementation of file mapping. In
order to use file mapping, these steps must be performed:</font></font></font>
<ol>
<li>
<font face="Arial,Helvetica"><font color="#CCCCCC"><font size=-1>call <b>CreateFile</b>
to open the file you want to map.</font></font></font></li>
<li>
<font face="Arial,Helvetica"><font color="#CCCCCC"><font size=-1>call <b>CreateFileMapping</b>
with the file handle returned by CreateFile as one of its parameter. This
function creates a file mapping object from the file opened by CreateFile.</font></font></font></li>
<li>
<font face="Arial,Helvetica"><font color="#CCCCCC"><font size=-1>call <b>MapViewOfFile</b>
to map a selected file region or the whole file to memory. This function
returns a pointer to the first byte of the mapped file region.</font></font></font></li>
<li>
<font face="Arial,Helvetica"><font color="#CCCCCC"><font size=-1>Use the
pointer to read or write the file</font></font></font></li>
<li>
<font face="Arial,Helvetica"><font color="#CCCCCC"><font size=-1>call <b>UnmapViewOfFile</b>
to unmap the file.</font></font></font></li>
<li>
<font face="Arial,Helvetica"><font color="#CCCCCC"><font size=-1>call <b>CloseHandle</b>
with the handle to the mapped file as the parameter to close the mapped
file.</font></font></font></li>
<li>
<font face="Arial,Helvetica"><font color="#CCCCCC"><font size=-1>call <b>CloseHandle</b>
again this time with the file handle returned by CreateFile to close the
actual file.</font></font></font></li>
</ol>
<h3>
<font face="Arial,Helvetica"><font color="#CCCCCC"><font size=+0>Example:</font></font></font></h3>
<font face="Arial,Helvetica"><font color="#CCCCCC"><font size=-1>The program
listed below lets you open a file via an open file dialog box. It opens
the file using file mapping, if it's successful, the window caption is
changed to the name of the opened file. You can save the file in another
name by select File/Save as menuitem. The program will copy the whole content
of the opened file to the new file. Note that you don't have to call GlobalAlloc
to allocate a memory block in this program.</font></font></font>
<p><b><font face="Arial,Helvetica"><font color="#CCCCCC"><font size=-1>.386</font></font></font></b>
<br><b><font face="Arial,Helvetica"><font color="#CCCCCC"><font size=-1>.model
flat,stdcall</font></font></font></b>
<br><b><font face="Arial,Helvetica"><font color="#CCCCCC"><font size=-1>WinMain
proto :DWORD,:DWORD,:DWORD,:DWORD</font></font></font></b>
<br><b><font face="Arial,Helvetica"><font color="#CCCCCC"><font size=-1>include
\masm32\include\windows.inc</font></font></font></b>
<br><b><font face="Arial,Helvetica"><font color="#CCCCCC"><font size=-1>include
\masm32\include\user32.inc</font></font></font></b>
<br><b><font face="Arial,Helvetica"><font color="#CCCCCC"><font size=-1>include
\masm32\include\kernel32.inc</font></font></font></b>
<br><b><font face="Arial,Helvetica"><font color="#CCCCCC"><font size=-1>include
\masm32\include\comdlg32.inc</font></font></font></b>
<br><b><font face="Arial,Helvetica"><font color="#CCCCCC"><font size=-1>includelib
\masm32\lib\user32.lib</font></font></font></b>
<br><b><font face="Arial,Helvetica"><font color="#CCCCCC"><font size=-1>includelib
\masm32\lib\kernel32.lib</font></font></font></b>
<br><b><font face="Arial,Helvetica"><font color="#CCCCCC"><font size=-1>includelib
\masm32\lib\comdlg32.lib</font></font></font></b>
<p><b><font face="Arial,Helvetica"><font color="#CCCCCC"><font size=-1>.const</font></font></font></b>
<br><b><font face="Arial,Helvetica"><font color="#CCCCCC"><font size=-1>IDM_OPEN
equ 1</font></font></font></b>
<br><b><font face="Arial,Helvetica"><font color="#CCCCCC"><font size=-1>IDM_SAVE
equ 2</font></font></font></b>
<br><b><font face="Arial,Helvetica"><font color="#CCCCCC"><font size=-1>IDM_EXIT
equ 3</font></font></font></b>
<br><b><font face="Arial,Helvetica"><font color="#CCCCCC"><font size=-1>MAXSIZE
equ 260</font></font></font></b>
<p><b><font face="Arial,Helvetica"><font color="#CCCCCC"><font size=-1>.data</font></font></font></b>
<br><b><font face="Arial,Helvetica"><font color="#CCCCCC"><font size=-1>ClassName
db "Win32ASMFileMappingClass",0</font></font></font></b>
<br><b><font face="Arial,Helvetica"><font color="#CCCCCC"><font size=-1>AppName
db "Win32 ASM File Mapping Example",0</font></font></font></b>
<br><b><font face="Arial,Helvetica"><font color="#CCCCCC"><font size=-1>MenuName
db "FirstMenu",0</font></font></font></b>
<br><b><font face="Arial,Helvetica"><font color="#CCCCCC"><font size=-1>ofn
OPENFILENAME <></font></font></font></b>
<br><b><font face="Arial,Helvetica"><font color="#CCCCCC"><font size=-1>FilterString
db "All Files",0,"*.*",0</font></font></font></b>
<br><b><font face="Arial,Helvetica"><font color="#CCCCCC"><font size=-1>
db "Text Files",0,"*.txt",0,0</font></font></font></b>
<br><b><font face="Arial,Helvetica"><font color="#CCCCCC"><font size=-1>buffer
db MAXSIZE dup(0)</font></font></font></b>
<br><b><font face="Arial,Helvetica"><font color="#CCCCCC"><font size=-1>hMapFile
HANDLE 0
; Handle to the memory mapped file, must be</font></font></font></b>
<br><b><font face="Arial,Helvetica"><font color="#CCCCCC"><font size=-1>
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -