Method for making DLLs with Delphi

xiaoxiao2021-03-06  65

Method of DLL Method A DLL Method A DLL Production General Step Two Parameters Transfer 3 DLL Initialization and Exit Cleaning [If you need to initialize and exit cleanup] Four ELT variables The use of five calls Static load six call dynamic load seven in DLL build one TFORM Eight Create a TMDICHILDFORM Nine Example: Ten Delphi Produced DLL and other language mixed programming problems: Eleventh Related Information A DLL production generally divided into the following steps: 1 Write a process in a DLL project Or function 2 Write an exports keyword, the name of the process under which the process is written. 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]. One sentence, 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: the current directory; Path path; Windows; Widows / System; Windows / SSYSTEM32; Six call dynamic load 1 Create a process type [If you have the variable of the process type Just a clear words of a pointer, 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 Transfer an Application parameter, build form. Eight created a TMDICHILDM1 DLL in the DLL. MDIFORM.FormStyle is not used for Fmmdichild.2. Next sentences after Createform: Function ShowForm (Mainform: TForm) : Integer; stdcallvarform1: tform1; ptr: plongint; beginptr: = @ (Application.mainform); // first put the DLL's mainform handle, no need to release, just replace PTR ^: = longint (Mainform) // Replace the MAINFORM 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; Note: Parameters are the Application.Mainform nine samples of the Motor: DLL Source code: library project2; usessysutils, classes, dialogs, forms, unit2 in 'unit2.pas' {form2}; {$ r * .res} VARCCC : Pchar; mainform: tform); stdcall; varform1: tform1; ptr: plongint; beginptr: = @ (Application.mainform); ptr ^: = longint (mainform); Form1: = TFORM1.CREATE (MainForm); end; procedure InputCCC (Text: Pchar); stdcall; beginccc: = Text; end; procedure ShowCCC; stdcall; beginShowMessage (String (ccc)); end; exportsOpenForm; InputCCC, ShowCCC; beginend caller source:. 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}}} end; varform1: tform1; import physical ation {$ 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); // For experimentation Whether global variables in the DLL share end; procedure tform1.button2click (sender: TOBJECT); beginshowccc; // Here, the global variable in the Windows 32-bit application DLL is also in the application address space, 16 bits The application may be different, no experiments.

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

New Post(0)