Writing a dynamic link library DLL
DLL referred to as a dynamic link library, is an important part of the program in Windows. Imagine, a program requires many people to complete development, how is a common law? At this time, we must consider dividing the program into several modules, and each member of the team develops a module. The problem is coming: How do I combine the module into a complete system? Also, the software we have developed needs to be upgraded, how to upgrade? Do you have to re-build the entire project once again to the user? The scientific way to solve these problems is to develop dynamic link library DLLs. Now use the development of mydll.dll dynamic link library as an example, talk about the method of opening a dynamic link library in BCB. 1. Newly established a project: file-new-other ... Select DLL Wizard2 in the New card, save the project as MYDLL.BPR3, write interface code in MyDLL.CPP: ---------- -------------------------------------------------- ---------------
#include
} __ declspec (dllexport) __stdcall Ansistring Aboutme (void) {return "Zeng Brown root is good, you are a big stupid, actually learn to use DLL now! I will tell you half a year ago! The research progress is too slow!";} 4, need attention Yes, when writing a process such as a DLL, you should strive to simply, use a lot of memory allocation, try to design a modular structure as well as a modular structure design method, less use of object-oriented programming methods. 5. Go to Project-Options: Turkey in the Linker page The build with runtime packages in the packages page, press the Release button in the Compiler in the Version Info page, check the include version information in Project, and checks Auto -increment build number, then set up copyright information in it, now you can enter the Project-Build MyDLL to generate two files of MyDLL.DLL and MyDLL.LIB. Two static call dynamic link library DLL
There are two ways to call the DLL, one is static call, the other is dynamic call. Static calls require lib library files and DLL files. When compiled, you need to use the lib file. This lib file is no longer needed, and this dynamic link library has compiled processes, so that in the program This DLL file will be found when you start running. If this DLL file does not exist, then the program is started. Conversely, the dynamic calling DLL is not the case, it only needs a DLL file, the program is running, the program does not need to know if this DLL file is present, only when the program runs to a point, it is necessary to call the DLL file multiple applications. When the DLL, only one instance is generated in memory, so the memory space can be effectively saved, and the system's operating efficiency can be improved. Note that DLL has nothing to do with programming language, as long as the DLL interface specification, many languages can develop an efficient DLL program, other language developed DLLs, can also be called in the BCB. Here is the step of calling the DLL as an example of MyDLL.DLL: 1, copy myDLL.DLL and MYDLL.LIB file into the development project, notice that the application is not required when the application is published. If it is a DLL developed in other languages, in the case where there is no lib file, you can use the Implib.exe tool program to generate a lib file, usage: Implib.exe file name .lib file name .dll2, project-add to project will MYDLL .lib library imports into the project.
If you want to clear the library file from the project, there are two types: A, Project-Remove from Projectb, View-Project Manager3, write program code in the engineer1.cpp: ------------ -------------------------------------------------- ------------- #include
The following functions is actually definition int __stdcall myadd (int, int); int __stdcall (* myadd) (int, int); step 2: Define the module handle, global variable, which is the instance of the DLL file after the DLL file. Hinstance HMYDLL; Step 3: Load the DLL file, and get its handle hmydll = loadLibrary ("mydll.dll"); fourth step: Define function address variables FarProc P; fifth step: Get a function in dynamic link library Memory address p = getProcaddress (HMYDLL, "MyAdd"); Step 6: Force Type Conversion, the acquired function address is forced to convert to function myadd = (int __stdcall (__cdecl *) (int, int, int)) p; seventh Step: Function call n = myadd (10, 20); Eight step eight: Release DLL FREELIBRARY (HMYDLL); following dynamic call mydll.dll functions as an example, explain: ------------ -------------------------------------------------- ------------- #include
The following function is actually defined int __stdcall myadd (int, int); int, int, int); ansistring __stdcall (* about) (void); Step 2: Define the module handle, global variable, it is Example after loading the DLL file Hinstance Hmydll; ------------------------------------------------------------------------------------------------------------------------------------------------------------------------ -----------------------------------__ fastcall tform1 :: tform1 (tcomponent * oowner): TFORM (Owner) {Step 3: Load a DLL file, and get its handle hmydll = loadingLibrary ("mydll.dll");} --------------------- -------------------------------------------------- - void __fastcall tform1 :: button1click (TOBJECT * Sender) {INT N; Step 4: Define Function Address Variable FarProc P; if (HMYDLL! = NULL) {Step 5: Get a function in the dynamic link library Memory address P = getProcaddress (HMYDLL, "myadd"); if (p == null) {showMessage ("Open myAdd () function error!");} Else {Sixth step: Mandatory type conversion, will be acquired Address enforcement to function myadd = (int, int, int, int,)) P; Step 7: Function call n = myAdd (10, 20); showMessage (INTTOSTR (N));}} else {showMessage ("Open Dynamic Link Library MYDLL.DLL Error!");}} --------------------------------- ----------------------------------------- void __f ASTCALL TFORM1 :: FormDestroy (TOBJECT * Sender) {Eight Step: Release DLL Freelibrary (HMYDLL);} --------------------------- ---------------------------------------------- void __fastcall TFORM1 :: Button2Click (TOBJECT * Sender) {FarProc P; if (hmydll! = Null) {p = getProcaddress (HMYDLL, "AboutMe"); if (p == null) {showMessage ("Open the fromoutme () function error! ");} Else {aboutme = (Ansistring __stdcall (__cdecl *) (void)) P; showMessage ());}} else {showMessage (" Open Dynamic Link Library MYDLL.DLL Error! ");