A DLL production is generally divided into the following steps:
1 Write a process or function in a DLL engineering
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 transmission
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.
Initialization and exit cleaning of three DLL [If you need to initialize and exit cleanup]
1 DllProc [Sysutils Unit A 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;
There are four types of dwreason parameters:
DLL_PROCESS_ATTACH: When the process enters
DLL_PROCESS_DETACH process exits
DLL_THREAD_ATTACH thread
DLL_THREAD_DETACH thread exits
Write in the initialization section:
DllProc: = @dllenterpoint;
DLLLENTERPOINT (DLL_PROCESS_ATTACH);
2 If there is a TDCOMConnection component on the Form, you will write a Coinitialize (NIL) when you initialize;
3 must guarantee DCOMCONNECTION.CONNECTED: = FALSE, 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 is not connected to 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 call static load
1 client function sound name:
1) Sensitive is sensitive.
2) Like the declaration in DLL.
Such as: showform (form: tform); far; external'yproject_dll.dll ';
3) The parameter type passing during call is preferably the same as 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 are just a pointer to the process type, you know what is going on. Such as:
Type
MyPointer = procedure (form: tform); FAR; External
VAR
Hinst: thandle;
Showform: mypointer;
Begin
Hinst: = 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 TForm in DLL
1 In your Form Uses to DLL, your form is used by the associated unit to be Uses [this is the most troublesome, because your form is USES, Many special units or functions] 2 Pass an Application parameter Use it to build FORM.
Eight build a TMDICHILDFORM in DLL
1 DLL MDIFORM.FormStyle is not available for Fmmdichild.
2 Write the following two sentences after Createform:
Function ShowForm (Mainform: TForm): Integer; stdcall
VAR
FORM1: TFORM1;
PTR: PLONGINT;
Begin
PTR: = @ (Application.mainform); // Save the DLL's mainform handle and no need to release it, just replace it.
PTR ^: = longint (mainform); // replaces the MAINFORM of the MAINFORM with the Motor. 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 attribute
Form1: = tform1.create (mainform); // established with parameters
END;
Remarks: Parameters are the Application.mainform for the Motor.
Nine examples:
DLL source code:
Library Project2;
Uses
SYSUTILS,
Classes,
Dialogs,
Forms,
Unit2 in 'unit2.pas' {form2};
{$ R * .res}
VAR
CCC: PCHAR;
Procedure OpenForm (Mainform: TForm); stdcall;
VAR
FORM1: TFORM1;
PTR: PLONGINT;
Begin
PTR: = @ (Application.mainform);
PTR ^: = longint (mainform);
Form1: = TFORM1.CREATE (MainForm);
END;
Procedure InputCCC (TEXT: PCHAR); STDCALL;
Begin
CCC: = TEXT;
END;
PROCEDURE SHOWCCC; stdcall;
Begin
SHOWMESSAGE (String (CCC));
END;
Exports
OpenForm;
Inputccc,
SHOWCCC;
Begin
End.
Calling the source code:
Unit unit1;
Interface
Uses
Windows, Messages, Sysutils, Classes, Graphics, Controls, Forms, Dialogs,
STDCTRLS;
Type
TFORM1 = Class (TFORM)
Button1: tbutton;
Button2: tbutton;
EDIT1: TEDIT;
Procedure Button1Click (Sender: TOBJECT);
Procedure Button2Click (Sender: TOBJECT);
Private
{Private Declarations}
public
{Public declarations}
END;
VAR
FORM1: 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);
VAR
TEXT: PCHAR;
Begin
Text: = pchar (edit1.text);
// OpenForm (Application.Mainform); // To adjust MDICHILD
InputCCC (Text); // Whether the global variable in the experimental DLL is shared between various applications
END;
Procedure TFORM1.BUTTON2CLICK (Sender: TOBJECT);
Begin
Showccc; // Here, the global variable in the Windows 32-bit application DLL is also in the application address space, and the 16-bit application may vary, no experiments.
END;