Earlier we described the DLL generation, most of which were exported using the extern "C" __declspec (dllexport) + function name method. In fact, we have another way to export the DLL.
First introduce the reference:
1.dll Export Declaration related
2.vs2012 Creating DLL plots in C + +
Two ways to export functions in 3.DLL (dllexport and. def files)
4 Exporting a function from a DLL by ordinal rather than by name
5. Module definition (. DEF) file
Creating DLL plots in C + + in 6.vs2010: Describes how to define DEF files
7.MS vs generates only DLLs, does not generate Lib
The role of DEF files in 8.VS for generating DLLs and Lib files
And then:
We can focus on reading 3 and 6. 8 indicates that the function of DEF file is equivalent to extern "C" __declspec (dllexport)
I give an example here (click to download). proves the correctness of the words here.
The Post code is:
Testdll.h
#ifndef testdll_h_
#define Testdll_h_
extern "C"
{
int Add (int plus1, int. plus2);
int substract (int a);
}
#endif <span style= "color: #ff0000;" ><strong>
</strong></span>
. cpp
NewDLL.cpp: Defines an export function for a DLL application.
//
#include "stdafx.h"
#include "testdll.h"
#include <iostream>
using namespace std;
int Add (int plus1, int plus2)
{
int add_result = Plus1 + plus2;
return add_result;
}
int substract (int a)
{
return A;
}
Call DLL:
#include <Windows.h>
#include <iostream>
#include "testdll.h"
typedef int (*FUNC) (int, int);
#pragma comment (lib, "NewDLL.lib")
int main ()
{
hmodule hdll = LoadLibrary ("NewDLL.dll");
if (hdll! = nullptr)
{
func f = func (GetProcAddress (hDLL, Makeintresource (2)));
if (f! = nullptr)
{
std::cout << "Input 2 num:";
int a, B;
Std::cin >> a >> b;
Std::cout << "result is" << Add (A, b);
}
else
{
std::cout << "Connot find the function" << "add" << Std::endl;
}
FreeLibrary (hDLL);
}
else
{
std::cout << "Cannot load DLL" << "NewDLL.dll" << Std::endl;
}
System ("pause");
return 0;
}
DEF is defined as:
LIBRARY "Newdll"
exports
Add @2 NONAME
Attention:
1. We want to specify the header and Lib files of the DLL in the calling project. And in the project that generated the DLL, DEF would indicate
Select the linker in the Project > properties and find the entry. Enter ***.DEF in the module definition file
2. def files indicate the order of functions, and functions are generated using C-style, which allows us to invoke them directly using the function name or ordinal when explicitly called. We can also use the Noname property to save memory and remove the function name. Reference 4. The DLL we generated above has no function name, we can use DUMPBIN to view:
You can see that using Def, you can basically not change the function in the header file, we manually indicated in the def. And if your DLL is available to VC users, you just need to put the. Lib generated when compiling the DLL to the user,
It can be very easy to call your DLL. But if your DLL is for VB, PB, Delphi user use, then will create a small trouble.
Because the VC + + compiler will perform name conversions for __declspec (dllexport) declared functions, such as the following function:
__declspec (dllexport) int __stdcall Add ()
will be converted to add@0, so you must declare it in VB:
Declare Function Add Lib "DLLTestDef.dll" Alias "add@0" () as Long
The number after the @ may be different due to different parameter types. This is obviously not very convenient. So if you want to avoid this conversion, you'll need to export the function using the. def file.