• 主页
  • 相册
  • 随笔
  • 目录
  • 存档
Total 244
Search AboutMe

  • 主页
  • 相册
  • 随笔
  • 目录
  • 存档

创建和使用DLL

2020-01-01

1. C++创建和使用DLL

2. 实验目的

  • 在 Visual Studio 中创建 DLL 项目。
  • 将导出的函数和变量添加到该 DLL。
  • 在 Visual Studio 中创建一个控制台应用项目。
  • 在该控制台应用中使用从 DLL 导入的函数和变量。
  • 运行已完成的应用。
  • 手工编写DLL

3. 实验环境

  • VS2019

4. 实验原理

dllexport、dllimport

Microsoft Specific

The dllexport and dllimport storage-class attributes are Microsoft-specific extensions to the C and C++ languages. You can use them to export and import functions, data, and objects to or from a DLL.


These attributes explicitly define the DLL’s interface to its client, which can be the executable file or another DLL. Declaring functions as dllexport eliminates the need for a module-definition (.def) file, at least with respect to the specification of exported functions. The dllexport attribute replaces the __export keyword.

5. 实验内容

6. VS编写与引入DLL

编写DLL

  • 新建项目,选择 动态链接库(DLL),默认项目目录如下👇

  • 修改pch.h

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    #ifndef PCH_H
    #define PCH_H

    #ifdef _EXPORTING
    #define API_DECLSPEC __declspec(dllexport)
    #else
    #define API_DECLSPEC __declspec(dllimport)
    #endif
    // 添加要在此处预编译的标头
    #include "framework.h"

    #endif //PCH_H


    API_DECLSPEC void func(char* msg);
  • 修改pch.cpp

    1
    2
    3
    4
    5
    #include "pch.h"
    void func(char* msg)
    {
    MessageBoxA(0,"OK", msg, MB_OK);
    }
  • 调试,在debug目录下生成.lib和.dll文件

引入DLL

  • 新开控制台项目,新建源文件

    1
    2
    3
    4
    5
    6
    7
    8
    9
    #define _CRT_SECURE_NO_WARNINGS
    #include <iostream>
    #include"pch.h"
    int main()
    {
    char msg[10];
    strcpy(msg, "123");
    func(msg);
    }
  • (可选)将DLL项目下的四个默认生成文件与DEBUG文件夹拷贝到当前目录下

  • 进行如下配置👇

    • 将 DLL 标头添加到包含路径

  • 指定指向 pch.h 头文件的位置的路径

    • 因为已经拷贝到当前目录,所以可以缺省
      • 此时可以编译通过,但无法链接,这是由于还未指定.lib文件的目录
  • 将 DLL 导入库添加到项目中

    * 指定静态数据连接库文件
    * 指定库所在地址——`Debug`目录下

运行结果

  • 调试

  • 用Process Explorer查看

7. 手工编写与引入DLL

  • 创建源文件

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    #include <Windows.h>
    // 不会被导出的函数
    int intnal_function() {
    return 0;
    }
    // 会被导出的函数
    int lib_function(char* msg) {
    printf("1");
    return 0;
    }
  • 创建.def文件

    其含义是将导出的lib文件命名为baselib和只把lib_function函数导出

    1
    2
    3
    LIBRARY   baselib
    EXPORTS
    lib_function
  • 编译:cl /c x.cpp

  • 链接:link x.obj /out:baselib.dll /dll /def:Source.def

  • 结果

7.1. 常见问题及解决

不包括路径集\无法打开xx.h

  • 编译:在环境变量中新建INCLUDE,添加.h所在目录地址
  • 链接:在环境变量中新建LIB,添加.lib所在目录地址

8. 参考资料

演练:创建和使用自己的动态链接库 (C++)

dllexport, dllimport | Microsoft Docs

命令行下cl.exe编译链接的问题及解决方法 - ShadonSniper - 博客园

  • sec
  • Security
  • Software Security
数字内容安全备忘录
进线程实验
  1. 1. 1. C++创建和使用DLL
  2. 2. 2. 实验目的
  3. 3. 3. 实验环境
  4. 4. 4. 实验原理
  5. 5. 5. 实验内容
    1. 5.1. 6. VS编写与引入DLL
    2. 5.2. 7. 手工编写与引入DLL
      1. 5.2.1. 7.1. 常见问题及解决
  6. 6. 8. 参考资料
© 2024 何决云 载入天数...