最简单的delphi加载C++dll实例

来源:互联网 发布:windows 关键问题 编辑:程序博客网 时间:2024/06/05 21:05

C++的dll:

#ifndef _DLL_FOR_DEL_H_
#define _DLL_FOR_DEL_H_
extern "C"
{
_declspec(dllexport)int mult();


};
#endif

---------

#include "dllfordel.h"
int mult()
{
return 5*6;
}

================================delphi==========================

unit CPlusDllTest;


interface


uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls;


type
          CplusFuc=function():Integer;stdcall;
  TForm1 = class(TForm)
    lbl1: TLabel;
    edt1: TEdit;
    btn1: TButton;
    btn2: TButton;
    procedure btn1Click(Sender: TObject);
    procedure btn2Click(Sender: TObject);


  private
    { Private declarations }
  public
    { Public declarations }
  end;


var
  Form1: TForm1;
      Func:CplusFuc;
      hMhd:Thandle ;
      ret:Integer;
implementation


{$R *.dfm}


procedure TForm1.btn1Click(Sender: TObject);
begin
hMhd:=LoadLibrary('dllfordel.dll');
if hMhd=null then
 begin
 Application.MessageBox('加载C++ dll失败',0)
 end
else


  begin
      // Application.MessageBox('加载C++ dllsucess','加载dll',MB_OK)    ;
       @Func:=GetProcAddress(hMhd,'mult');
       ret:=Func();
       edt1.Text:=IntToStr(ret);
  end;
    end;
  //end;
//end;


procedure TForm1.btn2Click(Sender: TObject);
begin
edt1.Text:='';
end;


end.

原创粉丝点击