Delphi 用DLL實(shí)現(xiàn)插件的簡(jiǎn)單實(shí)例
Delphi 用DLL實(shí)現(xiàn)插件的簡(jiǎn)單實(shí)例
這是DLL的代碼
實(shí)現(xiàn)代碼:
library MyDll;
uses
SysUtils,
Dialogs,
Classes;
procedure ShowInfo(info:PChar);stdcall;
begin
ShowMessage('您選擇了【'+info+'】');
end;
function GetCaption:Pchar;
begin
Result := '中國(guó)';
end;
exports ShowInfo,
GetCaption;
{$R *.res}
begin
end.
這是調(diào)用窗體的代碼
本例只使用了一個(gè)DLL,所以當(dāng)有多個(gè)DLL時(shí),需要循環(huán)DLL所在目錄,依次加載DLL
unit Main;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, Menus, ExtCtrls;
type
TShowInfo = procedure (info:PChar);stdcall;
TGetCaption = function : PChar;stdcall;
TForm1 = class(TForm)
Button1: TButton;
Button2: TButton;
MainMenu1: TMainMenu;
Image1: TImage;
procedure Button2Click(Sender: TObject); private
{ Private declarations }
FHandel : THandle; //DLL句柄
FProAddress: Pointer; //DLL中ShowInfo的地址
showinfo: TShowInfo; //為動(dòng)態(tài)加載DLL而設(shè)
procedure LoadPlusIn; //加載插件(DLL)
procedure ItemClick(Sender: TObject); //自定義菜單點(diǎn)擊事件
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
procedure TForm1.Button2Click(Sender: TObject);
begin
LoadPlusIn;
end;
procedure TForm1.ItemClick(Sender: TObject);
begin
@showinfo := FProAddress; //取地址
if @showinfo <> nil then
showinfo(PWideChar(TMenuItem(Sender).Caption)); //執(zhí)行DLL中的ShowInfo
end;
procedure TForm1.LoadPlusIn;
var
getcaption: TGetCaption;
item : TMenuItem;
begin
FHandel := LoadLibrary('MyDll.dll'); //加載
if FHandel = 0 then
begin
ShowMessage('加載失?。?);
Exit;
end
else
begin
@getcaption := GetProcAddress(FHandel,'GetCaption'); //取DLL中GetCaption地址
if @getcaption <> nil then
begin
item := TMenuItem.Create(MainMenu1); //創(chuàng)建一個(gè)菜單
item.Caption := getcaption; //取Caption,即調(diào)用DLL中的GetCaption
FProAddress := GetProcAddress(FHandel,'ShowInfo'); //取得DLL中ShowInfo的地址
item.OnClick := ItemClick; //賦予菜單項(xiàng)的點(diǎn)擊事件
MainMenu1.Items.Add(item); //添加到主菜單
end;
end;
end;
end.
如有疑問請(qǐng)留言或者到本站社區(qū)交流討論,感謝閱讀,希望能幫助到大家,謝謝大家對(duì)本站的支持!
相關(guān)文章
Delphi實(shí)現(xiàn)檢測(cè)并枚舉系統(tǒng)安裝的打印機(jī)的方法
這篇文章主要介紹了Delphi實(shí)現(xiàn)檢測(cè)并枚舉系統(tǒng)安裝的打印機(jī)的方法,需要的朋友可以參考下2014-07-07
Delphi 實(shí)現(xiàn)軟件自動(dòng)升級(jí)的功能
這篇文章主要介紹了Delphi 實(shí)現(xiàn)軟件自動(dòng)升級(jí)的功能的相關(guān)資料,希望通過本文能幫助到大家實(shí)現(xiàn)這樣的功能,需要的朋友可以參考下2017-09-09
Delphi實(shí)現(xiàn)獲取句柄并發(fā)送消息的方法
這篇文章主要介紹了Delphi實(shí)現(xiàn)獲取句柄并發(fā)送消息的方法,需要的朋友可以參考下2014-07-07
delphi7連接mysql5的實(shí)現(xiàn)方法
這篇文章主要介紹了delphi7連接mysql5的實(shí)現(xiàn)方法,需要的朋友可以參考下2014-07-07
Delphi 根據(jù)字符串找到函數(shù)并執(zhí)行的實(shí)例
這篇文章主要介紹了Delphi 根據(jù)字符串找到函數(shù)并執(zhí)行的實(shí)例的相關(guān)資料,希望通過本能幫助到大家實(shí)現(xiàn)這樣的功能,需要的朋友可以參考下2017-09-09

