2016-04-02 79 views
0

我有一個遺留項目是寫在很多年前,用VB6編寫的。由於VB 6太舊,我無法在Google上找到VB 6的足夠信息。我做錯了,我從VB6調用dll

我想從VB 6中調用一個用VC++編寫的DLL。問題是VB調用DLL時,VB已經崩潰。我認爲這是DLL的錯誤,然後我調試DLL。但是,我發現該DLL工作正常。它最終返回0,但VB傳遞雙數組不成功,但是DLL工作正常。然後當DLL執行完畢並返回到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 ) 

我做了什麼錯?

回答

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的文檔。

+0

[Here](https://msdn.microsoft.com/en-us/library/aa338032(v = VS.60).aspx)是MSDN VB6參考庫的(隱藏)鏈接。如你所見,微軟已經竭盡全力使VB6文檔難以找到。 – BobRodes