DLL technology notes (1)

xiaoxiao2021-03-06  20

DLL technical notes

Due to the learning of the DLL, there is no example, it is difficult to understand the essence of the DLL. Now I want to do an example to introduce: First, add a function in the DLL: Fun (double a, double b) // I am too lazy here, Participate from others (many people, thank you here) Come over: // 1, static loading DLL method: 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 to generate 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); saved.

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, 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 as 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 to the void cmydlltestview :: 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", x); PDC-> Textout (10, 10, str);}

Step 8:

Right click on the MYDLTEST Files in Workspace, select Add Files To Project, add myDll.lib to the project. Ok, our work is finished, running! /// 2, dynamically loaded DLL: starting from the sixth step:

Add the void CMydlltestView :: OnDraw (CDC * pDC) code is as follows: void CMydlltestView :: OnDraw (CDC * pDC) {CMydlltestDoc * pDoc = GetDocument (); ASSERT_VALID (pDoc); // TODO: add draw code for native data Hereint X; TypedEf Int (* PADD) (INT X1, INT X2, INT X3); Hinstance HDLL; PADD Add; HDD; HDLL = LoadLibrary ("MyDLL.DLL"); add = (padd) getProcadDress (HDLL, "Fun") ; x = add (2, 6, 5); CString Str; str.format ("% d", x); PDC-> Textout (10, 10, str);

}

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

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

New Post(0)