Delphi 用DLL实现插件的简单实例

所属分类: 软件编程 / Delphi 阅读数: 167
收藏 0 赞 0 分享

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时,需要循环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;  //为动态加载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)); //执行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);  //创建一个菜单 
   item.Caption := getcaption;       //取Caption,即调用DLL中的GetCaption 
   FProAddress := GetProcAddress(FHandel,'ShowInfo'); //取得DLL中ShowInfo的地址 
   item.OnClick := ItemClick;       //赋予菜单项的点击事件 
   MainMenu1.Items.Add(item);       //添加到主菜单 
  end; 
 
 end; 
end; 
 
end. 

如有疑问请留言或者到本站社区交流讨论,感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!

更多精彩内容其他人还在看

Delphi实现毫秒级别的倒计时实例代码

这篇文章主要介绍了Delphi实现毫秒级别的倒计时实例代码,需要的朋友可以参考下
收藏 0 赞 0 分享

Delphi中对时间操作方法汇总

这篇文章主要介绍了Delphi中对时间操作方法汇总,可以对Delphi的时间操作有一个更加深入的了解,需要的朋友可以参考下
收藏 0 赞 0 分享

Delphi实现碰撞球体完整实例代码

这篇文章主要介绍了Delphi实现碰撞球体完整实例代码,通过该实例,读者可以完整的了解一个Delphi项目的创建过程,加深对Delphi运行原理的了解,需要的朋友可以参考下
收藏 0 赞 0 分享

Delphi实现读取系统时间与日期完整实例

这篇文章主要介绍了Delphi实现读取系统时间与日期完整实例,需要的朋友可以参考下
收藏 0 赞 0 分享

Delphi常用关键字用法详解

这篇文章主要介绍了Delphi常用关键字用法,包括了各个常用的关键字及其详细用法,需要的朋友可以参考下
收藏 0 赞 0 分享

Delphi实例演示Rect、Bounds生成TRect的区别

这篇文章主要介绍了Delphi实例演示Rect、Bounds生成TRect的区别,需要的朋友可以参考下
收藏 0 赞 0 分享

Delphi之Pascal语言中的关键字及保留字汇总

这篇文章主要介绍了Delphi之Pascal语言中的关键字及保留字汇总,需要的朋友可以参考下
收藏 0 赞 0 分享

Delphi实现图片滚动切换的完整实例代码

这篇文章主要介绍了Delphi实现图片滚动切换的完整实例代码,通过本实例,读者可以进一步掌握Delphi控件及图形处理的技巧,需要的朋友可以参考下
收藏 0 赞 0 分享

Delphi实现图像文本旋转特效完整实例代码

这篇文章主要介绍了Delphi实现图像文本旋转特效完整实例代码,对于帮助读者进一步理解Delphi图形及文字特效的处理有一定的借鉴价值,需要的朋友可以参考下
收藏 0 赞 0 分享

Delphi实现限定软件使用时间的方法

这篇文章主要介绍了Delphi实现限定软件使用时间的方法,商业软件开发中非常实用的功能,需要的朋友可以参考下
收藏 0 赞 0 分享
查看更多