我需要将一些动态库进行封装,代码写完了,却无法运行进来,高手快来救救我,急死我了。 ( 积分: 150 )

  • 主题发起人 主题发起人 jobsxy
  • 开始时间 开始时间
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部分代码:

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();
 
提供的信息不足以解决你的问题
 
jianguobu:还需要哪些信息,尽管提
 
从你现在的代码来看,看不出问题。你的那两个DLL是自己做的吗?
 
不是我自己做的,但这两个DLL应该不会有问题,因为我没有封装前,调用的时候运行很正常,再说了DLL是硬件厂家提供的,肯定是没问题的
 
问几个问题:
1.以下两个声明直接调用可以成功?
Function Init(Com:integer ):integer;stdcall;external 'ADE400.dll';
Function Close(): Integer;stdcall;external 'ACD400.dll';
2.在以下程序段trace into ,看看到哪里出错,错误信息请贴出来
begin //停在这里,再按F9,就弹出地址错误
Application.Initialize;
Application.CreateForm(TForm1, Form1);
Application.Run;
end.
3.在调用下句前,看看FCOM的值是你期望的吗?
FunResult := INIT(FCOM);
 

Function Init(Com:integer ):integer;stdcall;external 'ADE400.dll';
Function Close(): Integer;stdcall;external 'ACD400.dll';

的函数声明与动态库内的定义可能不一致,或者动态库自身有问题
 
FunResult := INIT(FCOM);
FunResult := Close();
这两句是在你的封装的单元里吗?
 
我是个初学着,在学习使用DLL时也出现了这样的问题,
begin //停在这里,再按F9,就弹出地址错误
Application.Initialize;
Application.CreateForm(TForm1, Form1);
Application.Run;
end.
我的代码很简单,DLL也是自己做的,现将所有的代码贴出来,那位高手看一下,也算是造福一方。
test_dll.prj:

library test_dll;
uses
ShareMem,
SysUtils,
Classes,
test_function in 'test_function.pas';

{$R *.res}

begin
end.

test_function.pas:
unit test_function;

interface
uses Dialogs,Controls,Messages,Windows,SysUtils,Classes;

function test(X : string): string; stdcall;

implementation
function test(X : string): string;
begin
ShowMessage(X);
end;
end.
以上这俩个文件编译生成了test_dll.dll

project1.prj:
program Project1;

uses
ShareMem,
Forms,
Unit1 in 'Unit1.pas' {Form1};

{$R *.res}

begin
Application.Initialize;
Application.CreateForm(TForm1, Form1);
Application.Run;
end.
unit1.pas:
unit Unit1;

interface

uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;

type
TForm1 = class(TForm)
Button1: TButton;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;

function test(X:string) : string;stdcall; external 'test_dll.dll';
var
Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.Button1Click(Sender: TObject);
begin
test('button1');
end;
end.
这俩个文件生成了project1.exe.在调试的时候就发生以上的问题,应用程序总是无法初始化。
 
在你的DLL工程文件中要导出该函数名
exports test;



library test_dll;
uses
ShareMem,
SysUtils,
Classes,
test_function in 'test_function.pas';

{$R *.res}
exports
test;
begin
end.


 
多人接受答案了。
 
后退
顶部