 |
forums.silverfrost.com Welcome to the Silverfrost forums
|
View previous topic :: View next topic |
Author |
Message |
Kent Lyons
Joined: 10 Dec 2009 Posts: 14 Location: Miami, FL USA
|
Posted: Wed May 19, 2010 6:28 pm Post subject: Passing arrays (or anything) from vb6 to FTN95 |
|
|
Hi,
I apologize for asking this question because it is similar to several other questions in both the General and Support forums, however, I have read those discussions several times and find I am still baffled regarding how to do this. The problem is probably that I'm an engineer and only have a vague understanding of what is going on, so please keep things simple - thanks.
Anyway, I wrote a long program in vb6 and find it is very slow so I decided to convert it to Fortran. However, I can't get even the most basic program to work - I am getting an overflow error from vb
OS - Vista 64 Business (unfortunately)
FTN95 V5.40
vb6 code:
Private Declare Sub AIRFOIL_CALC Lib "Z:\...\Release\Win32\AIRFOIL_CALC.dll" (x, y)
Private Sub Call_FTN95()
Dim x(1 To 2) As Long
Dim y(1 To 2) As Long
x(1) = 1
x(2) = 2
Call AIRFOIL_CALC(x, y)
End Sub
FTN95 code:
F_STDCALL Subroutine AIRFOIL_CALC (x,y)
Implicit none
Integer i
Real x(1:2), y(1:2)
Do i = 1, 2
y(i) = x(i) + 1
Enddo
Return
End Subroutine
Compiler options:
Debugging, optimization, numerical, language, source & diagnostics have nothing checked
Under Miscellaneous I have the following:
Output filename: Release\Win32\AIRFOIL_CALC.dll
Output filetype: DLL
Preprocess source files is checked
Linker options:
Export all is checked
I have also tried this with x & y as simple variables rather than arrays and I get the same result
Thanks for your help,
Kent |
|
Back to top |
|
 |
brucebowler Guest
|
Posted: Thu May 20, 2010 3:19 pm Post subject: |
|
|
I don't know for sure that this is the problem, but long is an integer type in VB, but your fortran code is expecting a real variable. |
|
Back to top |
|
 |
Kent Lyons
Joined: 10 Dec 2009 Posts: 14 Location: Miami, FL USA
|
Posted: Thu May 20, 2010 3:26 pm Post subject: |
|
|
Thanks Bruce,
That was stupid of me. I was expecting something really esoteric and it's probably right in front of me. I'll try your fix ASAP
Kent |
|
Back to top |
|
 |
Kent Lyons
Joined: 10 Dec 2009 Posts: 14 Location: Miami, FL USA
|
Posted: Thu May 20, 2010 3:44 pm Post subject: |
|
|
Thanks again for the help, but unfortunately that wasn't my only error. I modified things and I still get an overflow when I call the Fotran routine from vb
Modified Fortran:
F_STDCALL Subroutine AIRFOIL_CALC (x,y)
Implicit none
Integer i
Real*8 x(1:2), y(1:2)
Do i = 1, 2
y(i) = x(i) + 1
Enddo
Return
End Subroutine
Modified vb:
Private Sub Call_FTN95()
Dim x(1 To 2) As Double
Dim y(1 To 2) As Double
x(1) = 1
x(2) = 2
Call AIRFOIL_CALC(x, y)
End Sub
Kent |
|
Back to top |
|
 |
Kent Lyons
Joined: 10 Dec 2009 Posts: 14 Location: Miami, FL USA
|
Posted: Fri May 21, 2010 10:13 pm Post subject: |
|
|
After spending a day researching and trying random solutions I've found a fix, although I don't know why it works and why the original method did not. There are probably better methods, but at least this runs.
The fix appears to be to pass only the first element of each array rather than passing the entire array, then passing the dimension of each array separately. I have read that this methodology is incorrect and only works by some fluke, but it's the only method I could get to work. Regardless:
Modified vb6 code:
Private Declare Sub CALLED_BY_VB Lib "Z:\...\Release\Win32\CALLED_BY_VB.dll" (x as Double, y as Double, I_Dim as Long)
Private Sub Call_FTN95()
Dim x(2) As Double, y(2) as Double
Dim i_Dim as Long
x(1) = 1
x(2) = 2
I_Dim = 2
Call CALLED_BY_VB( x(1), y(1), I_Dim )
End Sub
Modified Fortran:
F_STDCALL Subroutine CALLED_BY_VB(x, y, I_Dim)
Implicit none
Integer*4 i, I_Dim
Real*8 :: x(I_Dim), y(I_Dim) !Note that specifying the starting element - i.e. X(1:I_Dim) does not appear to work even if a similar change is made in the vb code
Do i = 1, I_Dim
y(i) = x(i) + 1
Enddo
Return
End Subroutine
I hope this helps anyone else having problems with arrays,
Kent |
|
Back to top |
|
 |
Robert

Joined: 29 Nov 2006 Posts: 457 Location: Manchester
|
Posted: Fri May 21, 2010 11:00 pm Post subject: |
|
|
Kent
You are correct you should pass a reference to the first element. The FTN95 side expects that, a pointer to the first element in the array. If you put x(1) on the VB side then that is exactly what you are passing. However... if you put just x on the VB side you are passing a pointer to a description of the array - probably a SAFEARRAY structure.
http://msdn.microsoft.com/en-us/library/cc237826%28PROT.10%29.aspx |
|
Back to top |
|
 |
Kent Lyons
Joined: 10 Dec 2009 Posts: 14 Location: Miami, FL USA
|
Posted: Mon May 24, 2010 1:44 pm Post subject: |
|
|
Thanks for the clarification, Robert
I have Vista 64 and the following article made me worry about using that method:
http://software.intel.com/en-us/articles/visual-basic-passing-array-to-fortran-dll-in-x64/
My favorite quote was the MSDN comment "...so it was a total fluke that it worked simply be passing the first element ByRef anyway."
I probably have taken this out of context and it may have been referring to a much later of vb or something
Thanks again for your help,
Kent |
|
Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
Powered by phpBB © 2001, 2005 phpBB Group
|