⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 tut12.html

📁 WINDOWS程序员使用指南--汇编基础
💻 HTML
📖 第 1 页 / 共 5 页
字号:

<li>
<font face="Arial,Helvetica"><font color="#CCCCCC"><font size=-1><b>CREATE_ALWAYS</b>
Creates a new file. The function overwrites the file if it exists.</font></font></font></li>

<li>
<font face="Arial,Helvetica"><font color="#CCCCCC"><font size=-1><b>OPEN_EXISTING</b>
Opens the file. The function fails if the file does not exist.</font></font></font></li>

<li>
<font face="Arial,Helvetica"><font color="#CCCCCC"><font size=-1><b>OPEN_ALWAYS</b>
Opens the file, if it exists. If the file does not exist, the function
creates the file as if dwCreationDistribution were CREATE_NEW.</font></font></font></li>

<li>
<font face="Arial,Helvetica"><font color="#CCCCCC"><font size=-1><b>TRUNCATE_EXISTING</b>
Opens the file. Once opened, the file is truncated so that its size is
zero bytes. The calling process must open the file with at least GENERIC_WRITE
access. The function fails if the file does not exist.</font></font></font></li>
</ul>
<font face="Arial,Helvetica"><font color="#CCCCCC"><font size=-1>dwFlagsAndAttributes
specifies the file attributes</font></font></font>
<ul>
<li>
<font face="Arial,Helvetica"><font color="#CCCCCC"><font size=-1><b>FILE_ATTRIBUTE_ARCHIVE</b>
The file is an archive file. Applications use this attribute to mark files
for backup or removal.</font></font></font></li>

<li>
<font face="Arial,Helvetica"><font color="#CCCCCC"><font size=-1><b>FILE_ATTRIBUTE_COMPRESSED</b>
The file or directory is compressed. For a file, this means that all of
the data in the file is compressed. For a directory, this means that compression
is the default for newly created files and subdirectories.</font></font></font></li>

<li>
<font face="Arial,Helvetica"><font color="#CCCCCC"><font size=-1><b>FILE_ATTRIBUTE_NORMAL</b>
The file has no other attributes set. This attribute is valid only if used
alone.</font></font></font></li>

<li>
<font face="Arial,Helvetica"><font color="#CCCCCC"><font size=-1><b>FILE_ATTRIBUTE_HIDDEN</b>
The file is hidden. It is not to be included in an ordinary directory listing.</font></font></font></li>

<li>
<font face="Arial,Helvetica"><font color="#CCCCCC"><font size=-1><b>FILE_ATTRIBUTE_READONLY</b>
The file is read only. Applications can read the file but cannot write
to it or delete it.</font></font></font></li>

<li>
<font face="Arial,Helvetica"><font color="#CCCCCC"><font size=-1><b>FILE_ATTRIBUTE_SYSTEM
</b>The file is part of or is used exclusively by the operating system.</font></font></font></li>
</ul>
<b><font face="Arial,Helvetica"><font color="#CCCCCC"><font size=-1>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
invoke GlobalAlloc,GMEM_MOVEABLE or GMEM_ZEROINIT,MEMSIZE</font></font></font></b>
<br><b><font face="Arial,Helvetica"><font color="#CCCCCC"><font size=-1>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
mov&nbsp; hMemory,eax</font></font></font></b>
<br><b><font face="Arial,Helvetica"><font color="#CCCCCC"><font size=-1>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
invoke GlobalLock,hMemory</font></font></font></b>
<br><b><font face="Arial,Helvetica"><font color="#CCCCCC"><font size=-1>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
mov&nbsp; pMemory,eax</font></font></font></b><font face="Arial,Helvetica"><font color="#CCCCCC"><font size=-1></font></font></font>
<p><font face="Arial,Helvetica"><font color="#CCCCCC"><font size=-1>When
the file is opened, we allocate a block of memory for use by ReadFile and
WriteFile functions. We specify GMEM_MOVEABLE flag&nbsp; to let Windows
move the memory block around to consolidate memory. GMEM_ZEROINIT flag
tells GlobalAlloc to fill the newly allocated memory block with zeroes.</font></font></font>
<br><font face="Arial,Helvetica"><font color="#CCCCCC"><font size=-1>When
GlobalAlloc returns successfully, eax contains the handle to the allocated
memory block. We pass this handle to GlobalLock function which returns
a pointer to the memory block.</font></font></font><font face="Arial,Helvetica"><font color="#CCCCCC"><font size=-1></font></font></font>
<p><b><font face="Arial,Helvetica"><font color="#CCCCCC"><font size=-1>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
invoke ReadFile,hFile,pMemory,MEMSIZE-1,ADDR SizeReadWrite,NULL</font></font></font></b>
<br><b><font face="Arial,Helvetica"><font color="#CCCCCC"><font size=-1>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
invoke SendMessage,hwndEdit,WM_SETTEXT,NULL,pMemory</font></font></font></b><font face="Arial,Helvetica"><font color="#CCCCCC"><font size=-1></font></font></font>
<p><font face="Arial,Helvetica"><font color="#CCCCCC"><font size=-1>When
the memory block is ready for use, we call ReadFile function to read data
from the file. When a file is first opened or created, the file pointer
is at offset 0. So in this case, we start reading from the first byte in
the file onwards. The first parameter of ReadFile is the handle of the
file to read, the second is the pointer to the memory block to hold the
data, next is the number of bytes to read from the file, the fourth param
is the address of the variable of DWORD size that will be filled with the
number of bytes actually read from the file.</font></font></font>
<br><font face="Arial,Helvetica"><font color="#CCCCCC"><font size=-1>After
we fill the memory block with the data, we put the data into the edit control
by sending WM_SETTEXT message to the edit control with lParam containing
the pointer to the memory block. After this call, the edit control shows
the data in its client area.</font></font></font><font face="Arial,Helvetica"><font color="#CCCCCC"><font size=-1></font></font></font>
<p><b><font face="Arial,Helvetica"><font color="#CCCCCC"><font size=-1>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
invoke CloseHandle,hFile</font></font></font></b>
<br><b><font face="Arial,Helvetica"><font color="#CCCCCC"><font size=-1>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
invoke GlobalUnlock,pMemory</font></font></font></b>
<br><b><font face="Arial,Helvetica"><font color="#CCCCCC"><font size=-1>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
invoke GlobalFree,hMemory</font></font></font></b>
<br><b><font face="Arial,Helvetica"><font color="#CCCCCC"><font size=-1>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
.endif</font></font></font></b><font face="Arial,Helvetica"><font color="#CCCCCC"><font size=-1></font></font></font>
<p><font face="Arial,Helvetica"><font color="#CCCCCC"><font size=-1>At
this point, we have no need to keep the file open any longer since our
purpose is to write the modified data from the edit control to another
file, not the original file. So we close the file by calling CloseHandle
with the file handle as its parameter. Next we unlock the memory block
and free it. Actually you don't have to free the memory at this point,
you can reuse the memory block during the save operation later. But for
demonstration purpose , I choose to free it here.</font></font></font><font face="Arial,Helvetica"><font color="#CCCCCC"><font size=-1></font></font></font>
<p><b><font face="Arial,Helvetica"><font color="#CCCCCC"><font size=-1>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
invoke SetFocus,hwndEdit</font></font></font></b><font face="Arial,Helvetica"><font color="#CCCCCC"><font size=-1></font></font></font>
<p><font face="Arial,Helvetica"><font color="#CCCCCC"><font size=-1>When
the open file dialog box is displayed on the screen, the input focus shifts
to it. So after the open file dialog is closed, we must move the input
focus back to the edit control.</font></font></font>
<br><font face="Arial,Helvetica"><font color="#CCCCCC"><font size=-1>This
end the read operation on the file. At this point, the user can edit the
content of the edit control.And when he wants to save the data to another
file, he must select File/Save as menuitem which displays a save as dialog
box. The creation of the save as dialog box is not much different from
the open file dialog box. In fact, they differ in only the name of the
functions, GetOpenFileName and GetSaveFileName. You can reuse most members
of the ofn structure too except the Flags member.</font></font></font><font face="Arial,Helvetica"><font color="#CCCCCC"><font size=-1></font></font></font>
<p><b><font face="Arial,Helvetica"><font color="#CCCCCC"><font size=-1>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
mov ofn.Flags,OFN_LONGNAMES or\</font></font></font></b>
<br><b><font face="Arial,Helvetica"><font color="#CCCCCC"><font size=-1>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
OFN_EXPLORER or OFN_HIDEREADONLY</font></font></font></b><font face="Arial,Helvetica"><font color="#CCCCCC"><font size=-1></font></font></font>
<p><font face="Arial,Helvetica"><font color="#CCCCCC"><font size=-1>In
our case, we want to create a new file, so OFN_FILEMUSTEXIST and OFN_PATHMUSTEXIST
must be left out else the dialog box will not let us create a file that
doesn't already exist.</font></font></font>
<br><font face="Arial,Helvetica"><font color="#CCCCCC"><font size=-1>The
dwCreationDistribution parameter of the CreateFile function must be changed
to <b>CREATE_NEW</b> since we want to create a new file.</font></font></font>
<br><font face="Arial,Helvetica"><font color="#CCCCCC"><font size=-1>The
remaining code is identical to those in the open file section except the
following:</font></font></font><font face="Arial,Helvetica"><font color="#CCCCCC"><font size=-1></font></font></font>
<p><b><font face="Arial,Helvetica"><font color="#CCCCCC"><font size=-1>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
invoke SendMessage,hwndEdit,WM_GETTEXT,MEMSIZE-1,pMemory</font></font></font></b>
<br><b><font face="Arial,Helvetica"><font color="#CCCCCC"><font size=-1>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
invoke WriteFile,hFile,pMemory,eax,ADDR SizeReadWrite,NULL</font></font></font></b><font face="Arial,Helvetica"><font color="#CCCCCC"><font size=-1></font></font></font>
<p><font face="Arial,Helvetica"><font color="#CCCCCC"><font size=-1>We
send WM_GETTEXT message to the edit control to copy the data from it to
the memory block we provide, the return value in eax is the length of the
data inside the buffer. After the data is in the memory block, we write
them to the new file.</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 + -