DLL中如何使用窗体?

bluesky 2000-04-22 11:00:00

我用Delphi 3.0 编写一显示输入用户名和口令的对话框的DLL后,在调用时出现访问错误.原代码如下:
//DLL实现单元
unit dll;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
StdCtrls, Buttons;
type
Tinputbox = class(TForm)
Edit1: TEdit;
Edit2: TEdit;
Label1: TLabel;
Label2: TLabel;
Label3: TLabel;
BitBtn1: TBitBtn;
BitBtn2: TBitBtn;
private
{ Private declarations }
public
{ Public declarations }
end;
var
inputbox: Tinputbox;
function iinput:String;export;
implementation
{$R *.DFM}
function iinput:String;
var
str:String;
begin
inputbox:=TInputBox.Create(application);
if InputBox.ShowModal=1 Then str:=inputBox.edit1.Text
else
str:='';
inputbox.Free;
iinput:=str;
End;
end.
//DLL工程单元
library dlldemo;
uses
ShareMem,
dll in 'dll.pas' {inputbox};
{$R *.RES}
Exports
iinput;
begin
end.
//DLL调用单元
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
StdCtrls;
type
TForm1 = class(TForm)
Button1: TButton;
Edit1: TEdit;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.DFM}
procedure TForm1.Button1Click(Sender: TObject);
var
hand:THandle;
func:function:String;
begin
hand:=LoadLibrary('d:\dlldemo.dll');
func:=GetProcAddress(hand,'iinput');
form1.Edit1.Text:=func;
FreeLibrary(hand);
end;
end.


...全文
194 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
jll 2000-04-23
  • 打赏
  • 举报
回复
补充:DLL中要使用String类型也是可以的!不过要在DLL和调用程序的第一个引用库中申明ShareMem单元!
如:uses ShareMem,……
Lin 2000-04-23
  • 打赏
  • 举报
回复
zensst说得不全对:主要是返回值的缘故,将String改为PChar就万事大吉。
RIAEasy 2000-04-22
  • 打赏
  • 举报
回复
注意,在Dll中调用Form,必须要有AOwner,即Application。
所以,你的函数iinput应该申明为function iinput(AH: THandle;
var P: PCHAR): Boolean; stdcall;
你的程序有两个不妥:iinput的Application为非法值,因为你没有创
建(在Application中是自动的,而Dll中却不是);返回值不要用String,
最好由主程序传递参数.

5,386

社区成员

发帖
与我相关
我的任务
社区描述
Delphi 开发及应用
社区管理员
  • VCL组件开发及应用社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧