Routine detail dynamic link library

zhaozj2021-02-11  180

Routine detail dynamic link library BULL77

Dynamic Link Library (DLL, That is Dynamic-Link Library) is an executable module, but it does not have its own stack, must be in calling dynamic link library functions

Run in the environment. Dynamic Link Library can not only be used as a run module, including function code, and can include any data or resources other than programs (bitmap, icon)

and many more). The dynamic link library is to provide functions or resources to the application.

Dynamic links and static links are relatively vertical. When the static link, the executable program contains the code accessed, and the user occupies the space occupied by the executable.

Big; but running, there is no need for other modules to support, so that the speed is relatively fast. Dynamic links do not include dynamically access function code, only

Its reference and other modules (DLLs) are required for support, and the speed is relatively slow.

The dynamic link library is an extremely important technology for Windows. It enables developers to make it easy to flexibly implement large programs by writing a dynamic link library.

Send, expand the operating system according to your own wishes.

The following example describes how to define functions, resources, and classes, and how to use dynamic link libraries in the dynamic link library, resources,

class.

First, the definition and use of functions:

first step:

Run AppWizard, define the project name MyDLL, select the MFC AppWizard (DLL) instead of MFC AppWizards (Exe).

Step 2:

In this example, only one appwizard screen appears, select the MFC Extension DLL (MFC Extension DLL), click

Finish generation project.

third step:

Click New in the file, select C / C Header File, enter the DLL in File Name, click OK, create a dll.h. Enter EXTERN "C" __DECLSPEC (DLLEXPORT)

INT FUN (INT X1, INT X2, INT X);, save.

the fourth step:

Click New in File, select C Source File, enter the DLL in File Name, click OK, create a dll.cpp. enter

#include "stdafx.h" #include "dll.h" extern "c" __declspec (dllexport) int Fun (int X1, int x2, int x3) {RETURN X1 X2 X3;}

Compile MYDLL.DLL and MYDLL.LIB.

the fifth step:

Select New in Add to Project in Project, regenerate a project, select MFC AppWizards (Exe), the project is named mydlltest, select Single

Document, click Finish to generate a new project. Select Project àset Active Project àmydlltest in the menu, set mydlltest to the current activity project

.

Step 6:

Copy ... / mydll / debug / mydll.dll to ../mydllte/debug/ under, copy ... / mydll / debug / mydll.lib to ... / mydlltest / directory.

Step 7:

Add from #endif in MyDLTestView.cpp

Extern "C" __DECLSPEC (DLLIMPORT) INT FUN (INT X1, INT X2, INT X); add code in Void CMYDLTESTVIEW:: ONDRAW (CDC * PDC) as follows:

Void CMYDLTESTVIEW :: OnDraw (CDC * PDC) {cmydlltestdoc * pdoc = getDocument (); assert_valid (pdoc); // Todo: add draw code for native data here int x = fun (1, 2, 3); CSTRING STR; Str.Format ("% D", STR); PDC-> Textout (10, 10, x);}

Step 8:

Right click on the MYDLTEST Files in Workspace, select Add Files to Project, add myDll.dll to the project. Ok, our work is finished, running!

Second, the addition and use of the class:

Generate a new class in MyDLL, changed to class AFX_EXT_CLASS CMYCLASS, which adds yourself in this class and other classes.

And functions, build new DLL and LIB, repeat the sixth step above, use the above method to generate a MyClass.h file in MyDLLTEST, copy MYDLL

All code of MyClass.h to the newly generated myclass.h. In the file that needs to be used in the CMYClass class in the DLL, you can include Myclass.h, then we can travel to other classes.

Use this class like it.

Third, the definition and use of strings

Define string resources in MyDLL to compile new DLLs and libs, repeat the sixth step above, add #define IDS_MYSTR in MyClass.h in MyDLLTEST

1 (Suppose the ID number of the string resources we define is IDS_MYSTR), you can use the word "myclass.h" where you want to add #include "myclass.h".

The string.

CString str2; str2.loadstring (IDS_MYSTR);

Fourth, the definition and use of dialog

Define dialog resources in MyDLL, generate a new class CDLG, change to class AFX_EXT_CLASS CDLG: Public CDialog, like other pairs

The words are the same define the properties and features of the dialog, compile new DLL and lib, repeat the sixth step above, use the above method to generate a DLG.h file in MyDLLTEST

, Copy all the code of DLG.H in MyDLL to the newly generated DLG.H. In the file that needs to be used in the DLL, you can include DLG.H in the file, then we can

Use this dialog in the dialog.

CDLG DLG; DLG.DOMODAL ();

Ok, all our functions here are all realized, so I'm running!

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

New Post(0)