Silverfrost Forums

Welcome to our forums

Passing arrays (or anything) from vb6 to FTN95

19 May 2010 5:28 #6384

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

20 May 2010 2:26 #6396

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

20 May 2010 2:44 #6397

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

21 May 2010 9:13 #6402

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 Integer4 i, I_Dim Real8 :: 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

21 May 2010 10:00 #6403

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

24 May 2010 12:44 #6408

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

Please login to reply.