我有一个遗留项目是多年前用VB 6编写的。由于VB 6太旧,我在Google上找不到足够的VB 6信息。
我正在尝试从VB 6调用一个用VC ++编写的DLL。问题是当VB调用dll时,VB已经崩溃了。我以为这是DLL的错误,然后我调试DLL。但是,我发现dll工作正常。它最终返回0,但VB传递双数组不成功,但是DLL工作正常。然后当dll exectued完成并返回VB时,VB则崩溃。我无法弄清楚发生了什么。有什么想法吗?
这是我的VB代码
Declare Function parseexcel Lib "parseexcelct.dll" (ByVal thepath As String, ByRef total() As Double, ByRef xy() As Double, ByRef ylxx() As Double, ByRef zy() As Double, ByRef zcy() As Double, ByRef gj1 As Double, ByRef gj2 As Double, ByRef xs1 As Double, ByRef xs2 As Double, ByVal gjt1 As Double, ByVal gjt2 As Double, ByVal xst1 As Double, ByVal xst2 As Double) As Long
Dim mypathstr As String
Dim total(0 To 20) As Double
Dim xy(0 To 20) As Double
Dim ylxx(0 To 20) As Double
Dim zy(0 To 20) As Double
Dim zcy(0 To 20) As Double
Dim gj1 As Double, gj2 As Double, xs1 As Double, xs2 As Double, gjt1 As Double
Dim gjt2 As Double, xst1 As Double, xst2 As Double
Dim result As Integer
mypathstr = CommonDialog.FileName
Dim i As Integer
'try to initial the array
For i = 0 To 20
total(i) = 1.1
xy(i) = 1.1
ylxx(i) = 1.1
zy(i) = 1.1
zcy(i) = 1.1
Next i
result = 0
gj1 = 1.1
gj2 = 1.1
xs1 = 1.1
xs2 = 1.1
gjt1 = 1.1
gjt2 = 1.1
xst1 = 1.1
xst2 = 1.1
result = parseexcel(mypathstr, total(), xy(), ylxx(), zy(), zcy(), gj1, gj2, xs1, xs2, gjt1, gjt2, xst1, xst2)'program have crashed here
DLL函数是
int __stdcall parseexcel(const char * thepath,double * total,double * xy,double * ylxx,double * zy,double * zcy,double & gj1,double & gj2,double & xs1,double & xs2,double gjt1,double gjt2,double xst1,double xst2 )
我做错了什么?
答案 0 :(得分:1)
C ++使用相当“哑”的数组,并且在任何情况下都默认不使用SAFEARRAY。所以你不能只是将一个指向SAFEARRAY的指针传递给它,而是想要一个指向BLOB数据的指针。
在VB6中,这通常只需通过传递第一个数组元素ByRef将指针传递给数组数据的开头即可实现:
Declare Function parseexcel Lib "parseexcelct.dll" ( _
ByVal thepath As String, _
ByRef total As Double, _
ByRef xy As Double, _
ByRef ylxx As Double, _
ByRef zy As Double, _
ByRef zcy As Double, _
ByRef gj1 As Double, _
ByRef gj2 As Double, _
ByRef xs1 As Double, _
ByRef xs2 As Double, _
ByVal gjt1 As Double, _
ByVal gjt2 As Double, _
ByVal xst1 As Double, _
ByVal xst2 As Double) As Long
result = parseexcel(mypathstr, _
total(0), _
xy(0), _
ylxx(0), _
zy(0), _
zcy(0), _
gj1, _
gj2, _
xs1, _
xs2, _
gjt1, _
gjt2, _
xst1, _
xst2)
这一点在VB6文档中有很好的介绍。