DLL文件在Delphi的創建及調用
發表時間:2023-08-01 來源:明輝站整理相關軟件相關文章人氣:
[摘要]現時系統的開發,多數都在幾人以上的組合,工合作方式開發,這樣也方便系統的快速開發目的。而DLL的方法最為方便。我現整理了我的一些這方面資料,希望能幫助一些有需要的同行。一.函數過程的寫法:libra...
現時系統的開發,多數都在幾人以上的組合,工合作方式開發,這樣也方便系統的快速開發目的。
而DLL的方法最為方便。我現整理了我的一些這方面資料,希望能幫助一些有需要的同行。
一.函數過程的寫法:
library FIRSTDLL;
uses
SysUtils,
Classes;
{$R *.RES}
// 1.定義函數具體過程和輸出接口方式
// --------------------------------
// 函數 1
// 功能:事數據3倍放大函數
// --------------------------------
function PenniesToSoins(SourceResult:Integer):Integer;stdCall;
begin
if SourceResult>0 then
Result:=SourceResult*3 //結果存放于Result
else
Result:=SourceResult;
end;
exports
PenniesToSoins; //2.函數輸出口定義
end.
==
==
二.在DLL中創建Form
=======================
1.一步,創建DLL工程,及加入設置好的Form
library MGRPERSN;
uses
SysUtils,
Classes,
MGRPERFM in 'MGRPERFM.pas' {FormPERSON};//1.Form的代碼(與一般的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;
{ Declare the export function 宣布Form函數出口}//改變之二
function ShowPerSN(AHandle: THandle; ACaption: String):BOOL; StdCall;
implementation
{$R *.DFM}
//函數據過程定義
function ShowPerSN(AHandle: THandle; ACaption: String):BOOL;
var
FormPERSON: TFormPERSON; //定義窗體類(上面的放到了此處)
begin
// Copy application handle to DLL's TApplication object
//拷貝應用程式句柄給DLL的應有程式對象
Application.Handle := AHandle;
FormPERSON := TFormPERSON.Create(Application);//創建控件TForm
try
FormPERSON.Caption := ACaption;
FormPERSON.ShowModal;//顯示此Form
// Pass the date back in Result
Result := False; //反回成功值
finally
FormPERSON.Free;
end;
end;
三.DLL中函數及窗體的調用
==========================
1.調用方法一
--------------
implementation //在此的下方寫明調用函數的DLL
{$R *.DFM}
//DLL內函數調用
function PenniesToSoins(SourceResult:Integer):Integer;
StdCall external 'FIRSTDLL.DLL';
........
2.調用方法二
==============
type //在此創建一個函數類
// 1 -------------------------------
{ First, define a procedural data type, this should reflect the
procedure that is exported from the DLL. }
{ Create a new exception class to reflect a failed DLL load }
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('Unable to Load DLL(無法成功裝入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 ==================