delphi中dll综合运用的例子

  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。

delphi中dll综合运用的例子(动态加载插件)

2008年01月15日星期二 10:59

1,新建dll客户端模块

---------------dll工程文件PlugIns.dll------------------------------- library PlugIns;

{ Important note about DLL memory management: ShareMem must be the first unit in your library's USES clause AND your project's (select Project-View Source) USES clause if your DLL exports any procedures or functions that pass strings as parameters or function results. This applies to all strings passed to and from your DLL--even those that are nested in records and classes. ShareMem is the interface unit to the BORLNDMM.DLL shared memory manager, which must be deployed along with your DLL. To avoid using BORLNDMM.DLL, pass string information using PChar or ShortString parameters. }

uses

SysUtils,

Classes,

PlugInFrm in 'PlugInFrm.pas' {FrmPlugIns};

{$R *.res}

//输出接口函数

exports

ShowDLLForm,GetCaption;

begin

end.

-------------新建模块PlugInFrm.pas的窗体文件---------------

unit PlugInFrm;

interface

uses

Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,

Dialogs, StdCtrls;

type

TFrmPlugIns = class(TForm)

Label1: TLabel;

private

{ Private declarations }

public

{ Public declarations }

end;

//定义ShowDLLForm,用于打开本窗体

function ShowDLLForm(AHandle: THandle; ACaption: string): Boolean; Stdcall;

//输出标题

function GetCaption: Pchar; stdcall;

implementation

{$R *.dfm}

//输出标题

function GetCaption: Pchar; stdcall;

begin

Result := '插件演示NO1';

end;

//打开本窗体

function ShowDLLForm(AHandle: THandle; ACaption: string): Boolean; var

DLL_Form: TfrmPlugins;

begin

result := true;

try

application.Handle := AHandle; //传递应用程序地址

DLL_Form := TFrmPlugins.Create(Application);//创建窗体

try

DLL_Form.caption := Acaption;//给窗体标题赋值

DLL_Form.ShowModal; //模式显示窗体

finally

DLL_Form.Free;

end;

except

result := false;

end;

end;

end.

2,新建工程主模块

-------------------新建主窗体文件MainFrm.pas--------------

unit MainFrm;

interface

uses

Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,

Dialogs, Menus;

type

TFrmMain = class(TForm)

MainMenu1: TMainMenu;

N1: TMenuItem;

N2: TMenuItem;

N_Plugins: TMenuItem;

procedure FormCreate(Sender: TObject);

procedure FormDestroy(Sender: TObject);

private

{ Private declarations }

FForm: TForm;

procedure LoadPlugIns; //初始化插件,也就装载插件,并在菜单提供调用

procedure PlugInsClick(Sender: TObject); //插件菜单点击事件

procedure FreePlugIns; //释放插件

public

{ Public declarations }

end;

var

FrmMain: TFrmMain;

implementation

{$R *.dfm}

type

//定义接口函数类型

TShowDLLForm = function(AHandle: THandle; ACaption: string): Boolean; Stdcall;

TGetCaption = function: Pchar; StdCall;

EDLLLoadError = class(Exception);

//定义TTestPlugIn类,存放caption,Address,call等信息

TTestPlugIn = class

相关文档
最新文档