C++动态链接库 静态绑定 动态绑定

版权声明:本文为博主原创文章,转载请注明出处,谢谢! https://blog.csdn.net/xp731574722/article/details/80957299

动态链接库

Dynamic-Link Libraries - MSDN

动态链接库(DLL)时一个包含了数据和函数,可以由另一个模块(应用程序或DLL)中被调用。

DLL主要有两大功能,导出部分和内部部分,导出的函数由其他模块及定义它们的DLL中调用,内部函数通常仅用于定义他们的DLL中调用。尽管DLL可以导出数据,但其数据通常由自己的函数使用。当然,另一个模块读写该地址时没有任何保护。

DLL提供了一种模块化应用程序的方法,以便更轻松和重用它们的功能。当多个应用程序使用相同的功能时,DLL还有助于减少内存开销,因为尽管每个应用程序会获得自己的DLL数据副本,但应用程序共享DLL代码。

Windows的API实现为一组DLL,因此使用Windows API的任何进程都使用动态连接。

创建DLL

  1. 建立项目
    这里写图片描述

  2. 头文件中添加项

//CreateDLL.h
#ifdef CREATEDLL_EXPORTS  
#define CREATEDLL_API __declspec(dllexport)  
#else  
#define CREATEDLL_API __declspec(dllimport)  
#endif  
extern "C" _declspec(dllexport) CREATEDLL_API void printMax(int&, int&);

CREATEDLL_API void printMax(int&, int&);
CREATEDLL_API void printMax(int&, int&, int&);
  1. 修改
// CreateDLL.cpp: 定义 DLL 应用程序的导出函数。
//

#include "stdafx.h"
#include "CreateDLL.h"  
#include <iostream>

CREATEDLL_API void printMax(int& a, int& b)
{
    std::cout << "Among (" << a << "," << b << "), the Max Number is " << (a>b ? a : b) << "\n";
}

右键项目->生成

静态绑定

In load-time dynamic linking, a module makes explicit calls to exported DLL functions as if they were local functions. This requires you to link the module with the import library for the DLL that contains the functions. An import library supplies the system with the information needed to load the DLL and locate the exported DLL functions when the application is loaded.

加载时动态链接,显示调用,要求DLL的导入库和程序链接,导入库为系统提供加载DLL所需的信息,在加载程序时找到导出的DLL函数

右击解决方案->添加->新建项目

这里写图片描述

将上一步生成的.lib文件和.h文件复制到当前项目

添加项

//static.cpp
#include <iostream>
#include "CreateDLL.h"
#pragma comment(lib, "CreateDLL.lib")

int main()
{
    int x, y;
    std::cout << "Input 2 Numbers:";
    std::cin >> x >> y;
    printMax(x, y);
    return 0;
}

开始调试这里写图片描述

动态绑定

In run-time dynamic linking, a module uses the LoadLibrary or LoadLibraryEx function to load the DLL at run time. After the DLL is loaded, the module calls the GetProcAddress function to get the addresses of the exported DLL functions. The module calls the exported DLL functions using the function pointers returned by GetProcAddress. This eliminates the need for an import library.

运行时动态链接,使用LoadLibrary或LoadLibraryEX函数在运行时加载DLL。调用GetPorcAddress函数获取导出的DLL函数的地址。消除了对导入库的需要。

右击解决方案->添加->新建项目这里写图片描述

在源文件中添加项

// UseDLL.cpp
#include<Windows.h>  
#include<iostream>  

typedef void(*FUNA)(int&, int&);
int main()
{
    const char* dllName = "CreateDLL.dll";
    const char* funName1 = "printMax";
    int x(100), y(100), z(100);
    HMODULE hDLL = LoadLibrary(dllName);
    if (hDLL != NULL)
    {
        FUNA fp1 = FUNA(GetProcAddress(hDLL, funName1));
        if (fp1 != NULL)
        {
            std::cout << "Input 2 Numbers:";
            std::cin >> x >> y;
            fp1(x, y);
        }
        else
        {
            std::cout << "Cannot Find Function " << funName1 << std::endl;
        }
        FreeLibrary(hDLL);
    }
    else
    {
        std::cout << "Cannot Find " << dllName << std::endl;
    }
    system("pause");
    return 1;
}

将本项目设为启动项目,开始调试
这里写图片描述

猜你喜欢

转载自blog.csdn.net/xp731574722/article/details/80957299