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.
    书搞进脑袋 创新 创造; 积极
  • 相关阅读:
    处理流之转换流
    处理流之缓冲流 buffered
    java学习笔记 字符流Reader与Writer
    java学习笔记 OutputStream与InputStream
    java学习笔记 Map接口
    java 学习笔记 Iterator 迭代器
    java学习笔记 genenic 范形
    应急响应介绍
    安全之红蓝对抗简介
    密码学基础下篇
  • 原文地址:https://www.cnblogs.com/tobetterlife/p/12161776.html
Copyright © 2011-2022 走看看