【发布时间】:2020-01-19 19:41:48
【问题描述】:
我在 Delphi 10.3.3 中创建了一个 DLL 并编译它(作为 32 位):
library TestDLL;
uses
Vcl.ExtCtrls,
Vcl.Graphics,
System.SysUtils,
System.Classes;
{$R *.res}
procedure ColorPanel(APanel: TPanel);
begin
APanel.Color := clRed;
end;
exports
ColorPanel;
begin
end.
然后在 Delphi 10.3.3 中,我创建了一个 VCL 应用程序来托管 DLL:
unit Main;
interface
uses
Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.ExtCtrls, Vcl.StdCtrls;
type
TForm3 = class(TForm)
pnlTest: TPanel;
btnTest: TButton;
procedure btnTestClick(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form3: TForm3;
implementation
{$R *.dfm}
procedure ColorPanel(APanel: TPanel); external 'TestDLL.dll';
procedure TForm3.btnTestClick(Sender: TObject);
begin
ColorPanel(pnlTest);
end;
end.
这是表单文件'Main.dfm':
object Form3: TForm3
Left = 0
Top = 0
Caption = 'Form3'
ClientHeight = 217
ClientWidth = 359
Color = clBtnFace
Font.Charset = DEFAULT_CHARSET
Font.Color = clWindowText
Font.Height = -13
Font.Name = 'Tahoma'
Font.Style = []
OldCreateOrder = False
Position = poScreenCenter
PixelsPerInch = 120
TextHeight = 16
object pnlTest: TPanel
Left = 0
Top = 0
Width = 153
Height = 217
Align = alLeft
Caption = 'pnlTest'
TabOrder = 0
end
object btnTest: TButton
Left = 208
Top = 24
Width = 75
Height = 25
Caption = 'Test'
TabOrder = 1
OnClick = btnTestClick
end
end
这是项目文件“TestDLL.dpr”:
program DllHost;
uses
Vcl.Forms,
Main in 'Main.pas' {Form3};
{$R *.res}
begin
Application.Initialize;
Application.MainFormOnTaskbar := True;
Application.CreateForm(TForm3, Form3);
Application.Run;
end.
单击按钮应将面板着色为红色。
为什么它不起作用,我怎样才能使它起作用?
【问题讨论】:
-
这行不通。你需要使用包。
-
软件包将无法工作,因为主机与 DLL 的 Delphi 版本不同。您可以从我的代码中看到 DLL 运行良好。为什么你总是想破坏我?
-
如果你有不同的delphi版本,那么你的代码只能在偶然的情况下工作。您将在来自不同版本 dll 的对象上执行来自 VCL 的一个版本的代码。即使使用相同的 delphi 版本,它也不起作用,因为您有两个不同的 VCL 实例。无论问题是什么,因此都不是解决方案。
-
user1580348:在您的 Q 中,您写道“为什么它不起作用,我怎样才能使它起作用?”但是在您对@David 评论的回复中,您写道“您可以从我的代码中看到 DLL 运行良好。”后一条评论不仅奇怪,而且似乎与您在 Q 中的评论相矛盾。
-
另外,在您的 Q 中,您写道 DLL 和客户端应用程序都是使用 Delphi 10.3.3 构建的,但在您的评论中您写道“主机是与 DLL 不同的 Delphi 版本”,我也觉得有点奇怪。
标签: delphi dll delphi-10.3-rio