下面的公共单元到底用不用放到dll工程里?如果是,build的时候是不是也放到dll文件里去了?要使用这个dll的程序也要uses这个公共单元,就是要拷贝一个公共单元放到这个程序的开发目录下吗?这样的话dll中有一个,程序的文件夹中又有一个,两个公共单元文件。这样对吗。
截图如下:代码如下:
用Delphi封装类到DLL:一个公共单元:unit ITest;interfacetype
IT = interface
function GetString:string;
procedure ShowMsg(p:PChar);
procedure Msg;
end;implementationend.
类单元,这个写在DLL里面的:
unit UTest;interfaceuses
SysUtils,
Windows,
ITest;type
TTest = class(TInterfacedObject,IT)
private
i:Integer;
protected public
constructor Create; //override;
destructor Destroy; override;
function GetString:string;
procedure ShowMsg(p:PChar);
procedure Msg;
published end; implementationconstructor TTest.Create;
begin
i:=0;
end;destructor TTest.Destroy;
begin
inherited;
end;function TTest.GetString:string;
begin
Result := 'Test string';
end;procedure TTest.ShowMsg(p:PChar);
begin
MessageBox(0,p,'Test',MB_OK);
end;procedure TTest.Msg;
begin
Inc(i);
MessageBox(0,'Test MessageBox',PChar(IntToStr(i)),MB_OK);
end;end.DLL的prj:
library Test;uses
SysUtils,
Classes,
ITest in 'ITest.pas',
UTest in 'UTest.pas';{$R *.res}function TestCreate:IT; stdcall;
begin
Result := TTest.Create;
end; exports
TestCreate; //用此初始化begin
end.DLL部分就这样了,到EXE部分调用:uses
ITest; //引用单元function TestCreate:IT; stdcall; external 'Test.dll' name 'TestCreate'; //引用DLL函数//声明作为测试
private
AA:IT;
BB:IT;procedure TForm1.FormCreate(Sender: TObject);
begin
AA:= TestCreate;
BB:= TestCreate;
end;procedure TForm1.Button1Click(Sender: TObject);
begin
Button1.Caption := AA.GetString;
end;procedure TForm1.Button2Click(Sender: TObject);
begin
AA.ShowMsg('123abc');
end;procedure TForm1.Button3Click(Sender: TObject);
begin
AA.Msg;
end;procedure TForm1.Button4Click(Sender: TObject);
begin
BB.Msg;
end;
截图如下:代码如下:
用Delphi封装类到DLL:一个公共单元:unit ITest;interfacetype
IT = interface
function GetString:string;
procedure ShowMsg(p:PChar);
procedure Msg;
end;implementationend.
类单元,这个写在DLL里面的:
unit UTest;interfaceuses
SysUtils,
Windows,
ITest;type
TTest = class(TInterfacedObject,IT)
private
i:Integer;
protected public
constructor Create; //override;
destructor Destroy; override;
function GetString:string;
procedure ShowMsg(p:PChar);
procedure Msg;
published end; implementationconstructor TTest.Create;
begin
i:=0;
end;destructor TTest.Destroy;
begin
inherited;
end;function TTest.GetString:string;
begin
Result := 'Test string';
end;procedure TTest.ShowMsg(p:PChar);
begin
MessageBox(0,p,'Test',MB_OK);
end;procedure TTest.Msg;
begin
Inc(i);
MessageBox(0,'Test MessageBox',PChar(IntToStr(i)),MB_OK);
end;end.DLL的prj:
library Test;uses
SysUtils,
Classes,
ITest in 'ITest.pas',
UTest in 'UTest.pas';{$R *.res}function TestCreate:IT; stdcall;
begin
Result := TTest.Create;
end; exports
TestCreate; //用此初始化begin
end.DLL部分就这样了,到EXE部分调用:uses
ITest; //引用单元function TestCreate:IT; stdcall; external 'Test.dll' name 'TestCreate'; //引用DLL函数//声明作为测试
private
AA:IT;
BB:IT;procedure TForm1.FormCreate(Sender: TObject);
begin
AA:= TestCreate;
BB:= TestCreate;
end;procedure TForm1.Button1Click(Sender: TObject);
begin
Button1.Caption := AA.GetString;
end;procedure TForm1.Button2Click(Sender: TObject);
begin
AA.ShowMsg('123abc');
end;procedure TForm1.Button3Click(Sender: TObject);
begin
AA.Msg;
end;procedure TForm1.Button4Click(Sender: TObject);
begin
BB.Msg;
end;
但更好的办法是,解决共享参数,这样就不要放了。