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,了解新功能和改进!https://aka.ms/PSWindows
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.dll
File Type: DLL
Summary
1000 .00cfg
1000 .data
1000 .idata
1000 .msvcjmc
2000 .rdata
1000 .reloc
1000 .rsrc
5000 .text
10000 .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.dll
File Type: DLL
Section contains the following exports for Dll1.dll
00000000 characteristics
FFFFFFFF time date stamp
0.00 version
1 ordinal base
2 number of functions
2 number of names
ordinal hint RVA name
1 0 00011136 ?add@@YAHHH@Z = @ILT+305(?add@@YAHHH@Z)
2 1 000111E5 ?subtract@@YAHHH@Z = @ILT+480(?subtract@@YAHHH@Z)
Summary
1000 .00cfg
1000 .data
1000 .idata
1000 .msvcjmc
2000 .rdata
1000 .reloc
1000 .rsrc
5000 .text
10000 .textbss
4.参考
1.VC++深入详解(第3版)(基于Visual Studio 2017) 孙鑫@编著