下面的公共單元到呼叫不用放到dll工程里?如果是,build的時候是不是也放到dll檔案里去了?要使用這個dll的程式也要uses這個公共單元,就是要拷貝一個公共單元放到這個程式的開發目錄下嗎?這樣的話dll中有一個,程式的檔案夾中又有一個,兩個公共單元檔案。。。。這樣對嗎。。。。
截圖如下:

代碼如下:
用Delphi封裝類到DLL:
一個公共單元:
unit ITest;
interface
type
IT = interface
function GetString:string;
procedure ShowMsg(p:PChar);
procedure Msg;
end;
implementation
end.
類單元,這個寫在DLL里面的:
unit UTest;
interface
uses
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;
implementation
constructor 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;
uj5u.com熱心網友回復:
都要有。dll和程式各放一個。uj5u.com熱心網友回復:
Dll不通信共享引數,當然要放了。但更好的辦法是,解決共享引數,這樣就不要放了。
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/94527.html
標籤:VCL組件開發及應用
