J
jobsxy
Unregistered / Unconfirmed
GUEST, unregistred user!
我封装的UNIT部分代码:
unit Unit1;
interface
uses
Windows,Controls,SysUtils,Classes,Variants;
type
TTest = class
private
FCom: integer;
FActive: boolean;
procedure SetActive(const Value: boolean);
public
constructor Create;
destructor Destroy; override;
published
property COM: Integer read FCom Write FCom; //端口设置
property Active: boolean read FActive write SetActive; //端口初始化
end;
Function Init(Com:integer ):integer;stdcall;external 'ADE400.dll';
Function Close(): Integer;stdcall;external 'ACD400.dll';
implementation
{ TTest }
constructor TTest.Create;
begin
//略
end;
destructor TTest.Destroy;
begin
//略
inherited;
end;
procedure TTest.SetActive(const Value: boolean);
var
FunResult: Integer;
begin
if Value Then
begin
FunResult := INIT(FCOM);
if FunResult<0 Then raise exception.Create('初始化端口失败');
end
else
begin
FunResult := Close();
if FunResult<0 Then raise exception.Create('关闭端口失败');
end;
FActive := Value;
end;
end.
下面是我调用的代码:
uses unit1;
procedure TForm1.Button3Click(Sender: TObject);
var
test: ttest;
begin
test:= ttest.Create;
try
test.Active := True;
finally
test.Active := False;
test.Free;
end;
end;
-------------------------------------
运行到
{$R *.res}
begin //停在这里,再按F9,就弹出地址错误
Application.Initialize;
Application.CreateForm(TForm1, Form1);
Application.Run;
end.
奇怪的是,我把TTest里调用函数的代码注释掉以后,程序就不会报以上错误了,敬请有经验的大侠快施援手。
// FunResult := INIT(FCOM);
// FunResult := Close();
unit Unit1;
interface
uses
Windows,Controls,SysUtils,Classes,Variants;
type
TTest = class
private
FCom: integer;
FActive: boolean;
procedure SetActive(const Value: boolean);
public
constructor Create;
destructor Destroy; override;
published
property COM: Integer read FCom Write FCom; //端口设置
property Active: boolean read FActive write SetActive; //端口初始化
end;
Function Init(Com:integer ):integer;stdcall;external 'ADE400.dll';
Function Close(): Integer;stdcall;external 'ACD400.dll';
implementation
{ TTest }
constructor TTest.Create;
begin
//略
end;
destructor TTest.Destroy;
begin
//略
inherited;
end;
procedure TTest.SetActive(const Value: boolean);
var
FunResult: Integer;
begin
if Value Then
begin
FunResult := INIT(FCOM);
if FunResult<0 Then raise exception.Create('初始化端口失败');
end
else
begin
FunResult := Close();
if FunResult<0 Then raise exception.Create('关闭端口失败');
end;
FActive := Value;
end;
end.
下面是我调用的代码:
uses unit1;
procedure TForm1.Button3Click(Sender: TObject);
var
test: ttest;
begin
test:= ttest.Create;
try
test.Active := True;
finally
test.Active := False;
test.Free;
end;
end;
-------------------------------------
运行到
{$R *.res}
begin //停在这里,再按F9,就弹出地址错误
Application.Initialize;
Application.CreateForm(TForm1, Form1);
Application.Run;
end.
奇怪的是,我把TTest里调用函数的代码注释掉以后,程序就不会报以上错误了,敬请有经验的大侠快施援手。
// FunResult := INIT(FCOM);
// FunResult := Close();