delphi 调用dll窗体

1.首先用delphi建一个dll工程:

代码如下:

library Project2;

uses
  SysUtils,
  Classes,
  Unit1 in 'Unit1.pas' {Form1};   //dll中的窗体为Form1
procedure ShowTest();stdcall;   //dll函数 用于显示窗体
var
f:TForm1;

begin
f:=TForm1.Create(nil);
try
   f.ShowModal;
finally
   f.Free;
end;
end;

{$R *.res}
exports
    ShowTest;    //外部调用函数

begin

end.

 

2.新建Form1窗体,窗体为空窗体--》Unit1.pas。

unit Unit1;

interface

uses

Windows, SysUtils, Controls, Forms;

type

TForm1 = class(TForm)

private

    { Private declarations }

public

    { Public declarations }

end;

var

Form1: TForm1;

implementation

{$R *.dfm}

end.

 

3. CTRL+F9编译dll。 编译成功,并生成Project2.dll

 

4.新建工程,Project1. 采用动态调用dll方式:

 

unit Unit_main;

interface

uses

Windows, SysUtils, Classes, Controls, Forms, StdCtrls;

type

TForm1 = class(TForm)

    Button1: TButton;

    procedure Button1Click(Sender: TObject);

private

    { Private declarations }

public

    { Public declarations }

end;

var
Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.Button1Click(Sender: TObject);

type

mypointer=Procedure();stdcall;

var
Handle:THandle;
frm_p:mypointer;
begin
Handle:=loadlibrary('Project2.dll');
if Handle>32 then
begin
     frm_p:=GetProcAddress(Handle,'ShowTest');
     if @frm_p<>nil then frm_p;
end;
Freelibrary(Handle);
end;


end.

 

5. 修改Project1.dpr文件,加上Sharemem:

  uses
  Sharemem ,Forms,
  Unit_main in 'Unit_main.pas' {Form1};

 

6. 运行Project1,点击Button1,实现调用出dll窗体。


参考了网上的东西,自己试验了一遍,记录下来  加深印象。

你可能感兴趣的:(delphi 调用dll窗体)