Windows 下的 C++动态加载DLL调用方法

 

文献参考 http://man7.org/linux/man-pages/man0/dlfcn.h.0p.html

    http://man7.org/linux/man-pages/man3/dlopen.3.html

    http://tldp.org/HOWTO/Program-Library-HOWTO/dl-libraries.html

    http://www.yolinux.com/TUTORIALS/LibraryArchives-StaticAndDynamic.html

 

加入头文件

#include <dlfcn.h>

定义函数接口 如:

typedef long (*PFN_TEST)(const char* szName, int nAge);
PFN_TEST g_Test = NULL;

然后是调用步骤:

    void* handle = dlopen("/path/to/so", RTLD_LAZY);
    if(!handle)
    {        
            printf("ERROR, Message(%s).\n", dlerror());
            return -1;
    }

    g_Test = (PFN_TEST)dlsym(handle, "Test");
    char* szError = dlerror();
    if(szError != NULL)
    {
        printf("ERROR, Message(%s).\n", szError);
        dlclose(handle);
        return -1;
    }
    if(g_Test != NULL)
    {
        g_Test ("wjshan", 0808);
    }
    dlclose(handle);
    g_Test = NULL;
    return 0;

 

相关文章:

  • 2021-08-11
  • 2021-12-25
  • 2021-10-21
  • 2022-02-04
  • 2021-05-25
  • 2021-06-18
猜你喜欢
  • 2022-12-23
  • 2022-12-23
  • 2022-02-05
  • 2021-06-10
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
相关资源
相似解决方案