Two methods of calling dll files in C++

There are two ways to declare an exported function in a DLL: one is to add the _declspec(dllexport) keyword in the function declaration, and the other is to use the module definition file declaration. The .def file provides the linker with information about the exports, properties, and other aspects of the linked program. During the linking phase, the .def file can be invoked with the /DEF (specify module definition file) linker option.

The .def file must contain at least the following module definition statements: 
1) The first statement in the file must be a LIBRARY statement. This statement identifies the .def file as belonging to a DLL. The LIBRARY statement is followed by the name of the DLL. The linker places this name in the DLL's import library. 
2) The EXPORTS statement lists the name and, if possible, the ordinal value of the DLL's exported function. Add the @ sign and a number after the function name to assign an ordinal value to the function. When specifying an ordinal value, the ordinal value must range from 1 to N, where N is the number of functions exported by the DLL. When using symbols, external callers can load functions using the function name and ordinal. 
 
You can try the following steps to create a DLL using the Def file.
1] Create a new win32 console application, select the DLL project type, and create an empty project.
2] Add a .cpp file to the project and output the following code.

     int __stdcall add(int a, int b)
    {
       return (a + b);
    }    
    int __stdcall sub(int a, int b)
    {
       return (a - b);
    }

3) Add a .def file to the project, name it my.def, and enter the following code:
  LIBRARY my
  EXPORTS  
  add 
  sub

4) Compile the project, in the solution directory, .dll and .lib files will be generated.

 

An application can use a DLL in two ways: one is implicitly linked, the other is explicit linking. Before using the DLL, you must first know the structure information of the functions in the DLL. Visual C++6.0 provides a small program named Dumpbin.exe in the VC\bin directory, which can be used to view the function structure in the DLL file. In addition, the Windows system will follow the following search order to locate the DLL: 1. Directory containing EXE files, 2. 2. The current working directory of the process. Windows system directory, 3. Windows directory, 5. A sequence of directories listed in the Path environment variable.

  1. implicit link

  Implicit linking is to load the DLL file into the application when the program starts executing. It is easy to implement implicit linking, as long as the import function keyword _declspec (dllimport) function name is written into the corresponding header file of the application. The following example calls the Min function in the MyDll.dll library through implicit linking. First generate a project as TestDll, enter the following codes in the DllTest.h and DllTest.cpp files respectively:


//Dlltest.h
#pragma comment(lib,"MyDll.lib")
extern "C"_declspec(dllimport) int Max(int a,int b);
extern "C"_declspec(dllimport) int Min(int a,int b);
//TestDll.cpp
#include
#include"Dlltest.h"
void main()
{int a;
a=min(8,10)
printf("比较的结果为%d\n",a);
}


  Before creating the DllTest.exe file, copy MyDll.dll and MyDll.lib to the directory where the current project is located, or to the System directory of Windows. If the DLL uses a def file, delete the keyword extern "C" in the TestDll.h file. The keyword Progam commit in the TestDll.h file is to link the Visual C+ compiler to the MyDll.lib file when linking. Of course, the developer can also not use the #pragma comment(lib, "MyDll.lib") statement, And directly fill in MyDll.lib in the Object/Moduls column of the Setting->Link page of the project.

  2. explicit link

  Explicit linking means that the application can load or unload DLL files at any time during the execution process, which cannot be done by implicit linking, so explicit linking has better flexibility and is more suitable for interpreted languages. as appropriate. Implementing explicit linking is a bit trickier, though. In the application, use LoadLibrary or AfxLoadLibrary provided by MFC to explicitly call in the dynamic link library made by yourself. The file name of the dynamic link library is the parameter of the above two functions, and then use GetProcAddress() to get the desired import. The function. From now on, you can call this imported function as if you were using a custom function in your application. Before the application exits, the dynamic link library should be released with FreeLibrary or AfxFreeLibrary provided by MFC. Below is an example of calling the Max function in a DLL with explicit linking.


#include
#include
void main(void)
{ typedef int(*pMax)(int a,int b); typedef int(*pMin)(int a,int b); HINSTANCE hDLL; PMax Max HDLL=LoadLibrary("MyDll. dll");//Load the dynamic link library MyDll.dll file; Max=(pMax)GetProcAddress(hDLL,"Max"); A=Max(5,8); Printf("The result of comparison is %d\n" , a); FreeLibrary(hDLL);//Uninstall MyDll.dll file; }









  In the above example, use the type definition keyword typedef to define a pointer to the same function prototype as in the DLL, then load the DLL into the current application through LoadLibray() and return the handle of the current DLL file, and then through the GetProcAddress() function Get the function pointer imported into the application. After the function is called, use FreeLibrary() to unload the DLL file. Before compiling the program, first copy the DLL file to the directory where the project is located or the Windows system directory.

  The corresponding Lib file is not required when compiling with an explicitly linked application. In addition, when using the GetProcAddress() function, you can use the MAKEINTRESOURCE() function to directly use the sequence number of the function in the DLL, such as changing GetProcAddress(hDLL, "Min") to GetProcAddress(hDLL, MAKEINTRESOURCE(2)) (function Min( ) The sequence number in the DLL is 2), so calling the function in the DLL is very fast, but remember the use sequence number of the function, otherwise an error will occur.

 

Related: Two methods of calling dll files in C++