Delphi调用VB编写的dll文件问题
下文所示为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;