DLL及调用方式

 一.隐式调用.

    library Project1;
uses
  SysUtils,
  Classes;

{$R *.res}
function sum(a,b:Integer):Integer;stdcall;
begin
  Result := a+b;
end;

function linkstr(str1,str2:PChar):PChar;stdcall;
begin
  result :=PChar(StrPas(str1)+strpas(str2));
end;

function ifcheck(int1,int2:Integer):Boolean;stdcall;
begin
  if int1>int2 then
  Result:=True
  else
  Result:=False;
end;
exports
 sum,linkstr,ifcheck;

begin

end.

=================

var
  Form1: TForm1;

implementation
function sum(a,b:integer):Integer;      stdcall; external 'Project1.dll';
function linkstr(str1,str2:PChar):PChar;stdcall; external 'project1.dll';
function ifcheck(int1,int2:Integer):Boolean;stdcall;external 'project1.dll';

{$R *.dfm}

procedure TForm1.Button1Click(Sender: TObject);
begin
Self.Caption:=IntToStr(sum(StrToInt(edt1.Text),StrToInt(edt2.Text)));
end;

procedure TForm1.Button2Click(Sender: TObject);
begin
  Self.Caption:=StrPas(linkstr(PChar(edt1.Text),PChar(edt2.Text)));
end;

procedure TForm1.Button3Click(Sender: TObject);
begin
  if ifcheck(StrToInt(edt1.Text),StrToInt(edt2.Text)) then
  Self.Caption:='数一大于数二'
  else
  Self.Caption:='数二大于数一';
end;

end.

//------------------------------------------------------------------------------------

二.显未调用.

procedure TForm1.Button1Click(Sender: TObject);
var
  hdll:HMODULE;
  sum:function(a,b:integer):Integer;stdcall;
begin
  hdll:=LoadLibrary('project1.dll');
  if hdll <> 0 then
  begin
    @sum :=GetProcAddress(hdll,'sum');
    if @sum <> nil then
    self.Caption:=IntToStr(sum(StrToInt(edt1.Text),strtoint(edt2.Text)))
    else
    ShowMessage('DLL中该功能不存在');
    FreeLibrary(hdll);
    end
    else
    ShowMessage('无法加载DLL');
end;

 


//end;

procedure TForm1.Button2Click(Sender: TObject);
var
  hdll:HMODULE;
  linkstr:function(str1,str2:pchar):PChar;stdcall;
begin
  hdll:=LoadLibrary('project1.dll');
  if hdll<>0 then
  begin
    @linkstr:=GetProcAddress(hdll,'linkstr');
    if @linkstr<> nil then
      Self.Caption:=StrPas(linkstr(PChar(edt1.text),pchar(edt2.text)))
    else
    ShowMessage('dll中模块函数加载不成功');
    FreeLibrary(hdll);
    END
  else
ShowMessage('DLL加载不成功');

end;

procedure TForm1.Button3Click(Sender: TObject);
var
  hdll:HMODULE;
  ifcheck:function(a,b:integer):Boolean;stdcall;
begin
  hdll:=LoadLibrary('project1.dll');
  if hdll<>0 then
  begin
    @ifcheck:=GetProcAddress(hdll,'ifcheck');
    if @ifcheck<>nil then
       if ifcheck(StrToInt(edt1.text),StrToInt(edt2.Text)) then
       Self.Caption:='数一大于数二'
       else
       Self.Caption:='数二大于数一'
    else
    ShowMessage('加载函数时出错');
  FreeLibrary(hdll);
  END
else
ShowMessage('加载DLL时出错');

end;

end.

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

你可能感兴趣的:(DLL及调用方式)