阅读 138

Delphi封装类到DLL

因为个人需要研究了一下封装类到DLL。把他发表出来

用Delphi封装类到DLL。

 

一个公共单元

unit ITest;

interface

type
  IT = interface
    function GetString:string;
    procedure ShowMsg(p:PChar);
    procedure Msg;
  end;

implementation

end.

类单元,这个写在DLL里面的

unit UTest;

interface

uses
  SysUtils,
  Windows,
  ITest;

type
  TTest = class(TInterfacedObject,IT)
  private
    i:Integer;
  protected

  public
    constructor Create; //override;
    destructor Destroy; override;
    function GetString:string;
    procedure ShowMsg(p:PChar);
    procedure Msg;
  published

  end;

implementation

constructor TTest.Create;
begin
  i:=0;
end;

destructor TTest.Destroy;
begin
  inherited;
end;

function TTest.GetString:string;
begin
  Result := Test string;
end;

procedure TTest.ShowMsg(p:PChar);
begin
  MessageBox(0,p,Test,MB_OK);
end;

procedure TTest.Msg;
begin
  Inc(i);
  MessageBox(0,Test MessageBox,PChar(IntToStr(i)),MB_OK);
end;

end.

DLL的prj

library Test;

uses
  SysUtils,
  Classes,
  ITest in ITest.pas,
  UTest in UTest.pas;

{$R *.res}

function TestCreate:IT; stdcall;
begin
  Result := TTest.Create;
end;

exports
  TestCreate; //用此初始化

begin
end.

DLL部分就这样了,到EXE部分调用

uses
  ITest;  //引用单元

function TestCreate:IT; stdcall; external Test.dll name TestCreate; //引用DLL函数

//声明作为测试
  private
    AA:IT;
    BB:IT;

procedure TForm1.FormCreate(Sender: TObject);
begin
  AA:= TestCreate;
  BB:= TestCreate;
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
  Button1.Caption := AA.GetString;
end;

procedure TForm1.Button2Click(Sender: TObject);
begin
  AA.ShowMsg(123abc);
end;

procedure TForm1.Button3Click(Sender: TObject);
begin
  AA.Msg;
end;

procedure TForm1.Button4Click(Sender: TObject);
begin
  BB.Msg;
end;

 

 原文:https://www.cnblogs.com/ruguo/archive/2012/08/26/2657821.html

原文:https://www.cnblogs.com/stroll/p/13843518.html

文章分类
代码人生
文章标签
版权声明:本站是系统测试站点,无实际运营。本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 XXXXXXo@163.com 举报,一经查实,本站将立刻删除。
相关推荐