Announcement

Collapse
No announcement yet.

Visual Basic Arrays and PB/DLL

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Visual Basic Arrays and PB/DLL

    I am currently trying to understand an extension of mine to a post under "Frequently Asked Questions" by Dave Navarro entitled "Visual Basic Arrays and PB/DLL". When I tried to extend his excellent suggestion for Long Arrays to Integer and Byte arrays, I received a compile error "BYREF Argument type mismatch" The modified VB6 code for the Integer case, which triggered the compile error, looked like the following:

    Private Sub Form_Load()
    Dim N As Integer
    Call Transfer(A(0), 5)
    For N = 0 To 4
    Text1.Text = Text1.Text & CStr(A(N)) & vbCrLf
    Next N
    End Sub


    Option Explicit
    Declare Sub Transfer Lib "D:\ArrayDLL_INT\Test.dll" (FirstElem As Long, ByVal Total As Integer)
    Global A(0 To 4) As Integer

    The associated PowerBasic code was:
    GLOBAL A()AS INTEGER
    SUB Transfer ALIAS "Transfer" (BYVAL FirstElem AS LONG, BYVAL Total AS INTEGER) EXPORT

    REDIM A(0 TO Total-1) AS INTEGER AT FirstElem
    DIM N AS INTEGER
    FOR N = 0 TO Total-1
    A(N) = 12345
    NEXT N
    END SUB


    To correct the compile error, I modified the VB6 code to the following:

    Private Sub Form_Load()

    Dim N As Integer
    Dim Address As Long
    Address = VarPtrArray(A())
    Call Transfer(Address, 5)
    For N = 0 To 4
    Text1.Text = Text1.Text & CStr(A(N)) & vbCrLf
    Next N
    End Sub

    Declare Sub Transfer Lib "D:\ArrayDLL_INT\Test.dll" _
    (ByVal FirstElem As Long, ByVal Total As Integer)

    Declare Function VarPtrArray Lib "msvbvm60.dll" _
    Alias "VarPtr" (Var() As Any) As Long

    Global A(0 To 4) As Integer

    I got the VarPtrArray function from the Microsoft Knowledge Base but this was the first time I have tried it. The VarPTRArray approach got rid of the compile error but the VB6 Array A() contained all zeros in lieu of the expected "12345". Furthermore, when I used this approach for the original A() dimensioned as a Long array, I also erroneously got all zeros in lieu of "12345" It seems that the VarPtrArray function doesn't work as I have applied it. Can you suggest an approach to remove the compile error which would work for both Integer and Byte arrays? What was wrong with the VarPtrArray approach that I used above?
Working...
X