Delphi调用VB编写的dll文件问题

wildhorsetlj 2008-12-01 11:29:44
下文所示为VB的动态连接库声明及调用情况:
===========================================================
Private Type DATA1REG
Value(1 To 8) As Byte '测量值
End Type

'结构体定义
Private Type DATAREG
Num As Long '编号
TimeStr(1 To 20) As Byte '时间
Value(1 To 64) As DATA1REG '测量值
End Type

'DLL声明
Private Declare Function ReadData Lib "test.dll" _
(ByVal FileStr As String, _
ByRef begintime As Byte, _
ByRef endtime As Byte, _
ByRef data As DATAREG) As Long

'按键处理--将指定数据文件转化为结构体数组
Private Sub Command1_Click()


Dim FileStr As String '数据文件名及路径
Dim begintime(7) As Byte '起始时间
Dim endtime(7) As Byte '结束时间
Dim Num As Integer '通道编号
Dim datas(0 To 50000) As DATAREG '返回的数据结构体,大小自己设置

Dim RecordNum As Long '返回的记录数量

Dim i, j, k As Integer
Dim str1, str2 As String

FileStr = "20070905.R01" '打开记录仪数据的路径

begintime(0) = 8 '06年
begintime(1) = 1 '1月
begintime(2) = 1 '1日
begintime(3) = 12 '12时
begintime(4) = 30 '30分
begintime(5) = 0 '0秒
begintime(6) = 0 '结束符
endtime(0) = 12 '09年
endtime(1) = 1 '1月
endtime(2) = 1 '1日
endtime(3) = 0 '0时
endtime(4) = 0 '0分
endtime(5) = 0 '0秒
endtime(6) = 0 '结束符

Num = 1 '第1通道
RecordNum = -10 '赋初值
RecordNum = ReadData(FileStr, begintime(0), endtime(0), datas(0))
if RecordNum > 0 Then
MsgBox ("数据处理过程结束!")
End If
End Sub
============================================================================
将上述代码转换到Delphi中后出现一系列错误,不知道是何原因,Dephi代码如下:

type
Data1Reg=record
Value:array[1..8] of Byte;
end;

type
//数据采集解析结构体
DataReg=record
Num : LongInt; //记录编号
TimeStr:array[1..20]of Byte; //记录时间
Value :array[1..64]of Data1Reg; //测量值
end;
type HWDataReg =^DataReg;

//DLL声明
function ReadData(Filestr:string;begintime :Byte;
endtime : Byte; data : DataReg) :LongInt;stdcall;external 'test.dll';

type
Tfrm_Main = class(TForm)
btn_Read: TButton;
edt_FileName: TEdit;
procedure btn_ReadClick(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;

var
frm_Main: Tfrm_Main;

implementation

{$R *.dfm}

procedure Tfrm_Main.btn_ReadClick(Sender: TObject);
var
timeBeg,timeEnd:array[0..6] of Byte;
StrFile:string;
RecordNum:longint;
nChValue:array[0..1000] of DataReg; //注,此处超过5000值时就报Stack over错误,更大的赋值就不可能了。
i,j,k,Num :integer;
str1,str2,str3:string;
begin
StrFile :='F:\MyProWork\HW60A\ReadFile\bin\'+Trim(edt_FileName.Text)+'.R01';
timeBeg[0]:=6; //年
timeBeg[1]:=1; //月
timeBeg[2]:=1; //日
timeBeg[3]:=0; //时
timeBeg[4]:=0; //分
timeBeg[5]:=0; //秒
timeBeg[6]:=0; //结束符
timeEnd[0]:=9; //年
timeEnd[1]:=1; //月
timeEnd[2]:=1; //日
timeEnd[3]:=0; //时
timeEnd[4]:=0; //分
timeEnd[5]:=0; //秒
timeEnd[6]:=0; //结束符
Num:=1;
RecordNum :=-10;
//GetMem(nChValue,Length(DataReg));
{SetLength(nChValue,50000);
for i:=0 to 50000 do
begin
nChValue[i]:=(New(HWDataReg))^;
end; }
RecordNum :=ReadData(StrFile,timeBeg[0],timeEnd[0],nChValue[0]); //该句无法返回RecordNum值,报Access Violation错误。
if RecordNum >0 then
begin
ShowMessage('数据处理完毕,共有记录'+Inttostr(RecordNum));
end;
end;
...全文
128 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
wildhorsetlj 2008-12-01
  • 打赏
  • 举报
回复
谢谢unsigned、 yuqianyi1974 两位,问题解决!结贴!
yuqianyi1974 2008-12-01
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 wildhorsetlj 的回复:]
To:unsigned

非常感谢,函数调用的问题解决了,只是还有一个问题:
nChValue:array[0..1000] of DataReg;
该数组长度定义值超过2000时就报Stack over错误,如果要定义一个500000长度的数组的话该如何处理更妥当呢?
[/Quote]
nChValue:array of DataReg;

setlength(nchvalue,1000);//在堆中分配即可
wildhorsetlj 2008-12-01
  • 打赏
  • 举报
回复
To:unsigned

非常感谢,函数调用的问题解决了,只是还有一个问题:
nChValue:array[0..1000] of DataReg;
该数组长度定义值超过2000时就报Stack over错误,如果要定义一个500000长度的数组的话该如何处理更妥当呢?
僵哥 2008-12-01
  • 打赏
  • 举报
回复
Private Declare Function ReadData Lib "test.dll" _ 
(ByVal FileStr As String, _
ByRef begintime As Byte, _
ByRef endtime As Byte, _
ByRef data As DATAREG) As Long

Function ReadData
(const FileStr : PChar;
var begintime :Byte;
var endtime: Byte;
var data :DATAREG): LongInt; stdcall; external 'test.dll' name 'ReadData';

16,749

社区成员

发帖
与我相关
我的任务
社区描述
Delphi 语言基础/算法/系统设计
社区管理员
  • 语言基础/算法/系统设计社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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