1. 使用LoadLibrary函数加载DLL文件,返回一个句柄;
  2. 使用GetProcAddress函数获取DLL中指定函数的地址;
  3. 使用函数指针调用DLL中的函数;
  4. 使用FreeLibrary函数释放DLL文件。

示例代码:

#include <windows.h>
#include <iostream>

int main()
{
    HMODULE hModule = LoadLibrary("example.dll"); // 加载DLL文件,返回句柄
    if (hModule)
    {
        typedef int(*Fun)(int); // 定义函数指针类型
        Fun fun = (Fun)GetProcAddress(hModule, "exampleFunction"); // 获取函数地址
        if (fun)
        {
            int result = fun(5); // 调用函数
            std::cout << "Result: " << result << std::endl;
        }
        FreeLibrary(hModule); // 释放DLL文件
    }
    return 0;
}

其中,example.dll是一个示例DLL文件,exampleFunction是其中的一个函数。注意,函数指针类型的定义需要与DLL中函数的声明一致,包括返回值和参数类型。

把dll加载到内存中调用

原文地址: https://www.cveoy.top/t/topic/bIGm 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录