关于FreeLibrary函数,我将一段代码远程插入到其他进程中(远程线程插入,不是DLL插入)。在远程线程中执行LoadLibrary加载一个dll,dll加载成功。然后在远程线程中使用FreeLibrary释放这个加载的dll却始终不成功,用兵刃等工具查看,这个dll依旧存在于远程进程当中。这是什么原因呢?
那么我还有一个疑问,如果进程中的线程1调用LoadLibrary加载了一个dll文件。然后该进程的线程2通过消息的方式得到了加载这个dll的模块句柄,然后使用FreeLibrary是否可以释放掉这个dll。

解决方案 »

  1.   

    那么我还有一个疑问,如果进程中的线程1调用LoadLibrary加载了一个dll文件。然后该进程的线程2通过消息的方式得到了加载这个dll的模块句柄,然后使用FreeLibrary是否可以释放掉这个dll。
    这是可以的,因为所有的模块对整个进程都是透明的!
      

  2.   


    LoadLibrary和FreeLibrary可以跨线程配对,不能跨进程,至于你的注入就不了解了,可以GETLASTERROR看下。Module handles are not global or inheritable. A call to LoadLibrary by one process does not produce a handle that another process can use — for example, in calling GetProcAddress. The other process must make its own call to LoadLibrary for the module before calling GetProcAddress.Each process maintains a reference count for each loaded library module. This reference count is incremented each time LoadLibrary is called and is decremented each time FreeLibrary is called. A DLL module loaded at process initialization due to load-time dynamic linking has a reference count of one. This count is incremented if the same module is loaded by a call to LoadLibrary.During initial process startup or after a call to LoadLibrary, the system scans the list of loaded DLLs for the process. For each DLL that has not already been called with the DLL_PROCESS_ATTACH value, the system calls the DLL's entry-point function. This call is made in the context of the thread that caused the process address space to change, such as the primary thread of the process or the thread that called LoadLibrary.