Delphi 关于创建dll文件的问题
我的dll文件代码如下:
library mydll;
{ Important note about DLL memory management: ShareMem must be the
first unit in your library's USES clause AND your project's (select
Project-View Source) USES clause if your DLL exports any procedures or
functions that pass strings as parameters or function results. This
applies to all strings passed to and from your DLL--even those that
are nested in records and classes. ShareMem is the inte易做图ce unit to
the BORLNDMM.DLL shared memory manager, which must be deployed along
with your DLL. To avoid using BORLNDMM.DLL, pass string information
using PChar or ShortString parameters. }
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls,
Unit1 in 'Unit1.pas' {Form1};
{$R *.res}
procedure val();stdcall;export;
var form1:TForm1;
begin
Form1.edt1.Text:='hello world';
form1.edt1.Font.Color:=clred;
end;
exports
val;
begin
end.
我想将mydll文件装载到主程序后,把 val 过程放到button的click事件中,单击button后,让edt1文本框里显示红色的hello world。
但是运行并单击button后,弹出 Access Violation at address 00580D2B in module 'mydll.dll'。 Read of address 00000388
如果把 form1.edt1.Font.Color:=clred; 去掉,只输入一行语句,则可以正常运行。难道像这样在dll里的过程,只能有一行语句吗?
请高手帮忙,谢谢
补充:主窗体代码:
unit Unit1;
inte易做图ce
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;
type
TForm1 = class(TForm)
edt1: TEdit;
btn1: TButton;
procedure btn1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
Procedure val();stdcall;external 'mydll.dll';
implementation
{$R *.dfm}
procedure TForm1.btn1Click(Sender: TObject);
begin
val();
end;
end.
追问:procedure val();stdcall;export;
begin
Form1:=TForm1.create(nil);
Form1.Show;
form1.edt1.Text:='hello';
Form1.Font.Color:=clRed;
end;
在上述代码中,我加上Form1:=TForm1.create(nil);语句。并删除了var form1:TForm1;
程序虽然可以运行,但单击按钮后,是重新建立了一个窗体,在文本框里显示红色hello。
我想单击按钮后,在主程序窗体上显示应该怎样做?
答案:把你的声明改一点点就行了,如下:
procedure val(a:tForm;b:tEdit);stdcall;export;
begin
// a.InsertControl(b);
//b.Parent:=a;
b.Text:='Hello World';
b.Font.Color:=clred;
end;
exports
val;
begin
end.
实现调用就不必写了吧
其他:Form1:=TForm1.create(nil);
前面加一句这个. 主程序直接打开DLL中的窗体吧 [
var
Form1: TForm1;
] 你只声明form1,都没有实例化它,你dll怎么编译出来的?
上一个:delphi 中如何调用sql 存储过程
下一个:delphi 怎么改变按钮背景