Arrays in function argument list

VB6 is quite happy to let me declare a subroutine (in a STDCALL linked DLL) using Arrays in the argument list.  

What is less clear is how do I call a subroutine with arrays in the argument list?
Who is Participating?
trilloConnect With a Mentor Commented:
To pass an array to a DLL, or any other api, you should pass the adress of the first item in your array. This means:

Call doStuff (mode, Level(0))
An example:

Private Sub Command1_Click()
    Dim cA(10) As Long
    cA(0) = 10
    ArrayInArg cA
End Sub

Private Function ArrayInArg(cArray() As Long)
    Debug.Print cArray(0)
End Function
The above code passes the array ByRef, so if you modify cA, cArray will also change.  You can make a local copy of cArray in ArrayInArg without modifying the original array:

Private Function ArrayInArg(cArray() As Long)
    Dim cArr 'Variant
    cArr = cArray() 'Copy of cArray
    cArr(0) = 5
    Debug.Print cArr(0)
End Function
The new generation of project management tools

With’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

griffittAuthor Commented:
Apologies Erick37 - I don't think my question was well wordeed :-)

Perhaps I should have given an example of what I am doing.

In the Module:

Public Declare Sub DoStuff Lib "stuff.dll" Alias "DOSTUFF" (ByRef mode As Integer, ByRef Level() As Double)

In the Form:
Dim mode as Integer
Dim Level(16) as Double

Call DoStuff(mode,Level)

This gives a type mismatch error for Level.  I can't see why it doesn't work!!
griffittAuthor Commented:
Thanks Trillo - you were close enough to get the cigar ;-)

It actually turns out that you need to use:

Call doStuff (mode, Level())

with empty parenthesis!

The other problem that was catching me out was that in my 'genuine' code the subroutine is called using 34 arguments (most of which are arrays).  When I was defining these in my code each must be defined seperately. ie.

Dim A(10) as Double, B(10) as Double

rather than

Dim A(10),B(10) as Double

Don't ask me why!
Most DLLs which accept arrays declare it as a void *, and should be declared in VB with the Any type.

Public Declare Sub DoStuff Lib "stuff.dll" Alias "DOSTUFF" (ByRef mode As Integer, ByRef pLevel As Any)

Then you pass Level(0) to the pointer.
All Courses

From novice to tech pro — start learning today.