zoukankan      html  css  js  c++  java
  • 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;
    }
  • 相关阅读:
    Kafka使用代码设置offset值
    单机wordcount
    队列:队列在线程池等有限资源池中的应用
    栈:如何实现浏览器的前进和后退功能?
    Codeforces Beta Round #57 (Div. 2) E. Enemy is weak
    Codeforces Round #345 (Div. 1) A. Watchmen
    Codeforces Round #291 (Div. 2) B. Han Solo and Lazer Gun
    zoj4027 Sequence Swapping
    zoj4028 LIS
    zoj 3946 Highway Project
  • 原文地址:https://www.cnblogs.com/duxie/p/10859314.html
Copyright © 2011-2022 走看看