二 參數傳遞
三 DLL的初始化和退出清理[如果需要初始化和退出清理
procedure DllEnterPoint(dwReason: DWORD);far;stdcall;
dwReason參數有四種類型
DLL_PROCESS_ATTACH:進程進入時
DLL_PROCESS_DETACH進程退出時
DLL_THREAD_ATTACH 線程進入時
DLL_THREAD_DETACH 線程退出時
在初始化部分寫:
DLLProc := @DLLEnterPoint;
DllEnterPoint(DLL_PROCESS_ATTACH);
四 全局變量的使用
在widnows
五 調用靜態載入
如
六 調用動態載入
type
mypointer=procedure(form:Tform);Far;external;
var
Hinst:Thandle;
showform:mypointer;
begin
Hinst:=loadlibrary(
showform:=getprocaddress(Hinst
showform(application
Freelibrary(Hinst);
end;
七 在DLL建立一個TForM
八 在DLL中建立一個TMDIChildForM
function ShowForm(mainForm:TForm):integer;stdcall
var
Form
ptr:PLongInt;
begin
ptr:=@(Application
ptr^:=LongInt(mainForm);//用主調程序的mainForm替換DLL的MainForm
//為什麼不直接Application
Form
end;
備注
九 示例
DLL源代碼
library Project
uses
SysUtils
Classes
Dialogs
Forms
Unit
{$R *
var
ccc: Pchar;
procedure OpenForm(mainForm:TForm);stdcall;
var
Form
ptr:PLongInt;
begin
ptr:=@(Application
ptr^:=LongInt(mainForm);
Form
end;
procedure InputCCC(Text: Pchar);stdcall;
begin
ccc := Text;
end;
procedure ShowCCC;stdcall;
begin
ShowMessage(String(ccc));
end;
exports
OpenForm;
InputCCC
ShowCCC;
begin
end
調用方源代碼
unit Unit
interface
uses
Windows
StdCtrls;
type
TForm
Button
Button
Edit
procedure Button
procedure Button
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form
implementation
{$R *
procedure OpenForm(mainForm:TForm);stdcall;External
procedure ShowCCC;stdcall;External
procedure InputCCC(Text: Pchar);stdcall;External
procedure TForm
var
Text: Pchar;
begin
Text := Pchar(Edit
// OpenForm(Application
InputCCC(Text);//為了實驗DLL中的全局變量是否在各個應用程序間共享
end;
procedure TForm
begin
ShowCCC;//這裡表明WINDOWS
end;
From:http://tw.wingwit.com/Article/program/Delphi/201311/24661.html