Delphi 用DLL實現插件的簡單實例
Delphi 用DLL實現插件的簡單實例
這是DLL的代碼
實現代碼:
library MyDll; uses SysUtils, Dialogs, Classes; procedure ShowInfo(info:PChar);stdcall; begin ShowMessage('您選擇了【'+info+'】'); end; function GetCaption:Pchar; begin Result := '中國'; end; exports ShowInfo, GetCaption; {$R *.res} begin end.
這是調用窗體的代碼
本例只使用了一個DLL,所以當有多個DLL時,需要循環(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; //為動態(tài)加載DLL而設 procedure LoadPlusIn; //加載插件(DLL) procedure ItemClick(Sender: TObject); //自定義菜單點擊事件 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)建一個菜單 item.Caption := getcaption; //取Caption,即調用DLL中的GetCaption FProAddress := GetProcAddress(FHandel,'ShowInfo'); //取得DLL中ShowInfo的地址 item.OnClick := ItemClick; //賦予菜單項的點擊事件 MainMenu1.Items.Add(item); //添加到主菜單 end; end; end; end.
如有疑問請留言或者到本站社區(qū)交流討論,感謝閱讀,希望能幫助到大家,謝謝大家對本站的支持!
上一篇:Delphi實現截屏存盤的方法
欄 目:Delphi
下一篇:Delphi解析FTP地址的方法
本文標題:Delphi 用DLL實現插件的簡單實例
本文地址:http://mengdiqiu.com.cn/a1/Delphi/8603.html
您可能感興趣的文章
- 01-10在Delphi實現在數據庫中存取圖像的圖文演示無錯
- 01-10delphi建立、讀取、存貯INI文件的方法《三》
- 01-10Delphi Command模式
- 01-10delphi 正弦曲線圖
- 01-10delphi建立、讀取、存貯INI文件的方法《二》
- 01-10插件管理框架 for Delphi(二)
- 01-10插件管理框架 for Delphi(一)
- 01-10Delphi中判斷文件是否為文本文件的函數
- 01-10delphi中一個值得大家來考慮的DLL問題
- 01-10初探Delphi中的插件編程


閱讀排行
本欄相關
- 01-10在Delphi實現在數據庫中存取圖像的圖
- 01-10delphi建立、讀取、存貯INI文件的方法
- 01-10delphi 正弦曲線圖
- 01-10Delphi Command模式
- 01-10delphi建立、讀取、存貯INI文件的方法
- 01-10插件管理框架 for Delphi(二)
- 01-10插件管理框架 for Delphi(一)
- 01-10Delphi中判斷文件是否為文本文件的函
- 01-10delphi中一個值得大家來考慮的DLL問題
- 01-10初探Delphi中的插件編程
隨機閱讀
- 01-10SublimeText編譯C開發(fā)環(huán)境設置
- 01-10C#中split用法實例總結
- 08-05dedecms(織夢)副欄目數量限制代碼修改
- 01-11Mac OSX 打開原生自帶讀寫NTFS功能(圖文
- 04-02jquery與jsp,用jquery
- 01-10delphi制作wav文件的方法
- 01-11ajax實現頁面的局部加載
- 01-10使用C語言求解撲克牌的順子及n個骰子
- 08-05DEDE織夢data目錄下的sessions文件夾有什
- 08-05織夢dedecms什么時候用欄目交叉功能?