HanDs
NO.2

[Delphi文章] 动态链接库 





学习中请遵循国家相关法律法规,黑客不作恶。没有网络安全就没有国家安全

本站需要登陆后才能查看

在Delphi集成开发环境中,通过菜单File|New打开New Items对话框,选中New标签中
的DLL 项,按下OK按钮。系统会自动创建一个DLL 工程,并在代码编辑窗口中打开
Project1.dpr文件。
library Project1;
uses
SysUtils,
Classes;
{$R *.RES}
function InStr2(Source: PChar;Ch: Char): integer;stdcall;
var
  Len,i: Integer;
begin
  Len := strlen(Source);
  for i := 0 to Len-1 do
  if Source[i] = ch then
  begin
   Result := i;
   Exit;
  end;
  Result := -1;
end;
function InStr(Source: PChar;Ch: Char): Integer; export;
var
  Len,i: Integer;
begin
  Len := strlen(Source);
  for i := 0 to Len-1 do
  if Source[i] = ch then
  begin
    Result := i;
    Exit;
  end;
  Result := -1;
end;

exports InStr2;
exports  InStr;
begin
end.

然后创建一个工程,放两个按钮Button1,button2。
代码如下:
unit Unit2;

interface

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

type
  TForm1 = class(TForm)
    Button1: TButton;
    Button2: TButton;
    procedure Button1Click(Sender: TObject);
    procedure Button2Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;
  TInStr=function(Source: PChar;Ch: Char): Integer;
  function InStr2(Source: PChar;Ch: Char): Integer; stdcall;external 'Project2.dll';
var
  Form1: TForm1;
  InStr1:TInStr;
  Moudle: THandle;
implementation

{$R *.dfm}
//静态调用
procedure TForm1.Button1Click(Sender: TObject);
begin
  ShowMessage(IntToStr(InStr2('yanghai','y')));
end;
//动态调用
procedure TForm1.Button2Click(Sender: TObject);
var
  TX:TFarProc;
begin
 Moudle := LoadLibrary('Project2.dll');
 try
  if Moudle <>0 then
  begin
    TX:=GetProcAddress(Moudle,'InStr');
    if TX<>nil then
    begin
       InStr1:=TInStr(TX);
       ShowMessage(IntToStr(InStr1('yanghai','y')));
    end;
  end;
 finally
  FreeLibrary(Moudle);
 end;
end;

end.


学习中请遵守法律法规,本网站内容均来自于互联网,本网站不负担法律责任
动态 链接
#1楼
发帖时间:2016-7-9   |   查看数:0   |   回复数:0
游客组