如何在C++中从内存中卸载DLL?

use*_*875 5 c++ memory dll loadlibrary

如何从内存中卸载DLL.我使用FreeLibrary但它仍然加载

HINSTANCE hGetProcIDDLL = LoadLibrary("path.dll");
f_funci func = (f_funci)GetProcAddress(hGetProcIDDLL, "method");
int x = func();
FreeLibrary(hGetProcIDDLL);
Run Code Online (Sandbox Code Playgroud)

我曾经UnmapViewOfFileFreeLibraryAndExitThread内存,但它仍然太

HDJ*_*MAI 14

在这个例子中,我将展示一个简短的测试,我们可以看到这两个函数LoadLibrary并且FreeLibrary运行良好.

我将用于Process explorer显示DLL是否在当前进程地址空间中加载.

所以我创建了一个名为test3.dll的非常简单的DLL

这是一个使用它的简单程序:

// A simple program that uses LoadLibrary and 
// Access test3.dll. 
// Then Unload test3.dll 
#include <windows.h> 
#include <iostream> 

typedef int (__cdecl *MYPROC)(LPWSTR); 

int main( void ) 
{ 
   HINSTANCE hinstLib; 
   BOOL fFreeResult; 

   // Get a handle to the DLL module.
   hinstLib = LoadLibrary(TEXT("test3.dll"));    //1: load the DLL

   // If the handle is valid, unload the DLL
   if (hinstLib != NULL) 
   {  
       fFreeResult = FreeLibrary(hinstLib);      //2: unload the DLL
   } 

   return 0;
}
Run Code Online (Sandbox Code Playgroud)

第一步:

当我们执行此语句时:

hinstLib = LoadLibrary(TEXT("test3.dll"));
Run Code Online (Sandbox Code Playgroud)

结果如下:

在此输入图像描述

我们可以清楚地看到test3.dll被加载到进程的地址空间中 useDLL.exe

第二步:

执行fFreeResult = FreeLibrary(hinstLib);语句时,结果如下:

在此输入图像描述

正如我们所见,DLL在进程useDLL.exe的地址空间中没有加载longuer

这两个功能LoadLibraryFreeLibrary工作很棒.

您可以查看本教程,了解如何使用它process explorer来显示给定进程中加载​​的DLL.