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

所属分类: 软件编程 / Delphi 阅读数: 99
收藏 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 分享

Delphi7中群发Email邮件的方法

这篇文章主要介绍了Delphi7中群发Email邮件的方法,涉及邮件服务器软件的使用,电子邮件的判断与发送功能的实现,是非常实用的技巧,需要的朋友可以参考下
收藏 0 赞 0 分享

delphi字符串分隔函数用法实例

这篇文章主要介绍了delphi字符串分隔函数用法,通过自定义函数SeparateTerms2实现将字符串分割后存入字符串列表的功能,具有一定的实用价值,需要的朋友可以参考下
收藏 0 赞 0 分享

delphi实现将BMP格式图形转化为JPG格式图形的方法

这篇文章主要介绍了delphi实现将BMP格式图形转化为JPG格式图形的方法,通过简单的自定义函数调用系统自带的changefileext及SaveToFile等方法来实现格式转换功能,是非常实用的技巧,需要的朋友可以参考下
收藏 0 赞 0 分享

Delphi解析FTP地址的方法

这篇文章主要介绍了Delphi解析FTP地址的方法,涉及Delphi解析FTP的相关技巧,需要的朋友可以参考下
收藏 0 赞 0 分享

Delphi实现窗体感知鼠标滑过并自动隐藏与显示窗口的方法

这篇文章主要介绍了Delphi实现窗体感知鼠标滑过并自动隐藏与显示窗口的方法,涉及Delphi操作窗口及鼠标事件的技巧,需要的朋友可以参考下
收藏 0 赞 0 分享

Delphi实现截屏存盘的方法

这篇文章主要介绍了Delphi实现截屏存盘的方法,涉及Delphi图片操作的相关技巧,需要的朋友可以参考下
收藏 0 赞 0 分享

Delphi基本图像处理方法汇总

这篇文章主要介绍了Delphi基本图像处理方法,实例汇总了Delphi操作图像实现浮雕、反色、模糊、翻转等常用效果的方法,非常具有实用价值,需要的朋友可以参考下
收藏 0 赞 0 分享

Windows API GetLastError()函数返回值含义解释

这篇文章主要介绍了Windows API GetLastError()函数返回值含义解释,本文罗列了所有错误代码及中文注释,需要的朋友可以参考下
收藏 0 赞 0 分享

delphi制作wav文件的方法

这篇文章主要介绍了delphi制作wav文件的方法,涉及Delphi操作多媒体文件的相关技巧,需要的朋友可以参考下
收藏 0 赞 0 分享
查看更多