DLLs And Exes

Running the Executable Module

When an executable file is invoked, the operating system loader creates the virtual address space for the process.

Then the loader maps the executable module into the process' address space. The loader examines the executable's

import section and attempts to locate and map any required DLLs into the process' address space.

Because the import section contains just a DLL name without its pathname, the loader must search the user's disk

drives for the DLL.

 

Here is the loader's search order:

   1. The directory containing the executable image file

  
   2. The Windows system directory returned by GetWindowsDirectory

  

   3. The 16-bit system directory—that is, the System subfolder under the Windows directory

  

   4. The Windows directory returned by GetSystemDirectory

  

   5. The process' current directory
   

   6. The directories listed in the PATH environment variable



Notice that the application current directory is searched after the Windows directories. This change occurred in

 

Windows XP SP2 to avoid having fake system DLLs be found and loaded from the application current directory instead

of from their official location in the Windows directories. The MSDN online help mentions how a DWORD value under

HKEY_LOCAL_MACHINE/SYSTEM/CurrentControlSet/Control/Session Manager could change this search order, but you

 

should never set it if you don't want to allow malware to compromise your machine. Be aware that other things can affect

 

how the loader searches for a DLL. (See Chapter 20 for more information.)

As the DLL modules are mapped into the process' address space, the loader checks each DLL's import section as well.

If an import section exists (and usually it does), the loader continues to map the additional required DLL modules

into the process' address space. The loader keeps track of the DLL modules that it is loading and maps a module

only once even if multiple modules require that module.



If the loader cannot locate a required DLL module, An error message box will come up.


After all the DLL modules have been located and mapped into the process' address space, the loader fixes up all

references to imported symbols. To do this, it again looks in each module's import section. For each symbol listed,

the loader examines the designated DLL's export section to see if the symbol exists. If the symbol does not exist

(which is very rare), the loader displays a error message box .

 


If the symbol does exist, the loader retrieves the RVA of the symbol and adds the virtual address of where the DLL

module is loaded (the location of the symbol in the process' address space). It then saves this virtual address in

the executable module's import section. Now, when the code references an imported symbol, it looks in the calling

module's import section and grabs the address of the imported symbol, and it can thus successfully access the

imported variable, function, or C++ class member function. When the dynamic link is complete, the process'

primary thread begins executing, and the application is finally running!



Naturally, it takes the loader quite a bit of time to load all these DLL modules and fix up every module's impor

section with the proper addresses of all the imported symbols. Because all this work is done when the process

initializes, there is no run-time performance hit for the application. For many applications, however, a slow

initialization is unacceptable. To help improve your application's load time, you should rebase and bind your

executable and DLL modules. Few developers know how to do this, which is unfortunate because these techniques

 

are extremely important. The system would run much better if every company performed these techniques. In fact, I

believe that operating systems should ship with a utility that automatically performs these operations.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在Python中,DLL(Dynamic Link Library)是一种动态链接库,可以包含可执行代码和数据,可以被程序动态加载和调用。要在Python中调用DLL文件,可以使用ctypes库。这个库在Python3中已经自带了,不需要额外下载。首先,将生成的DLL文件复制到Python项目的目录下,确保它和调用DLL文件的Python文件在同一个目录下。然后,使用ctypes库中的CDLL函数加载DLL文件。例如,可以使用以下代码调用DLL文件中的add函数: ```python import ctypes from ctypes import * mydll = CDLL("./demo.dll") # 一定要有./ 否则无法调用成功 result = mydll.add(1, 2) print(result) ``` 需要注意的是,在Windows系统上,Python解释器的可执行文件通常位于C:\PythonXX\python.exe,其中XX是Python版本号。例如,Python 3.9的解释器可执行文件位于C:\Python39\python.exe。确保在CDLL函数中正确指定了DLL文件的路径。\[1\]\[2\] 如果DLL文件中的函数接受float类型的变量,而不是int类型,那么在Python端调用时需要先将数值转换为c_float类型。可以使用ctypes库中的c_float函数进行类型转换。例如,可以使用以下代码调用DLL文件中的add函数,并传入float类型的变量: ```python import ctypes from ctypes import * mydll = CDLL("./demo.dll") # 一定要有./ 否则无法调用成功 result = mydll.add(ctypes.c_float(1.345), ctypes.c_float(2.232)) print(result) ``` 这样就可以成功调用DLL文件中的函数,并得到结果。\[3\] #### 引用[.reference_title] - *1* *3* [Python调用C/C++之动态链接库(.dll)(vs2019+pycharm)](https://blog.csdn.net/Vccp_1M/article/details/125953351)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insertT0,239^v3^insert_chatgpt"}} ] [.reference_item] - *2* [Python解释器的各个文件夹的含义](https://blog.csdn.net/m0_63668906/article/details/129737458)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insertT0,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值