书上提示的操作步骤是:
1、建立DLL
在Delphi的主菜单中选“File”→“New”→“Other”→“DLL Wizard”→“OK”,自动生成DLL代码。
2、建立DLL子窗体
在Delphi的主菜单中选“File”→“New”→“Form”,然后添加代码如下:
procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction);
begin
action:=cafree;
end;在Form的public中加入:
MyParentForm:TForm;
MyParentApplication:TApplication;
3、建立主窗体调用DLL,然后定义全局变量……
这一步不会了,书上没有说怎样“建立主窗体调用DLL”。请问定义全局变量的代码具体应该写在哪里呀?
1、建立DLL
在Delphi的主菜单中选“File”→“New”→“Other”→“DLL Wizard”→“OK”,自动生成DLL代码。
2、建立DLL子窗体
在Delphi的主菜单中选“File”→“New”→“Form”,然后添加代码如下:
procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction);
begin
action:=cafree;
end;在Form的public中加入:
MyParentForm:TForm;
MyParentApplication:TApplication;
3、建立主窗体调用DLL,然后定义全局变量……
这一步不会了,书上没有说怎样“建立主窗体调用DLL”。请问定义全局变量的代码具体应该写在哪里呀?
DLL源代码:
library Project2;uses
SysUtils,
Classes,
Dialogs,
Forms,
Unit2 in 'Unit2.pas' {Form2};{$R *.RES}
var
ccc: Pchar;procedure OpenForm(mainForm:TForm);stdcall;
var
Form1: TForm1;
ptr:PLongInt;
begin
ptr:=@(Application.MainForm);
ptr^:=LongInt(mainForm);
Form1:=TForm1.Create(mainForm);
end;procedure InputCCC(Text: Pchar);stdcall;
begin
ccc := Text;
end;procedure ShowCCC;stdcall;
begin
ShowMessage(String(ccc));
end;exports
OpenForm;
InputCCC,
ShowCCC;
begin
end.调用方源代码:
unit Unit1;interfaceuses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
StdCtrls;type
TForm1 = class(TForm)
Button1: TButton;
Button2: TButton;
Edit1: TEdit;
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;var
Form1: TForm1;implementation{$R *.DFM}
procedure OpenForm(mainForm:TForm);stdcall;External'project2.dll';
procedure ShowCCC;stdcall;External'project2.dll';
procedure InputCCC(Text: Pchar);stdcall;External'project2.dll';procedure TForm1.Button1Click(Sender: TObject);
var
Text: Pchar;
begin
Text := Pchar(Edit1.Text);
// OpenForm(Application.MainForm);//为了调MDICHILD
InputCCC(Text);//为了实验DLL中的全局变量是否在各个应用程序间共享
end;procedure TForm1.Button2Click(Sender: TObject);
begin
ShowCCC;//这里表明WINDOWS 32位应用程序DLL中的全局变量也是在应用程序地址空间中,16位应用程序或许不同,没有做实验。
end;
照搬您的程序,错误如下:
[Error] Project2.dpr(22): Undeclared identifier: 'TForm'
[Error] Project2.dpr(27): Undeclared identifier: 'application'
[Error] Project2.dpr(27): ')' expected but identifier 'MainForm' found
[Error] Project2.dpr(39): Undeclared identifier: 'ShowMessage'
[Error] Project2.dpr(44): Declaration expected but identifier 'InputCCC' found
修改后,是:
[Hint] Project2.dpr(29): Value assigned to 'Form1' never used
[Error] Project2.dpr(39): Undeclared identifier: 'ShowMessage'
[Error] Project2.dpr(44): Declaration expected but identifier 'InputCCC' found
不知怎么回事?再次请教。