Establishment and application of simple DLL dynamic link library

xiaoxiao2021-03-06  64

I. Create a DLL menu File -> New-> Other, pop up the new items form, select the DLL Wizard, select the New page, click OK. Pop up the DLL Wizard Form, the default option (C selection, USE VCL selection, Multi Threaded is not available to gray. Click OK.

Enter as follows in the code form: #include #include #pragma HDRSTOP

#pragma argsused

The // DLL function's external interface // int function return value zhuanhuan (int) is the parameter of the function name and function EXTERN "C" __DECLSPEC (DLLEXPORT) INT __STDCALL ZHUANHUAN (Int);

Bool WinApi Dllmain (Hinstance Hinstdll, Dword Fwdreason, LPVOID LPVReServed) {Return 1;} // DLL function implementation - add a value 1, then return INT __STDCALL ZHUANHUAN (INT A) {Int B; b = a 1; Return B;} After saving, press F9 to compile. After compiling, generate a project1.dll file in the directory.

2. Application DLL

Create an application, 1 Button on the form, 2 EDIT controls. To implement a number in Edit1, click the Button button, then call the DLL function to return to the Edit2 control. First save the project, After the above-compiled project1.dll copied to the current directory. Double-click the Button1 control, enter the following code in the code form: // ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ -------------------------------------------------- ----- void __fastcall tform1 :: button1click (TOBJECT * Sender) {INT i = strt (edit1-> text); hinstance ins; INS = loadingLibrary ("project1.dll"); if (INS == null) { throw "Can not load library!";} else {typedef __stdcall (* pZhuanHuan) (int); // defined data type pZhuanHuan zhuanHuan; zhuanHuan = (pZhuanHuan) GetProcAddress (ins, "zhuanHuan"); if (zhuanHuan == NULL) {freeelibrary (ins); throw "can't load address!";} Else {edit2-> text = zhuanhuan (i);}}}} (! Freelibrary (ins)) {throw "can't free!" ;}} // --------------------------------------------- ------------------------------ Run!

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

New Post(0)