Passing arrays from FORTRAN dll from VB 
Author Message
 Passing arrays from FORTRAN dll from VB

I'm trying to pass an array from a Watcom fortran11B DLL to VB4-32
program.  I've been able to figure out how to pass scalars back and
forth, but not an array.  Here's an example of my latest attempt:

VB
************************************************************************
[General]               [declarations]

'Try it with just an arrays and as a sub
Private Declare Sub SubF77 Lib "C:\MYDIR\dllsampf.dll"   (ByRef a As
Single)

Private Sub cmdF77Function_Click()
   Dim a(1 To 3) As Single
   SubF77 (a)
   For i = 1 To 3
      printa (i)
   Next i
End Sub

End of VB
************************************************************************

Beginning of FORTRAN
*************************************************************

*$pragma aux (__stdcall) SUBF77 "SubF77" export parm(reference)
      SUBROUTINE SUBF77( D )

      REAL D(3)  !Added an array

      CALL SETD(D)

      RETURN
      END

      SUBROUTINE SETD(D)

      REAL D(3)

      D(1) = 1.
      D(2) = 2.
      D(3) = 3.

      RETURN
      END

End of FORTRAN
*************************************************************

When I try to run the VB I get an "array argument must be byref."  I
understand that DVF wants you to just pass the first element, not sure
how you would get the rest of the elements though, also not sure if this
will work with Watcom.  

Is there anyway that I can use a private type for the argument?  I
attempted this earlier, but basically got the same problems.

ie. I earlier tried the following:

[General]               [declarations]

Private Type RealArray
   MyArray(1 To 3) As Single
End Type

Private Declare Sub SubF77 Lib "C:\MYDIR\dllsampf.dll"  (ByRef a As
RealArray)

Private Sub cmdF77Function_Click()
   Dim a(1 To 3) As RealArray
   SubF77 (a)
   For i = 1 To 3
      printa (i)
   Next i
End Sub

As far as I can tell, the FORTRAN doesn't have any problems with
returning arrays as arguments.  It doesn't generate any errors when I
compile it.

--
To respond via e-mail, please remove what's between Eric and Goforth in
my address in order to get my real e-mail address.



Mon, 26 Mar 2001 03:00:00 GMT  
 Passing arrays from FORTRAN dll from VB
I got it to work!  I had seen some postings related to doing the same
thing with DVF and VB5.0 that said that you should just pass the first
element in an array as a scalar and apparently the rest of the array
will follow.  It sounded like wishful thinking, but after some
experimentation I got it to work.  I used Watcom FORTRAN11b, VB4-32
Professional, Win95 OSR2.  Here's my source code:

Beginning of VB Code
******************************************************

[General]                        [declarations]
Private Declare Sub SubF77 Lib
"C:\WATCOM\samples\fortran\win\dll\FromWeb\Modified\Just1st\dllsampf.dll"
(ByRef a As Single)
Option Base 1   'Unnecessary? Dim a(1 To 3) As Single

Private Sub cmdF77Function_Click()

   Dim a(1 To 3) As Single

'Either way this will work
'   Call SubF77(a(1))
   SubF77 a(1)

   MsgBox "a(1) = " & a(1)
   MsgBox "a(2) = " & a(2)
   MsgBox "a(3) = " & a(3)

   Refresh
   DoEvents

End Sub

End of VB Code
*************************************************************

Beginning of FORTRAN Code
**********************************************************

*$pragma aux (__stdcall) SUBF77 "SubF77" export parm(reference)
      SUBROUTINE SUBF77( D )

!Changed REAL to REAL*4 although I'm pretty sure that they are the same.
      REAL*4 D(3)  !Added an array

      CALL SETD(D)

      RETURN
      END

      SUBROUTINE SETD(D)

      REAL*4 D(3)

      D(1) = 1.
      D(2) = 2.
      D(3) = 3.

      RETURN
      END

End of of FORTRAN Code
*************************************************************

Beginning of Batch file used to compile the FORTAN
*********************************

wfc386 /d2 /bd dllsampf.for
wlink d all sys nt_dll initi termi n dllsampf f dllsampf l nt.lib

End of Batch file used to compile the FORTAN
***************************************

--
To respond via e-mail, please remove what's between Eric and Goforth in
my address in order to get my real e-mail address.

--
To respond via e-mail, please remove what's between Eric and Goforth in
my address in order to get my real e-mail address.



Tue, 27 Mar 2001 03:00:00 GMT  
 Passing arrays from FORTRAN dll from VB
I got it to work!  I had seen some postings related to doing the same
thing with DVF and VB5.0 that said that you should just pass the first
element in an array as a scalar and apparently the rest of the array
will follow.  It sounded like wishful thinking, but after some
experimentation I got it to work.  I used Watcom FORTRAN11b, VB4-32
Professional, Win95 OSR2.  Here's my source code:

Beginning of VB Code
******************************************************

[General]                        [declarations]
Private Declare Sub SubF77 Lib
"C:\WATCOM\samples\fortran\win\dll\FromWeb\Modified\Just1st\dllsampf.dll"
(ByRef a As Single)
Option Base 1   'Unnecessary? Dim a(1 To 3) As Single

Private Sub cmdF77Function_Click()

   Dim a(1 To 3) As Single

'Either way this will work
'   Call SubF77(a(1))
   SubF77 a(1)

   MsgBox "a(1) = " & a(1)
   MsgBox "a(2) = " & a(2)
   MsgBox "a(3) = " & a(3)

   Refresh
   DoEvents

End Sub

End of VB Code
*************************************************************

Beginning of FORTRAN Code
**********************************************************

*$pragma aux (__stdcall) SUBF77 "SubF77" export parm(reference)
      SUBROUTINE SUBF77( D )

!Changed REAL to REAL*4 although I'm pretty sure that they are the same.
      REAL*4 D(3)  !Added an array

      CALL SETD(D)

      RETURN
      END

      SUBROUTINE SETD(D)

      REAL*4 D(3)

      D(1) = 1.
      D(2) = 2.
      D(3) = 3.

      RETURN
      END

End of of FORTRAN Code
*************************************************************

Beginning of Batch file used to compile the FORTAN
*********************************

wfc386 /d2 /bd dllsampf.for
wlink d all sys nt_dll initi termi n dllsampf f dllsampf l nt.lib

End of Batch file used to compile the FORTAN
***************************************
--
To respond via e-mail, please remove what's between Eric and Goforth in
my address in order to get my real e-mail address.

--
To respond via e-mail, please remove what's between Eric and Goforth in
my address in order to get my real e-mail address.



Tue, 27 Mar 2001 03:00:00 GMT  
 Passing arrays from FORTRAN dll from VB


Quote:
>I got it to work!  I had seen some postings related to doing the same
>thing with DVF and VB5.0 that said that you should just pass the first
>element in an array as a scalar and apparently the rest of the array
>will follow.  It sounded like wishful thinking, but after some
>experimentation I got it to work.  I used Watcom FORTRAN11b, VB4-32
>Professional, Win95 OSR2.  Here's my source code:

>--
>To respond via e-mail, please remove what's between Eric and Goforth in
>my address in order to get my real e-mail address.

>--
>To respond via e-mail, please remove what's between Eric and Goforth in
>my address in order to get my real e-mail address.

Hello Eric,

Actually, the rest of the array does not "follow"...what is happening is that VB stores numeric arrays in contiguous memory addresses (this is the original theory of array structures) and when you pass the first element you are passing the address of the first element only, but since all the other elements are contiguous in memory the receiving code simply needs to increment this address by multiples of the size in bytes of the array element in order to get the next element.  This is the same way the most other languages pass arrays as parameters; the syntax may specify the whole array or just the first element depending upon the language and implementation but in either case it is the address of the first element that is passed.

Hope this info helps your understanding of the "mechanics" involved.

Charles


"For God So Loved The World, That He Gave His
 Only Begotten Son, That Whosoever Believeth
 In Him Should Not Perish, But Have Everlasting
 Life"John3:16  * http://pw2.netcom.com/~cbrtjr/wrdthing.html *



Wed, 28 Mar 2001 03:00:00 GMT  
 
 [ 4 post ] 

 Relevant Pages 

1. Passing Arrays to Fortran DLL from VB

2. Passing string Arrays from Fortran DLL to Visual Basic

3. Passing VB strings to Lahey FORTRAN DLLs

4. Passing array from VB to DLL and returning array

5. Passing arrays from VB5 to MS FORTRAN PwrStn

6. passing arrays from vb dll to vb form

7. HELP: passing structure from VBA to Fortran DLL

8. Problem : Unable to pass an integer value from Visual Basic to Fortran DLL

9. HELP: passing structure from VBA to Fortran DLL

10. HELP: passing structure from VBA to Fortran DLL

11. PB/DLL passing multidimensional VB arrays by pointers

12. Passing Array of data from DLL to VB

 

 
Powered by phpBB® Forum Software