Based on the Visual C + + 6.0 Programming DDL

Author:    Updated:2008-3-26 12:30:25
I. Introduction

Since Microsoft introduced the Windows 16 operating system, since each version of the Windows operating system are very dependent on dynamic link library (DLL) of the functions and data, the Windows operating system, in fact almost all the contents are in a DLL or another form of a representative, for example, show that the fonts and icons stored in the GDI DLL in the Windows desktop display and handle user input required by the code are stored in a User DLL in the Windows programming required by the large number of API function has also been included in the Kernel DLL in the.

In the Windows operating system used in the DLL has many advantages, the most important point is that multiple applications, and even different languages of the applications can share a DLL files and truly achieved the resources "share", greatly narrowing the application procedures the implementation of the code, and more efficient use of memory; Another advantage of the use of DLL files is DLL as a separate program modules, Packaging, independence, and the need to upgrade the software, developers only need to amend the corresponding DLL documents can be, but, when the DLL function changes, as long as the parameters are not changed, the code does not need to recompile. This is very useful in the programming, greatly improving the software development and maintenance efficiency.

Since the DLL is so important, what is so clear DLL, and how to develop the use of the Windows operating system DLL is developers have to solve a problem. This paper address these problems through a simple example, a DLL that is the largest in the realization of comparison, the smallest integer these two simple functions, and comprehensive analysis in the Visual C + + compiler environment DLL programming process, used in the article Windows98 code in the system, Visual C + + 6.0 compiler environment through.

Second, the concept of DLL

DLL is built on client / server communication concept includes a number of functions, category or resources of the library, function and data are stored in a DLL (servers) on one or more customers by using derived, these customers can be Application procedures or other DLL. DLL library different from the static libraries, in the static case, function and the data have been compiled into a binary file (usually extension *. LIB), Visual C + + compiler code in the process to recover from the static libraries of these functions and data and their applications and in combination with other modules generate executable file. This process is known as "static link", this application because all the necessary elements are copied from the library out, static library itself executable file and do not need to issue.

Dynamic Library in the circumstances, there are two documents, one is the introduction of the (. LIB) document, a DLL file is the introduction of the document contains the DLL function derived the name and location of the actual DLL contains the function and data, applications LIB document procedures used by the need to use the links to the DLL files, libraries of functions and data is not copied to the executable file, and therefore the application executable file, the storage is not a function of the code is called, but in the DLL Call to be a function of the memory address, such as one or more applications running code is then call the function and the code links, thus saving memory resources. From the above statement can be seen, and DLL. LIB documents must be issued together with the application procedures, otherwise the application will lead to errors.

Microsoft's Visual C + + supports three DLL, they are Non-MFC Dll (MFC DLL), Regular Dll (conventional DLL), Extension Dll (extension DLL). Non-MFC DLL that is not the MFC class library structure, and the direct use of the C language to write DLL, the function is derived standard C interface, or can be non-MFC MFC application prepared by the call. Regular DLL: and the following Extension Dlls, MFC class library is used by one of its distinctive features in the source documents in a succession CWinApp class (Note: Although such DLL CWinApp derived from, but no news Circular), the derived function is C function, C + + or C + + class member functions (not to pay attention to terms with C + + class based on Microsoft MFC C + + Class of confusion), call conventional application DLL is not MFC applications, as long as they are able to Class C function calls the application can, they can be in the Visual C + +, Dephi, Visual Basic, Borland C compiler, such as the development environment by using the DLL application.

DLL can be subdivided into conventional static and dynamic link to MFC link to the MFC, the distinction between two kinds of conventional DLL will be described below. Compared with conventional DLL, the use of extension DLL MFC basis for enhanced export category or sub-category of function with this type of dynamic link library that can be used to output an inherited from the MFC class.

DLL extension of the use of the dynamic link MFC version created, and it is only being used by the MFC class library prepared by the calling application. For example, you have created a category from the MFC CtoolBar a derivative of the type used to create a new toolbar, in order to export this class, you have to put it to the expansion of a DLL in the MFC. DLL extension and conventional DLL not the same, it did not have a CWinApp inherited from the type of object, so developers must DllMain functions in the DLL initialization code and add the end of the code.



Third, the creation of dynamic link library

In Visual C + + 6.0 development environment, open the File \ New \ Project option, you can choose Win32 Dynamic-Link Library or MFC AppWizard [dll] to a different way to create Non-MFC Dll, Regular Dll, such as Extension Dll different types of dynamic link library.

1. Win32 Dynamic-Link Library creates Non-MFC DLL dynamic link library

Each one must have a DLL entry point, as we prepare to C applications, there must be a WINMAIN the same function. Non-MFC DLL in DllMain is a default in the import function, you do not need to prepare their own entrance DLL function, and this function will default to the entrance of dynamic link library is called proper initialization. If the application DLL need to allocate additional memory or resources, or the need for each process or thread initialization and removal operations, it needs to in the corresponding DLL works. CPP document on DllMain () function in the following the format of writing.


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;
)

Parameters, hMoudle is a dynamic library is called the transfer to a handle at its own (actually, it was pointed _ DGROUP paragraph at a choice); ul_reason_for_call is a dynamic that is the reason for the signs, when the process Thread loaded or unloaded or dynamic link library, the entrance to the operating system function calls, and the dynamic link library is called for, it all possible values are: DLL_PROCESS_ATTACH: the process is called, DLL_THREAD_ATTACH: thread is called, DLL_PROCESS_DETACH : process was stopped, DLL_THREAD_DETACH: thread being suspended; lpReserved parameters for reservations. End, the entrance of DLL function has been written, and the realization of the remaining part will not be difficult, you can DLL project you want to add the function or output variables.

We already know that contains a number of DLL function of the library, applications, the use of the DLL function, the pilot should these functions, in order to supply applications to use. To export these functions There are two ways, one at the function definition is derived using keywords _ declspec (dllexport), another way is to create DLL for use module definition files. Def. Need the attention of the readers in the use of a method when the document can not be used DEF. Below through two examples to illustrate how to use these two methods to create DLL files.

1) The use of keywords derived function _ declspec (dllexport) MyDll.dll create the dynamic link library has two functions, respectively, to the realization of two of the largest and the smallest number. MyDLL.cpp MyDll.h and in the document were the original input code as follows:


/ / 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;
)

The dynamic link library compiled after the success of the project opened MyDll debug directory, we can see that MyDll.dll, MyDll.lib two documents. LIB files contain the DLL file names and DLL function names in the document, the LIB files should DLL files only on the "image files" and the DLL files, LIB file to the length of the small, in implicit links DLL, to use it. Readers may have noted in MyDll.h in the keyword "extern C", which will enable you to visit other programming language in the preparation of the DLL function.

2) use. Def file creation works MyDll

In order to use. Def DLL file creation, please delete the previous examples to create works of MyDll.h documents, and retain MyDll.cpp first delete the document # include MyDll.h statement, the project at the same time to add a text file named MyDll.def, then joined the paper in the following code:

LIBRARY MyDll
EXPORTS
Max
Min

LIBRARY statement which shows that the document belongs to the corresponding def DLL, EXPORTS following statement should be derived from the function name. We can. Def derived function in the document added @ n, such as Max @ 1, Min @ 2, a function that should be derived sequence, in explicit linkage can use it. The DLL compiled after the success of the project opened Debug directory, and they will also see MyDll.dll MyDll.lib document.

2. MFC AppWizard [dll] generated conventional / extension DLL

In MFC AppWizard [dll] DLL files are generated in three ways, in the creation of DLL, according to the actual situation choose to create a DLL approach. A conventional static links to the DLL MFC, and the other is conventional DLL dynamic link to MFC. Is the difference between the two: the former is the use of the static MFC link library, DLL files generated length, generally do not use this method, the latter using MFC the dynamic link library, DLL files generated by the length of small, dynamic links to MFC DLL all the rules of the output function should start with the following statement:


AFX_MANAGE_STATE (AfxGetStaticModuleState ()) / / this statement to correctly state switching module MFC

Finally, the MFC extension DLL, this DLL is used to establish the characteristics of the derived class MFC, Dll MFC class library used only by the preparation of the application procedure call. We have already introduced earlier, Extension DLLs and Regular DLLs not the same, it did not have a CWinApp inherited from the category of object, the compiler default entrance to a DLL function DLLMain () as a DLL initialization, you can be in this function achieve initialization code as follows:


BOOL WINAPI APIENTRY DLLMain (HINSTANCE hinstDll, DWORD reason, LPVOID flmpload)
(
Switch (reason)
(
…………… / / Initialization code;
)
Return true;
)

Storage parameters hinstDll handle the DLL, the function call parameters specified reason why lpReserved was a reservation system parameters. The implicit links to a non-zero value, explicit links value is zero.

MFC in the establishment of DLL files will be automatically generated def framework, the other with the establishment of the Non-traditional MFC DLL there is no difference between, as long as the first document in the corresponding write keywords _ declspec (dllexport) function type and function names, or in the generated document EXPORTS def import function name on it. It must be noted that other developers to distribute MFC extension DLL, do not forget to provide DLL described in the first category, as well as the corresponding document. LIB files and DLL itself, since developers will be able to take full advantage of your DLL extension of the development.


4, dynamic link library DLL links

Application procedures for the use of DLL can be used in two ways: one is implicit links, and the other is explicit link. DLL in use before it must be remembered that DLL function in the structure of information. Visual C + + 6.0 in VC \ bin directory provides a procedure called Dumpbin.exe small, it can be read using the DLL function in the structure. In addition, the Windows operating system will follow the following sequence search to locate DLL: 1. EXE contains the directory, 2. The process of the current working directory, 3. Windows system directory, 4. Windows directory, 5. Included in the Path environment variable in a series of directory.

1. Implicit Links

Link is implicit in the beginning of the process implementation will be loaded DLL file to the application program. Achieve implicit links very easily, as long as the import function will be keywords _ declspec (dllimport) function of the corresponding applications such as wrote the first paper on it. The following example through implicit links in the call MyDll.dll Min function. First generate a project TestDll in DllTest.h, DllTest.cpp documents were entering the following code:


/ / 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 (8th, 10th)
Printf ( "Comparison of the results of% d \ n", a);
)
 

DllTest.exe before in the creation of documents, and the first will be MyDll.dll MyDll.lib copy of the current projects, the directory, windows can be copied to the System directory. If the DLL is used def document, it is necessary to delete keywords in the document TestDll.h extern "C." TestDll.h a keyword in a document Progam commit to Visual C + + compiler in the link, the link to MyDll.lib document, of course, developers can also use # pragma comment (lib, "MyDll.lib") Statements , and works directly in the Setting-> Object Link page / Moduls MyDll.lib can fill column.

2. Explicit links

Explicit links to applications in the implementation process could be loaded DLL files, DLL files can be unloaded at any time, it is implicit links could not be done, therefore, explicit links have better flexibility, explanatory language more appropriate. But to achieve explicit links to some trouble. LoadLibrary used in the application or provided by the MFC AfxLoadLibrary Explicit will be doing their own dynamic link library tune into the dynamic link library file name that is the function of these two parameters, then reuse GetProcAddress () access to the introduction of function. Since then, as you can use in the application procedures as a function of self-definition as to call this the introduction of the function. In the application before the withdrawal, we should use the MFC FreeLibrary or dynamic link library AfxFreeLibrary release. Below are the links through explicit call Max function in the DLL example.


# 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 ");// loaded dynamic link library MyDll.dll documents;
Max = (pMax) GetProcAddress (hDLL, "Max");
A = Max (4-5);
Printf ( "Comparison of the results of% d \ n", a);
FreeLibrary (hDLL); / / unloading MyDll.dll documents;
)

In the case of the use of keywords typedef type definition, the definition in the same direction and DLL function prototype pointer, and then through LoadLibray () DLL loading of the current application process and return to handle the current DLL files, and then through GetProcAddress ( ) function to access applications into the function pointer, the function call has been completed, the use of FreeLibrary () unloading DLL files. In the compiler before the first step is to copy the DLL to projects, the Windows system directory or directory.

Explicit links use application compiler does not need to use a Lib document. In addition, the use of GetProcAddress () function, you can use MAKEINTRESOURCE () function in the DLL function used directly in the order, such as GetProcAddress (hDLL, "Min") to GetProcAddress (hDLL, MAKEINTRESOURCE (2)) (function Min ( ) DLL is in the order of 2), this function in the DLL calls fast, but to remember the function of the use of serial numbers, otherwise they will be wrong.
Previous:Using VC 6.0 of the three serial communication methods
Next:CUJ: Standard Library: Allocator can do?
User Reviews
Site Search
Related Articles
Recommended article
AD