admin 管理员组

文章数量: 887021

VC++

文章目录

  • DLL的创建和使用
    • 动态链接库概述
    • 1.新建项目
      • 1-1.新建文件
      • 1-2.生成动态链接库
    • 2.Dumpbin命令
      • 2-1.用法
    • 3.从DLL中导出函数
    • 4.参考

DLL的创建和使用

动态链接库概述

1.新建项目

1-1.新建文件

新建DLL1.cpp

#include "pch.h"int add(int a, int b)
{return (a+b);
}int subtract(int a, int b)
{return (a-b);
}

1-2.生成动态链接库

2.Dumpbin命令

该命令位于:D:\03_tools\DesTools\Microsoft Visual Studio\2017\Community\VC\Tools\MSVC\14.16.27023\bin\Hostx64\x64目录下,添加到PATH环境变量中;

2-1.用法

PS D:\05_study\mark-down-doc\11-tools\VS2017\Dll1\ch20\Debug> dumpbin.exe
Microsoft (R) COFF/PE Dumper Version 14.16.27049.0
Copyright (C) Microsoft Corporation.  All rights reserved.用法: DUMPBIN [选项] [文件]选项:/ALL/ARCHIVEMEMBERS/CLRHEADER/DEPENDENTS/DIRECTIVES/DISASM[:{BYTES|NOBYTES}]/ERRORREPORT:{NONE|PROMPT|QUEUE|SEND}/EXPORTS/FPO/HEADERS/IMPORTS[:文件名]/LINENUMBERS/LINKERMEMBER[:{1|2}]/LOADCONFIG/NOLOGO/OUT:filename/PDATA/PDBPATH[:VERBOSE]/RANGE:vaMin[,vaMax]/RAWDATA[:{NONE|1|2|4|8}[,#]]/RELOCATIONS/SECTION:名称/SUMMARY/SYMBOLS/TLS/UNWINDINFO

Windows PowerShell
版权所有(C) Microsoft Corporation。保留所有权利。安装最新的 PowerShell,了解新功能和改进! D:\05_study\mark-down-doc\11-tools\VS2017\Dll1\ch20\Debug> dumpbin.exe -exports .\Dll1.dll
Microsoft (R) COFF/PE Dumper Version 14.16.27049.0
Copyright (C) Microsoft Corporation.  All rights reserved.Dump of file .\Dll1.dllFile Type: DLLSummary1000 .00cfg1000 .data1000 .idata1000 .msvcjmc2000 .rdata1000 .reloc1000 .rsrc5000 .text10000 .textbss

上面输出没有任何与函数有关的信息,Dll1.dll因此没有导出任何函数;

3.从DLL中导出函数

代码修改如下,在函数前面添加标识符“_declspec(dllexport)”;

#include "pch.h"_declspec(dllexport) int add(int a, int b)
{return (a+b);
}_declspec(dllexport) int subtract(int a, int b)
{return (a-b);
}

重新生成DLL文件。

PS D:\05_study\mark-down-doc\11-tools\VS2017\Dll1\ch20\Debug> dumpbin.exe -exports .\Dll1.dll
Microsoft (R) COFF/PE Dumper Version 14.16.27049.0
Copyright (C) Microsoft Corporation.  All rights reserved.Dump of file .\Dll1.dllFile Type: DLLSection contains the following exports for Dll1.dll00000000 characteristicsFFFFFFFF time date stamp0.00 version1 ordinal base2 number of functions2 number of namesordinal hint RVA      name1    0 00011136 ?add@@YAHHH@Z = @ILT+305(?add@@YAHHH@Z)2    1 000111E5 ?subtract@@YAHHH@Z = @ILT+480(?subtract@@YAHHH@Z)Summary1000 .00cfg1000 .data1000 .idata1000 .msvcjmc2000 .rdata1000 .reloc1000 .rsrc5000 .text10000 .textbss

4.参考

1.VC++深入详解(第3版)(基于Visual Studio 2017) 孙鑫@编著

本文标签: VC