DLL Development Summary
An introduction
DLL (Dynamic linkable Library), you can simply think of a DLL as a repository, which provides you with some variables, functions that you can use directly.
Static and dynamic libraries are all ways to share code, and their differences can be found in my previous blog post.
DLL programming is irrelevant to the specific language and compiler.
DLLs written in various languages can be used as long as they follow the agreed specification and invocation of DLL interfaces.
Calls to each other, such as the system DLLs provided by Windows, can be called in any development environment, regardless of the fact that Visualbasics,Visual C + + or Delphi.
The classification of VC dynamic link Library
Visualc++ supports three types of DLLs, namely Non-mfcdll(non- MFC dynamic Library), MFC
Regular DLLs (MFC-Rules DLLs), Mfcextension DLLs (MFC extension DLLs).
Non-MFC dynamic library does not adopt the MFC class library structure, its export function is the standard C interface, can be called by an application not written by MFC; The MFC rule DLL contains a class that inherits from CWinApp, but it has no message loop; The MFC extension DLL is created with the dynamic Link version of MFC. They can only be called by programs written by the MFC class library.
Two derived functions
There are two ways to declare a function in a DLL: one is to add __declspec (dllexport) to a function declaration, and the other is to use a module definition (. def) file declaration, which provides the linker with information about the exported, attributes, and other aspects of the linked program.
①extern "C" int __declspec (dllexport) Add (int x, int y) and how to use it can be found in the previous blog-the difference between a static library and a dynamic library.
②library Dlltest
Exports
Add @1
The rules for. def files are:
The ①library statement describes the corresponding DLL for the. def file;
The name of the function to be exported is listed after the ②exports statement. You can add @n to the exported function in the. def file to indicate that the ordinal of the function to be exported is n
Comments in the ③.def file are specified by a semicolon at the beginning of each comment line, and the comment cannot be accompanied by a statement.
When using a. def file, you can
hDLL = LoadLibrary ("DllTest.dll");
if (hdll!= NULL)
Addfun = (lpaddfun) GetProcAddress (Hdll,makeintresource (1));
FreeLibrary (hDLL);
Where 1 is the ordinal of the exported function.
Three export Variables
DLL-defined global variables can be accessed by the calling process, and DLLs can also access global data for the calling process, and we'll look at examples of variables referenced in the DLL in the application engineering.
Lib.h
#ifndef lib_h#define lib_hextern intdllglobalvar;extern "C" int getglobalvar (); #endif
Lib.cpp
#include "lib.h" #include <windows.h> intdllglobalvar; BOOL Apientrydllmain (HANDLE hmodule, DWORD ul_reason_for_call, lpvoid lpreserved ) { switch (ul_ Reason_for_call) {case dll_process_attach: dllglobalvar = +; break; Case Dll_thread_attach: Case Dll_thread_detach: Case Dll_process_detach: Break ; } return TRUE;}
The. def file is
LIBRARY Dlltestexportsdllglobalvardata
How to use:
#include "stdafx.h" #include <windows.h> #include <stdio.h> #pragmacomment (lib, "DllTest.lib") extern Intdllglobalvar; int main (INTARGC, char* argv[]) { printf ("%d", Getglobalvar ()); * (int *) Dllglobalvar = 1; It is particularly important to note that this method does not derive the variable itself, but rather a pointer to the exported variable in the DLL, and the application must //must use printf ("%d", Getglobalvar ()) by forcing the pointer to convert; return 0;}
If you perform such an assignment operation:
Dllglobalvar= 1;
The result is a change in the contents of the Dllglobalvar pointer, which is not referenced later in the program. So a better way to refer to global variables in a DLL in application engineering is to:
Externint _declspec (dllimport) dllglobalvar;//import with _declspec (dllimport)
Importing through _declspec (dllimport) is the global variable itself in the DLL and is no longer an address, so it is recommended to use this method in all possible cases.
Four DLL export classes
Point.h
#ifndefPOINT_H #definepoint_h #ifdefDLL_FILEclass_declspec (dllexport) point //Export Class Point#elseclass_declspec ( dllimport) point //import class Point#endif{public: float y; float x; Point (); Point (float x_coordinate,floaty_coordinate);}; #endif
Point.cpp
#ifndefDLL_FILE #definedll_file#endif #include "point.h"///////////////////////////////////////////////////////// construction/destruction////////////////////////////////////////////////////////////////////// Point::p oint () {}point::p oint (floatx_coordinate,float y_coordinate) { x = x_coordinate; y = y_coordinate;}
Circle.h
#ifndefCIRCLE_H #definecircle_h #include "point.h" #ifdefDLL_FILEclass_declspec (dllexport) Circle // Export Class Circle#elseclass_declspec (dllimport) Circle //Import class Circle#endif{public: void setcentre (const point¢ Repoint); void Setradius (float R); float Getgirth (); float Getarea (); Circle ();p rivate: float radius; Point Centre;}; <p> #endif </p>
Circle.cpp
#ifndefDLL_FILE #definedll_file#endif #include "circle.h" #definePI 3.1415926circle::circle () { Centre = point (0,0 ); radius = 0;} Floatcircle::getarea () { return Pi*radius*radius;} Floatcircle::getgirth () { return 2*pi*radius;} Voidcircle::setcentre (const point¢repoint) { centre = Centrepoint;} Voidcircle::setradius (float R) { radius = r;}
Call:
DllCall.cpp
#include "stdafx.h" #include <windows.h> #include ". \circle.h "#pragmacomment (Lib," DllTest.lib "); Intmain (int argc, char* argv[]) { circle C; Point P (2.0,2.0); C.setcentre (p); C.setradius (1.0); printf ("Area:%fgirth:%f", C.getarea (), C.getgirth ()); return 0;}
From the above source code can be seen, because in the DLL's class implementation code defined macro Dll_file, so in the implementation of the DLL contains the class declaration is actually
Class_declspec (dllexport) point{...};
And
Class_declspec (dllexport) circle{...};;..};
Dll_file is not defined in the application engineering DllCall.cpp, so the class declarations introduced after Point.h and circle.h include:
Class_declspec (dllimport) point{, ...};
And
Class_declspec (dllimport) circle{, ...};
We often decide whether to compile to Class_declspec (dllexport) class_name or Class_declspec (dllimport) class_name version by using a macro in the declaration header file of the class. This will not require two header files.
Discussion of blog links in static and dynamic library areas
http://blog.csdn.net/lqlblog/article/details/48086569
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
VC + + DLL Development Summary