DLL programming based on Visual C ++ 6.0

zhaozj2021-02-16  104

DLL programming based on Visual C 6.0

2003-3-28 12:44:42 Yesky Liu Tao reading: 14498

I. Introduction

Since Microsoft launches 16-bit Windows operating systems, each version of the Windows operating system is highly dependent on functions and data in dynamic link library (DLL), in fact, almost all content in the Windows operating system is taken by DLL Or another form is represented, for example, the displayed font and icon stored in the GDI DLL, displaying the code required for the Windows desktop and the user's input, which is stored in a USER DLL, and a large number of API functions required for Windows programming. Also included in the Kernel DLL.

There are many advantages in the Windows operating system, the most important point is a number of applications, and even applications written in different languages ​​can share a DLL file, which really realizes "sharing", which greatly reduces the implementation of the application. Code, more efficient use of memory; another advantage to use DLL is that the DLL file is a separate program module, the package, and independence. When the software needs to upgrade, the developer only needs to modify the corresponding DLL file. After the function in the DLL changes, the program code does not need to recompile as long as the function is changed. This is useful when programming, greatly improving the efficiency of software development and maintenance.

Since DLL is so important, what is DLL, how to develop a DLL in a Windows operating system is a problem that program developers have to resolve. In this paper, the process of programming the DLL in the Visual C compilation environment is comprehensively parsed in a DLL in a DLL, which compares the two simple functions of the Visual C compilation environment in a DLL. The code is passed in the Windows98 system, the Visual C 6.0 compilation environment.

Second, DLL concept

The DLL is the concept of establishing a client / server communication, a library file, a function, and data stored in a number of functions, classes, or resources are stored on a DLL (server) and is used by one or more customers. These customers can be Applications or other DLLs. The DLL library does not do with static libraries. In static libraries, functions and data are compiled into a binary (usually extended to * .lib), the Visual C compiler will restore these functions from a static library when handling program code. And data and combine them with other modules in the application to generate executables. This process is called "static link". At this time, because all the content required by the application is copied from the library, the static library itself does not need to be issued with the executable.

In the case of a dynamic library, there are two files, one is an introduction library (.lib) file, one is a DLL file, the introduction library file contains the name and location of the function exported by the DLL, the DLL contains actual functions and data, application The program uses the lib file to link to the DLL file you want, and the functions and data in the library do not copy to the executable, so in the application's executable, stored not called the function code, but the DLL The memory address of the function you want to call, so when one or more application runs, the program code is linked to the program code and the called function code, thereby saving memory resources. It can be seen from the above instructions that the DLL and .lib files must be issued with the application, otherwise the application will generate errors.

Microsoft's Visual C supports three DLLs, which are Non-MFC DLL (non-MFC dynamic libraries), Regular DLL (regular DLL), Extension DLL (extension DLL). The NON-MFC DLL refers to a class library structure that does not have to be written directly with a C language, which exports to the standard C interface, which can be called by an application written by non-MFC or MFC. Regular DLL: Like the following Extension DLLs, it is written in the MFC class library. Its obvious feature is that there is a class inherited CWINAPP in the source file (Note: This DLL is derived from cwinapp, but no news Loop), the exported function is a C function, C class, or C member function (note do not confuse the term C class with the Microsoft foundation C class of the MFC), calling a regular DLL application does not have to be an MFC application, as long as it is Calling a class C function can be used, which can be developed using DLL development applications in compilation environments such as Visual C , Dephi, Visual Basic, Borland C. Conventional DLLs are also finely divided into static links to MFC and dynamic links to the MFC, and the differences between these two conventional DLLs will be described below. With the conventional DLL, the extended DLL is used to export functions or subclasses that enhance the MFC base class, with this type of dynamic link library, can be used to output a class inherited from the MFC.

Extended DLL is created using the dynamic link version of the MFC and it is only called by the application written by the MFC class library. For example, you have created a derived class for ctoolbar classes from MFC to create a new toolbar. In order to export this class, you must put it in a MFC extended DLL. Extending the DLL and regular DLL are different, it does not have an object that is inherited from CWINAPP, so developers must add initialization code and end code in the DLLMAIN function in the DLL.

Third, the creation of dynamic link library

In the Visual C 6.0 development environment, open the file / new / project option, you can choose Win32 Dynamic-Link Library or MFC AppWizard [DLL] to create a different way to create a Non-MFC DLL, Regular DLL, Extension DLL, etc. Type dynamic link library.

1. WIN32 DYNAMIC-LINK LIBRARY mode creates a Non-MFC DLL dynamic link library

Each DLL must have an entry point, which is like a Winmain function like we have written with C. DLLMAIN in the Non-MFC DLL is a default entry function, you don't need to write your own DLL entry function, use this default portal function to make the dynamic link library to be properly initialized. If the application's DLL needs to assign an additional memory or resource, or when you need to initialize and clear your operation, you need to write the DLLMAIN () function in the corresponding DLL engineering .CPP file. .

Bool Apientry Dllmain (Handle Hmodule, DWORD UL_REASON_FOR_CALL, LPVOID LPRESERVED)

{

Switch (ul_reason_for_call)

{

Case DLL_Process_attach:

.......

Case DLL_THREAD_ATTACH:

.......

Case DLL_THREAD_DETACH:

.......

Case DLL_PROCESS_DETACH:

.....}

Return True;

}

In the parameter, hmoudle is a dynamic library that is transferred when the dynamic library is called (actually, it is a selector to the _dgroup segment); ul_reason_for_call is a logo that describes the cause of the dynamic library, when the process or When the thread is loaded or unloaded the dynamic link library, the operating system calls the entry function and describes the reason why the dynamic link library is called, all possible values: dll_process_attach: process is called, dll_thread_attach: thread called, DLL_Process_Detach: process Stop, DLL_THREAD_DETACH: The thread is stopped; LPRESERVED is the reserved parameter. So far, the entry function of the DLL has been written, and the remaining part is not difficult, you can join the functions or variables you want to output in the DLL project.

We already know that the DLL is a library file containing several functions. These functions should be exported before using functions in the DLL to supply applications. To export these functions, there are two ways, one is to use the export keyword_declspec (dllexport) when defining a function, and another method is to use the module definition file. DEF when creating a DLL file. Readers need to note that the DEF file cannot be used when using the first method. By following the two examples, how to create a DLL file using both methods.

1) Use the export function keyword_declspec (dllexPort) to create myDLL.dll, two functions in this dynamic link library, which are used to achieve maximum and minimum numbers of two numbers. Enter the following original code in mydll.h and mydll.cpp files, respectively:

//Mydll.h

Extern "C" _Declspec (DLLEXPORT) INT MAX (Int A, INT B);

Extern "C" _Declspec (DLLEXPORT) INT MIN (Int A, INT B);

//Mydll.cpp

#include

#include "mydll.h"

Int Max (Int A, Int B)

{

IF (a> = b) Return A;

Else

Return B;

}

INT min (int A, int b)

{

IF (a> = b) Return B;

Else

Return A;

}

After the dynamic link library is successful, open the debug directory in the MYDLL project, you can see two files of mydll.dll, mydll.lib. The lib file contains a DLL file name and a function name in the DLL file. This lib file is only a "image file" that should have a DLL file. In the DLL file, the length of the lib file is small, when implicitly linked DLL To use it. Readers may have noticed that there is a keyword "Extern C" in MyDLL.H, which allows other programming languages ​​to access functions in the DLL you have written.

2) Create a project with .def file MYDLL

In order to create a DLL with the .def file, first remove the mydll.h file in the project created by the previous example, keep mydll.cpp and remove the #include mydll.h statement in this file, and join a text file to the project , Named mydll.def, add the following code in this file:

Library mydll

Exports

Max

MIN

Where the library statement indicates that the DEF file is a corresponding DLL, the exports statement lists the function name to be exported. We can add @n, such as max @ 1, min @ 2, indicating that the function sequence to be exported is explicitly used in the export function to export. After the DLL is successful, open the Debug directory in the project, and you will also see the mydll.dll and mydll.lib files. 2. MFC AppWizard [DLL] generation regular / extended DLL

There are three ways to generate a DLL file under the MFC AppWizard [DLL], where to create a DLL is to select the way to create a DLL according to the actual situation. One is a routine DLL static link to the MFC, and the other is a conventional DLL dynamic link to the MFC. The difference between the two is that the former uses the MFC static link library, the generated DLL file length is large, generally not in this way, the latter uses the MFC dynamic link library, the generated DLL file length is small; dynamic link to MFC The rule DLL all the output functions should start with the following statement:

AFX_MANAGE_STATE (AFXGETSTATICModuleState ()) // This statement is used to switch the MFC module status correctly

The last one is the MFC extension DLL. This DLL feature is used to establish an MFC derived class, and the DLL is only called by the application written by the MFC class library. As we have already introduced, Extension DLLS and Regular DLLS are different. It does not have an object that is inherited from cwinapp, and the compiler defaults a DLL entry function dllmain () as an initialization of the DLL, you can function in this function Implementation, the code is as follows:

Bool WinApi Apientry Dllmain (Hinstance Hinstdll, DWord Reason, LPVOID FLMPLOAD)

{

Switch (REASON)

{

............... // Initialization code;

}

Return True;

}

Parameters Hinstdll Store DLL handle, parameter REASON specifies the cause of the call function, LPRESERVED is a parameter reserved by the system. For implicit links are a non-zero value, which is zero for explicit link values.

Building a DLL file under the MFC will automatically generate a DEF file framework. Other differences with the establishment of a traditional NON-MFC DLL, as long as you write keyword_declspec (dllexport) function types and function names in the corresponding header file, or Enter a function name under Exports in the generated DEF file. It should be noted that when the MFC extension DLL is distributed to other developers, don't forget to provide the header files and the corresponding .lib files and DLL itself, the developers can make full use of the extended DLL you develop.

Fourth, the link between Dynamic Link Library DLL

The application can use the DLL to use two ways: one is an implicit link and the other is an explicit link. You first know the structure information of the function in the DLL before using the DLL. Visual C 6.0 provides a small program named Dumpbin.exe in the VC / BIN directory, using it to view the function structure in the DLL file. In addition, the Windows system will follow the search order below to locate the DLL: 1. Contains the directory of the EXE file, 2. The current working directory of the process, 3. Windows system catalog, 4. Windows directory, 5. A series of directories listed in the PATH environment variable.

1. Implicit link

The implicit link is to load the DLL file into the application when the program starts execution. Implementing implicit links is easy, just write the import function keyword_declspec (dllimport) function name, etc., you can write to the application corresponding to the application. The following example calls the MIN function in the MyDLL.dll library by implicit link. First, you will be a project for TestDLL, in dlltest.h, DLLTest.cpp files, you can enter the following code, respectively: //dllteest.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 ("The result of comparison is% D / N", A);

}

Before creating a DLLTest.exe file, copy MYDLL.DLL and MYDLL.LIB to the directory where the current project is located, or copy it to the system directory of Windows. If the DLL uses a DEF file, you want to delete the keyword Extern "C" in the TestDll.h file. The keyword in the testdll.h file is the compiler to which you want to Visual C is linked to mydll.lib files, of course, developers can also use the #pragma Comment (Lib, "MyDll.lib") statement. The Object / Moduls column directly in the SETTING-> LINK page of the project can be fill in MYDLL.LIB.

2. Explicit link

Explicit link is that the application can load the DLL file at any time during the execution, or you can uninstall the DLL file at any time, which is impossible to do, so the explicit link has better flexibility, and more For the right. However, it is troublesome to achieve explicit link. In the application, the AFXLOADLIBRARY explicitly provided by LoadLibrary or MFC is explicitly tuned, the file name of the dynamic link library is the parameters of the two functions described above. So after getProcAddress () to get the desired introduction The function. Since then, you can call this introduction function like using the application custom function. Before the application exits, the AFXFREELIBRARY that will be provided with Freelibrary or MFC will release the dynamic link library. The following is an example of the MAX function in the DLL by explicitly linking the DLL.

#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 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 the MyDLL.dll file;

}

In the above example, use the type definition keyword type type type typedef, define the same function prototype pointer in the DLL, and then load the DLL into the current application and return the handle of the current DLL file, then pass the getProcAddress () function. Get the function pointer imported into the application, after the function call is completed, use freelibrary () to uninstall the DLL file. Before the compiler, first copy the DLL file to the directory where the project is located or Windows system directory. Use the explicit link application to compile the corresponding lib files when compiling. In addition, when using the getProcAddress () function, you can use the MakeintResource () function directly using the sequence number in the DLL, such as the GetProcAddress (HDLL, "MIN") to getProcaddress (HDLL, MakeintResource (2)) (function min) The sequence number in the DLL is 2), so that the function speed in the DLL is very fast, but to remember the use of the function of the function, an error will occur.

Based on the way, it is easy to understand, fully introduces the concept of dynamic link library, the dynamic link library creation and dynamic link links, and gives an easy way, I believe that readers can create their own dynamics Link library and applied to subsequent software development, of course, readers must skilled the DLL, and need to continue to explore in a large number of practices, I hope this paper can play a role in tiles.

转载请注明原文地址:https://www.9cbs.com/read-10746.html

New Post(0)