关于DLL加载的问题

今天用IceSword研究DLL的注入时遇到一个奇怪问题。

当我调用LoadLibrary时,不知道什么原因(后来发现,可能是手痒了,多按了几下),用FreeLibrary,都释放不了DLL。

后来经过我多次试验,发现以下规律:

当我多次调用LoadLibrary时,然后再调用FreeLibrary,最后用IceSword查看了目标进程的模块,发现注入的DLL未Unload!!!

但多次调用FreeLibrary时,DLL能Unload!!最后试验得出,LoadLibrary 与 FreeLibrary调用的次数要相等,才能Unload。

Oh,my god!!! 虽然解决了问题,但不知道根本原因啊!不甘心!为了查出真凶,不得不去查了一下全是英文的MSND。

 

后来去查了MSDN,发现

 

 

以下是MSDN的说明:

 

The system maintains a per-process reference count for each loaded module. A module that was loaded at process initialization due to load-time dynamic linking has a reference count of one. The reference count for a module is incremented each time the module is loaded by a call to LoadLibrary. The reference count is also incremented by a call to LoadLibraryEx unless the module is being loaded for the first time and is being loaded as a data or image file.

 

The reference count is decremented each time the FreeLibrary or FreeLibraryAndExitThread function is called for the module. When a module's reference count reaches zero or the process terminates, the system unloads the module from the address space of the process. Before unloading a library module, the system enables the module to detach from the process by calling the module's DllMain function, if it has one, with the DLL_PROCESS_DETACH value. Doing so gives the library module an opportunity to clean up resources allocated on behalf of the current process. After the entry-point function returns, the library module is removed from the address space of the current process.

 

简要的说就是每调用一次LoadLibrary内部计数会加1,每调用一次FreeLibrary内部计数减1,当内部计数为0时释放DLL。

 

 

by:pDaren

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值