zoukankan      html  css  js  c++  java
  • 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;
  • 相关阅读:
    2018-12-25-dot-net-double-数组转-float-数组
    2018-12-25-dot-net-double-数组转-float-数组
    2019-10-24-dotnet-列表-Linq-的-Take-用法
    2019-10-24-dotnet-列表-Linq-的-Take-用法
    2018-8-10-C#-代码占用的空间
    2018-8-10-C#-代码占用的空间
    2018-4-29-C#-金额转中文大写
    2018-4-29-C#-金额转中文大写
    Java实现 LeetCode 630 课程表 III(大小堆)
    Java实现洛谷 P1072 Hankson 的趣味题
  • 原文地址:https://www.cnblogs.com/leonkin/p/2664509.html
Copyright © 2011-2022 走看看