一.函數過程的寫法:
library FIRSTDLL;
uses
SysUtils,
Classes;
{$R *.RES}
// 1.定義函數具體過程和輸出接口方式
// --------------------------------
// 函數 1
// 功能:事數據3倍放大函數
// --------------------------------
function BBnToSSnn(SourceResult:Integer):Integer;stdCall;
begin
if SourceResult>0 then
Result:=SourceResult+3 //結果存放於Result
else
Result:=SourceResult;
end;
exports
BBnToSSnn; //2.函數輸出口定義
end.
二.在DLL中創建Form
=======================
1.一步,創建DLL工程,及加入設置好的Form
{$R *.RES}
exports
ShowPerSN;//2.函數輸出口定義
begin
end.
2. 在DLL設定的Form的設置
===========================================
unit MGRPERFM;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
ComCtrls, ToolWin, ImgList;
type
TFormPERSON = class(TForm)
private
{ Private declarations }
public
{ Public declarations }
end;
//些處的變量不再用,給其改個地方,如下(改變之一)
//var
// FormPERSON: TFormPERSON;
{宣布Form函數出口}//改變之二
function ShowPerSN(AHandle: THandle; ACaption: String):BOOL; StdCall;
implementation
{$R *.DFM}
//函數據過程定義
function ShowPerSN(AHandle: THandle; ACaption: String):BOOL;
var
FormPERSON: TFormPERSON; //定義窗體類(上面的放到了此處)
begin
//拷貝應用程式句柄給DLL的應有程式對象
Application.Handle := AHandle;
FormPERSON := TFormPERSON.Create(Application);//創建控件TForm
try
FormPERSON.Caption := ACaption;
FormPERSON.ShowModal;//顯示此Form
Result := False; //反回成功值
finally
FormPERSON.Free;
end;
end;
三.DLL中函數及窗體的調用
==========================
1.調用方法一
--------------
implementation //在此的下方寫明調用函數的DLL
{$R *.DFM}
//DLL內函數調用
function BBnToSSnn(SourceResult:Integer):Integer;
StdCall external 'FIRSTDLL.DLL';
........
2.調用方法二
==============
type //在此創建一個函數類
// 1 -------------------------------
TShowPerSN = function (AHandle: THandle; ACaption: String): BOOL; StdCall;
EDLLLoadError = class(Exception);//同時分創建一個出錯記錄類
// 1 -------------------------------
TMAINCLTR = class(TForm) //這裡不變,系統自動生成
......
procedure TMAINCLTR.ToolButton1Click(Sender: TObject);
var //按鈕的調用事件:調用過程
LibHandle: THandle;
ShowPerSN: TShowPerSN;
begin
Application.Title:='人力資源管理系統DLL文件測試程式';
{ Attempt to load the DLL 嘗試裝入DLL文件}
LibHandle := LoadLibrary('MGRPERSN.DLL');
try
if LibHandle = 0 then
raise EDLLLoadError.Create('無法成功裝入MGRPERSN.DLL');
@ShowPerSN := GetProcAddress(LibHandle, 'ShowPerSN');
if not (@ShowPerSN = nil) then
ShowPerSN(Application.Handle, '人事資料管理')//呼叫出窗體
else
RaiseLastWin32Error;
finally
FreeLibrary(LibHandle); // Unload the DLL.
end;
end;
============== END ==================