Method for making DLLs with Delphi

xiaoxiao2021-03-06  67

Method for making DLLs with Delphi

One DLL's Making General Step Two Parameters Transfer of Three DLL Initialization and Exit Cleaning [If you need to initialize and exit cleaning] Four ELT variables The use of the four calls Static load six calls Dynamic load seven in DLL build a TFORM eight in the DLL established A TMDICHILDFORM Nine Example: Ten Delphi made of DLLs and other languages ​​in other languages, in other words: Eleventh Related Information

One DLL is generally divided into the following steps: 1 Write a process or function in a DLL engineer 2 to write an exports keyword, the name of the process under which. Do not write parameters and call suffixes. Two-Parameter Pass 1 Parameter Type is best consistent with the parameter type of Window C . Do not use Delphi's data type. 2 It is best to return value [even a process], to report success or fail, or status. The return value of success or failure is preferably 1 [success] or 0 [failed]. In one sentence, with Windows C is compatible with WINDOWS C . 3 State the suffix with the stdcall. 4 is best sensitive. 5 None must use a FAR to call the suffix, which is just compatible with Windows 16-bit programs.

The initialization and exit cleaning of three DLL [If you need to initialize and exit cleanup] 1 DLLPROC [Sysutils unit's Pointer] is the entry of the DLL. Here you can replace its entry with your function. But your function must meet the following requirements [actually is a callback function]. As follows: procedure DllEnterPoint (dwReason: DWORD); far; stdcall; dwReason parameters There are four types: DLL_PROCESS_ATTACH: when entering the initialization process when DLL_PROCESS_DETACH partial write process to exit when DLL_THREAD_ATTACH thread enters DLL_THREAD_DETACH thread exits: DLLProc: = @DLLEnterPoint; DllEnterPoint (DLL_PROCESS_ATTACH; 2 If there is a TDCOMConnection component on the Form, you write a Coinitialize (NIL) when you initialize; 3 must guarantee dcomconnection.connected: = false when exiting, and the data set is closed. Otherwise, the address is wrong.

Four global variables In the Widnows 32-bit program, the address space of the two applications has not contacted each other. Although the DLL is in memory, the variable is in the address space of each process, so you can't reach data transfer between two applications with a global variable of the DLL unless you use a memory image file.

Five calls Static load 1 client function sound name: 1) Size sensitive. 2) Like the declaration in DLL. Such as: showform (form: tform); far; external'yproject_dll.dll '; 3) The parameter type passed during the call is preferably like Windows C . 4) When the call is called, the DLL must be in the Windows search path, the order is: current directory; Path path; windows; widows / system; windows / ssystem32;

Six calls Dynamic load 1 Establish a process type [If you have a pointer to the process type, you know what is going on. Such as: typemypointer = procedure (form: tform); far; external; varhinst: thandle; showform: mypoint; beginhinst: = loadingLibrary ('YPROJECT_DLL'); // Load A DLL, find it by file name. Showform: = getProcaddress (Hinst, 'ShowForm'); // Find by the function name, sensitive. If you know the essence of the automation object is clear. ShowForm (Application.mainform); / / Find the function portal pointer to call. Freelibrary (hinst); end; seven build a TFORM1 in DLL to put your form users to DLL, your form is associated with the associated unit to enter [This is the most troublesome, because your form may be Uses Many special units or functions] 2 pass an Application parameter and build FORM.

Eight MDIForm.FormStyle Dll establishment of a TMDIChildForM1 in the DLL is not written fmMDIChild.2 following two sentences after CreateForm: function ShowForm (mainForm: TForm): integer; stdcallvarForm1: TForm1; ptr: PLongInt; beginptr: = @ (Application .Mainform); // first save the DLL's mainform handle, no need to release, just replace the PTR ^: = longint (mainform); // replace the DLL of the DLL with the main MAINFORM. MainForm is a special Window that specifically manages the Forms resources in Application./ Why is not directly application.mainform: = mainform, because Application.MainForm is read-only property form1: = tForm1.create (mainform); // Establish parameters End; Remarks: Parameters are the Application.mainform for the Motor.

Nine Example: DLL Source Code: Library Project2;

UsessysUtils, classes, dialogs, forms, unit2 in 'unit2.pas' {form2};

{$ R * .res} VARCCC: PCHAR;

Procedure OpenForm; stdcall; var: plongint; beginptr: = @ (Application.mainform); ptr ^: = longint (mainform); Form1: = tForm1.create (mainform); END;

Procedure INPUTCCC (TEXT: PCHAR); STDCALL; Beginccc: = TEXT;

Procedure showccc; stdcall; beginshowMessage (String (CCC));

ExportsopenForm; InputCCC, Showccc; Beginend.

Calling Source Code: Unit Unit1;

Interface

Useswindows, Messages, Sysutils, Classes, Graphics, Controls, Forms, Dialogs, Stdctrls

typeTForm1 = class (TForm) Button1: TButton; Button2: TButton; Edit1: TEdit; procedure Button1Click (Sender: TObject); procedure Button2Click (Sender: TObject); private {Private declarations} public {Public declarations} end; varForm1: TForm1;

IMPLEMentation

{$ R * .DFM} procedure OpenForm (mainForm: TForm); stdcall; External'project2.dll '; procedure ShowCCC; stdcall; External'project2.dll'; procedure InputCCC (Text: Pchar); stdcall; External'project2. DLL ';

Procedure TForm1.Button1Click (Sender: Tobject); VARText: pchar; begintext: = pchar (edit1.text); // OpenForm (Application.Mainform); // To adjust MDICHILDINPUTCCCC (TEX); // In order to experiment DLL Whether the variable shares end in each application;

Procedure TForm1.Button2Click (Sender: TOBJECT); beginshowcc; // Here, the global variable in the Windows 32-bit application DLL is also in the application address space, and the 16-bit application may be different, no experiments. END;

Ten Delphi Mixed Programming Sometimes Issues: 1 and PowerBuilder Hybrid Programming In Defining Unregulated Dynamic Arrse, the old unrecover address is wrong when the function exits cleansing the stack, the reason is unknown, probably compiled with PB The principle is related to the principle, even if PB is compiled into binary code.

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

New Post(0)