📄 tut17.html
字号:
function name>. This function takes three parameters, only the first two
of which are important.</font></font>
<br><font face="Arial,Helvetica"><font size=-1><b><font color="#FFCC33">hInstDLL</font></b>
is the module handle of the DLL. It's not the same as the instance handle
of the process. You should keep this value if you need to use it later.
You can't obtain it again easily.</font></font>
<br><font face="Arial,Helvetica"><font size=-1><b><font color="#FFCC33">reason</font></b>
can be one of the four values:</font></font>
<ul>
<li>
<font face="Arial,Helvetica"><font size=-1><b><font color="#FFCCCC">DLL_PROCESS_ATTACH</font></b>
The DLL receives this value when it is first injected into the process
address space. You can use this opportunity to do initialization.</font></font></li>
<li>
<font face="Arial,Helvetica"><font size=-1><b><font color="#FFCCCC">DLL_PROCESS_DETACH</font></b>
The DLL receives this value when it is being unloaded from the process
address space. You can use this opportunity to do some cleanup such as
deallocate memory and so on.</font></font></li>
<li>
<font face="Arial,Helvetica"><font size=-1><b><font color="#FFCCCC">DLL_THREAD_ATTACH</font></b>
The DLL receives this value when the process creates a new thread.</font></font></li>
<li>
<font face="Arial,Helvetica"><font size=-1><b><font color="#FFCCCC">DLL_THREAD_DETACH</font></b>
The DLL receives this value when a thread in the process is destroyed.</font></font></li>
</ul>
<font face="Arial,Helvetica"><font size=-1>You return TRUE in eax if you
want the DLL to go on running. If you return FALSE, the DLL will not be
loaded. For example, if your initialization code must allocate some memory
and it cannot do that successfully, the entrypoint function should return
FALSE to indicate that the DLL cannot run.</font></font>
<br><font face="Arial,Helvetica"><font size=-1>You can put your functions
in the DLL following the entrypoint function or before it. But if you want
them to be callable from other programs, you must put their names in the
export list in the module definition file (.def).</font></font>
<br><font face="Arial,Helvetica"><font size=-1>A DLL needs a module definition
file in its developmental stage. We will take a look at it now.</font></font><font face="Arial,Helvetica"><font size=-1></font></font>
<p><font face="Arial,Helvetica"><font size=-1><b><font color="#FFCCCC">LIBRARY</font></b>
DLLSkeleton</font></font>
<br><font face="Arial,Helvetica"><font size=-1><b><font color="#FFCCCC">EXPORTS</font></b>
TestFunction</font></font><font face="Arial,Helvetica"><font size=-1></font></font>
<p><font face="Arial,Helvetica"><font size=-1>Normally you must have the
first line.The <b><font color="#FFCCCC">LIBRARY</font></b> statement defines
the internal module name of the DLL. You should match it with the filename
of the DLL.</font></font>
<br><font face="Arial,Helvetica"><font size=-1>The <b><font color="#FFCCCC">EXPORTS</font></b>
statement tells the linker which functions in the DLL are exported, that
is, callable from other programs. In the example, we want other modules
to be able to call TestFunction, so we put its name in the <b><font color="#FFCCCC">EXPORTS</font></b>
statement.</font></font>
<br><font face="Arial,Helvetica"><font size=-1>Another change is in the
linker switch. You must put <b><font color="#FFCCCC">/DLL</font></b> switch
and <b><font color="#FFCCCC">/DEF:<your def filename> </font></b>in
your linker switches like this:</font></font><font face="Arial,Helvetica"><font size=-1></font></font>
<p><b><font face="Arial,Helvetica"><font size=-1><font color="#FFFF00">link
</font><font color="#FFCCCC">/DLL</font><font color="#FFFF00"> /SUBSYSTEM:WINDOWS
</font><font color="#FFCCCC">/DEF:DLLSkeleton.def</font><font color="#FFFF00">
/LIBPATH:c:\masm32\lib DLLSkeleton.obj</font></font></font></b><font face="Arial,Helvetica"><font size=-1></font></font>
<p><font face="Arial,Helvetica"><font color="#FFFFFF"><font size=-1>The
assembler switches are the same, namely /c /coff /Cp. So after you link
the object file, you will get .dll and .lib. The .lib is the import library
which you can use to link to other programs that use the functions in the
DLL.</font></font></font>
<br><font face="Arial,Helvetica"><font color="#FFFFFF"><font size=-1>Next
I'll show you how to use LoadLibrary to load a DLL.</font></font></font><font face="Arial,Helvetica"><font size=-1></font></font>
<p><font face="Arial,Helvetica"><font color="#FFFFFF"><font size=-1>;---------------------------------------------------------------------------------------------</font></font></font>
<br><font face="Arial,Helvetica"><font color="#FFFFFF"><font size=-1>;
UseDLL.asm</font></font></font>
<br><font face="Arial,Helvetica"><font color="#FFFFFF"><font size=-1>;----------------------------------------------------------------------------------------------</font></font></font>
<br><b><font face="Arial,Helvetica"><font color="#FFFFFF"><font size=-1>.386</font></font></font></b>
<br><b><font face="Arial,Helvetica"><font color="#FFFFFF"><font size=-1>.model
flat,stdcall</font></font></font></b>
<br><b><font face="Arial,Helvetica"><font color="#FFFFFF"><font size=-1>option
casemap:none</font></font></font></b>
<br><b><font face="Arial,Helvetica"><font color="#FFFFFF"><font size=-1>include
\masm32\include\windows.inc</font></font></font></b>
<br><b><font face="Arial,Helvetica"><font color="#FFFFFF"><font size=-1>include
\masm32\include\user32.inc</font></font></font></b>
<br><b><font face="Arial,Helvetica"><font color="#FFFFFF"><font size=-1>include
\masm32\include\kernel32.inc</font></font></font></b>
<br><b><font face="Arial,Helvetica"><font color="#FFFFFF"><font size=-1>includelib
\masm32\lib\kernel32.lib</font></font></font></b>
<br><b><font face="Arial,Helvetica"><font color="#FFFFFF"><font size=-1>includelib
\masm32\lib\user32.lib</font></font></font></b><font face="Arial,Helvetica"><font size=-1></font></font>
<p><b><font face="Arial,Helvetica"><font color="#FFFFFF"><font size=-1>.data</font></font></font></b>
<br><b><font face="Arial,Helvetica"><font color="#FFFFFF"><font size=-1>LibName
db "DLLSkeleton.dll",0</font></font></font></b>
<br><b><font face="Arial,Helvetica"><font color="#FFFFFF"><font size=-1>FunctionName
db "TestHello",0</font></font></font></b>
<br><b><font face="Arial,Helvetica"><font color="#FFFFFF"><font size=-1>DllNotFound
db "Cannot load library",0</font></font></font></b>
<br><b><font face="Arial,Helvetica"><font color="#FFFFFF"><font size=-1>AppName
db "Load Library",0</font></font></font></b>
<br><b><font face="Arial,Helvetica"><font color="#FFFFFF"><font size=-1>FunctionNotFound
db "TestHello function not found",0</font></font></font></b><font face="Arial,Helvetica"><font size=-1></font></font>
<p><b><font face="Arial,Helvetica"><font color="#FFFFFF"><font size=-1>.data?</font></font></font></b>
<br><b><font face="Arial,Helvetica"><font color="#FFFFFF"><font size=-1>hLib
dd ?
; the handle of the library (DLL)</font></font></font></b>
<br><b><font face="Arial,Helvetica"><font color="#FFFFFF"><font size=-1>TestHelloAddr
dd ?
; the address of the TestHello function</font></font></font></b><font face="Arial,Helvetica"><font size=-1></font></font>
<p><b><font face="Arial,Helvetica"><font color="#FFFFFF"><font size=-1>.code</font></font></font></b>
<br><b><font face="Arial,Helvetica"><font color="#FFFFFF"><font size=-1>start:</font></font></font></b>
<br><b><font face="Arial,Helvetica"><font color="#FFFFFF"><font size=-1>
invoke LoadLibrary,addr LibName</font></font></font></b>
<br><b><font face="Arial,Helvetica"><font color="#FFCCCC"><font size=-1>;---------------------------------------------------------------------------------------------------------</font></font></font></b>
<br><b><font face="Arial,Helvetica"><font color="#FFCCCC"><font size=-1>;
Call LoadLibrary with the name of the desired DLL. If the call is successful</font></font></font></b>
<br><b><font face="Arial,Helvetica"><font color="#FFCCCC"><font size=-1>;
it will return the handle to the library (DLL). If not, it will return
NULL</font></font></font></b>
<br><b><font face="Arial,Helvetica"><font color="#FFCCCC"><font size=-1>;
You can pass the library handle to GetProcAddress or any function that
requires</font></font></font></b>
<br><b><font face="Arial,Helvetica"><font color="#FFCCCC"><font size=-1>;
a library handle as a parameter.</font></font></font></b>
<br><b><font face="Arial,Helvetica"><font color="#FFCCCC"><font size=-1>;------------------------------------------------------------------------------------------------------------</font></font></font></b>
<br><b><font face="Arial,Helvetica"><font color="#FFFFFF"><font size=-1>
.if eax==NULL</font></font></font></b>
<br><b><font face="Arial,Helvetica"><font color="#FFFFFF"><font size=-1>
invoke MessageBox,NULL,addr DllNotFound,addr AppName,MB_OK</font></font></font></b>
<br><b><font face="Arial,Helvetica"><font color="#FFFFFF"><font size=-1>
.else</font></font></font></b>
<br><b><font face="Arial,Helvetica"><font color="#FFFFFF"><font size=-1>
mov hLib,eax</font></font></font></b>
<br><b><font face="Arial,Helvetica"><font color="#FFFFFF"><font size=-1>
invoke GetProcAddress,hLib,addr FunctionName</font></font></font></b>
<br><b><font face="Arial,Helvetica"><font color="#FFCCCC"><font size=-1>;-------------------------------------------------------------------------------------------------------------</font></font></font></b>
<br><b><font face="Arial,Helvetica"><font color="#FFCCCC"><font size=-1>;
When you get the library handle, you pass it to GetProcAddress with the
address</font></font></font></b>
<br><b><font face="Arial,Helvetica"><font color="#FFCCCC"><font size=-1>;
of the name of the function in that DLL you want to call. It returns the
address</font></font></font></b>
<br><b><font face="Arial,Helvetica"><font color="#FFCCCC"><font size=-1>;
of the function if successful. Otherwise, it returns NULL</font></font></font></b>
<br><b><font face="Arial,Helvetica"><font color="#FFCCCC"><font size=-1>;
Addresses of functions don't change unless you unload and reload the library.</font></font></font></b>
<br><b><font face="Arial,Helvetica"><font color="#FFCCCC"><font size=-1>;
So you can put them in global variables for future use.</font></font></font></b>
<br><b><font face="Arial,Helvetica"><font color="#FFCCCC"><font size=-1>;-------------------------------------------------------------------------------------------------------------</font></font></font></b>
<br><b><font face="Arial,Helvetica"><font color="#FFFFFF"><font size=-1>
.if eax==NULL</font></font></font></b>
<br><b><font face="Arial,Helvetica"><font color="#FFFFFF"><font size=-1>
invoke MessageBox,NULL,addr FunctionNotFound,addr AppName,MB_OK</font></font></font></b>
<br><b><font face="Arial,Helvetica"><font color="#FFFFFF"><font size=-1>
.else</font></font></font></b>
<br><b><font face="Arial,Helvetica"><font color="#FFFFFF"><font size=-1>
mov TestHelloAddr,eax</font></font></font></b>
<br><b><font face="Arial,Helvetica"><font color="#FFFFFF"><font size=-1>
call [TestHelloAddr]</font></font></font></b>
<br><b><font face="Arial,Helvetica"><font color="#FFCCCC"><font size=-1>;-------------------------------------------------------------------------------------------------------------</font></font></font></b>
<br><b><font face="Arial,Helvetica"><font color="#FFCCCC"><font size=-1>;
Next, you can call the function with a simple call with the variable containing</font></font></font></b>
<br><b><font face="Arial,Helvetica"><font color="#FFCCCC"><font size=-1>;
the address of the function as the operand.</font></font></font></b>
<br><b><font face="Arial,Helvetica"><font color="#FFCCCC"><font size=-1>;-------------------------------------------------------------------------------------------------------------</font></font></font></b>
<br><b><font face="Arial,Helvetica"><font color="#FFFFFF"><font size=-1>
.endif</font></font></font></b>
<br><b><font face="Arial,Helvetica"><font color="#FFFFFF"><font size=-1>
invoke FreeLibrary,hLib</font></font></font></b>
<br><b><font face="Arial,Helvetica"><font color="#FFCCCC"><font size=-1>;-------------------------------------------------------------------------------------------------------------</font></font></font></b>
<br><b><font face="Arial,Helvetica"><font color="#FFCCCC"><font size=-1>;
When you don't need the library anymore, unload it with FreeLibrary.</font></font></font></b>
<br><b><font face="Arial,Helvetica"><font color="#FFCCCC"><font size=-1>;-------------------------------------------------------------------------------------------------------------</font></font></font></b>
<br><b><font face="Arial,Helvetica"><font color="#FFFFFF"><font size=-1>
.endif</font></font></font></b>
<br><b><font face="Arial,Helvetica"><font color="#FFFFFF"><font size=-1>
invoke ExitProcess,NULL</font></font></font></b>
<br><b><font face="Arial,Helvetica"><font color="#FFFFFF"><font size=-1>end
start</font></font></font></b><font face="Arial,Helvetica"><font size=-1></font></font>
<p><font face="Arial,Helvetica"><font color="#FFFFFF"><font size=-1>So
you can see that using LoadLibrary is a little more involved but it's also
more flexible.</font></font></font>
<br>
<hr WIDTH="100%">
<center><b><font face="Arial,Helvetica"><font size=-1>[<a href="http://win32asm.cjb.net">Iczelion's
Win32 Assembly Homepage</a>]</font></font></b></center>
</body>
</html>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -