• C++ DLL导出的两种方式和链接的两种方式


    第一种 导出方式

    extern "C" _declspec(dllexport) int Plus(int x, int y);
    extern "C" _declspec(dllexport) int Sub(int x, int y);
    extern "C" _declspec(dllexport) int Mul(int x, int y);
    extern "C" _declspec(dllexport) int Div(int x, int y);
    
    int Plus(int x, int y)
    {
        return x + y;
    }
    
    int Sub(int x, int y)
    {
        return x - y;
    }
    
    int Mul(int x, int y)
    {
        return x * y;
    }
    
    int Div(int x, int y)
    {
        return x / y;
    }

    第二种 导出方式

    在项目上添加一个def文件

    // def文件里面
    EXPORTS Plus @
    12 Sub @17 Mul @15 NONAME // (此种方式只导出序号) Div @16
    // CPP文件里面
    int
    Plus(int x, int y) { return x + y; } int Sub(int x, int y) { return x - y; } int Mul(int x, int y) { return x * y; } int Div(int x, int y) { return x / y; }

    DLL使用

    第一种 隐式链接

    // 先把TestDll.lib 和 TestDll.dll放在main.cpp 同一目录下
    #include <stdio.h> #pragma comment(lib, "TestDll.lib") extern "C" _declspec(dllimport) int Plus(int x, int y); extern "C" _declspec(dllimport) int Sub(int x, int y); extern "C" _declspec(dllimport) int Mul(int x, int y); extern "C" _declspec(dllimport) int Div(int x, int y); int main() { int d = Plus(2, 3); printf("%d", d); getchar(); return 0; }

    第二种 显示链接

    #include <stdio.h>
    #include <windows.h>
    
    int main()
    {
        // 定义函数指针
        typedef int(*lpPlus)(int, int);
        typedef int(*lpSub)(int, int);
        typedef int(*lpMul)(int, int);
        typedef int(*lpDiv)(int, int);
        // 获取模块句柄
        HMODULE hMoudle = LoadLibrary(L"TestDll.dll");
        // 获取函数地址
        lpPlus MyPlus = (lpPlus)GetProcAddress(hMoudle, "Plus");
        lpSub MySub = (lpSub)GetProcAddress(hMoudle, "Sub");
        lpMul MyMul = (lpMul)GetProcAddress(hMoudle, "Mul");
        lpDiv Mydiv = (lpDiv)GetProcAddress(hMoudle, "Div");
        // 调用
        int d = MyPlus(2, 3);
        printf("%d", d);
        getchar();
        return 0;
    }
  • 相关阅读:
    五大Java开源论坛
    mysql limit,offset 区别
    查询某个字段存在于哪几个表
    C++分享笔记:5X5单词字谜游戏设计
    Linux分享笔记:系统状态检测命令小结
    Linux分享笔记:查看帮助命令 & 常用系统工作命令
    数据结构(C语言)分享笔记:数据结构的逻辑层次、存储层次
    Linux分享笔记:shell终端的介绍
    Java开发学生管理系统
    JAVA使用JDBC连接,修改MySQL数据库(比较乱)
  • 原文地址:https://www.cnblogs.com/duxie/p/10859314.html
Copyright © 2020-2023  润新知