zoukankan      html  css  js  c++  java
  • Delphi调用c++写的dll (me)

    unit Unit1;
    
    interface
    
    uses
      Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
      Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls;
    
    type
      TForm1 = class(TForm)
        btn1: TButton;
        btn2: TButton;
        procedure btn1Click(Sender: TObject);
        procedure btn2Click(Sender: TObject);
    
      private
        { Private declarations }
      public
        { Public declarations }
      end;
    
    var
      Form1: TForm1;
    
      function add(a:integer;b:integer):integer;cdecl;external 'mydllForDelphi.dll' ;
      function sub(a:integer;b:integer):integer;cdecl;external 'mydllForDelphi.dll' ;
    
    implementation
    
    {$R *.dfm}
    
    //mydllForDelphi.dll  c++写的dll中的代码
    // extern "C" __declspec(dllexport) int add(int a, int b)
    //{
    //    return (a + b);
    //}
    //
    //extern "C" __declspec(dllexport) int sub(int a, int b)
    //{
    //    return (a - b);
    //}
    
    procedure TForm1.btn1Click(Sender: TObject);
    begin
      self.caption:= inttostr(add(4,3));
      self.caption:=self.caption+'///' +inttostr(sub(4,3));
    end;
    
    procedure TForm1.btn2Click(Sender: TObject);
    //  function add(a:integer;b:integer):integer;cdecl;external 'mydllForDelphi.dll' ;
    type
      TIntFunc=function(a:integer;b:integer):integer;cdecl; //cdecl 不能少
    var
      Th: Thandle;
      Tf:TIntFunc;
      Tp,tp2:TFarProc ;
    begin
      Th:= loadlibrary('mydllForDelphi.dll');
      if Th >0 then
      try
        Tp:=GetProcAddress(Th, PChar('add'));
        if Tp<>nil then
        begin
          Tf:=TIntFunc(Tp);
          Self.Caption:=IntToStr(Tf(9,1)); {调用TestC函数}
        end;
    
        Tp2:=GetProcAddress(Th, PChar('sub'));
        if Tp2<>nil then
        begin
          Tf:=TIntFunc(Tp2);
          Self.Caption:=Self.Caption+'|||'+IntToStr(Tf(9,1)); {调用TestC函数}
        end;
    
      finally
        freelibrary(th);
      end;
    
    end;
    
    end.
    书搞进脑袋 创新 创造; 积极
  • 相关阅读:
    计算机基础
    如何增加一个IT产品的用户黏性
    计算机相关专业一门课程一个案例
    【2012年6月14日】中兴发布Grand(智观)N970
    bmp格式浅析
    3Dbook的使用
    中英文翻译格式
    软件学习书籍推荐
    WEB开发基础
    PHP学习
  • 原文地址:https://www.cnblogs.com/tobetterlife/p/12161776.html
Copyright © 2011-2022 走看看