From: aeroguy on
I am calling a fortran DLL from excel which passes an array to fortran
program.

The array values are incrimented by 10.

Its crishing the program. Why? and how do i resolve this.

Full code presented below



Subroutine FortranDLL( Array1, upbound )
!DEC$ ATTRIBUTES DLLEXPORT, ALIAS: 'FORTRANDLL' :: FortranDLL
!DEC$ ATTRIBUTES REFERENCE :: Array1
!DEC$ ATTRIBUTES REFERENCE :: upbound
Implicit None
! ...argument declarations
Integer :: upbound
Integer :: Array1(1:upbound)

! Local variables
Integer :: i

do i=1,upbound
Array1(i)=Array1(i)+10
end do

End Subroutine FortranDLL


In Excel i did this


Declare Sub FortranDLL Lib "C:\TEMP\FcallA.dll" Alias
"FORTRANDLL" (Array1() As Long, ByVal upbound As Long)



Sub CommandButton1_Click()
Dim II As Long
Dim test(5) As Long

For I = 0 To 5
test(I) = I
Next I
II = 6

Call FortranDLL(test, II)
For I = 0 To 5
Debug.Print test(I)
Next I

End Sub


I am using Visual studio and Intel Fortran Compiler 10.0


On execution Excel aborts and kills itself.

Why? and how do i resolve this. am i doing something wrong?

Thanks for your time.

From: e p chandler on
On Dec 4, 10:13 am, aeroguy <sukhbinder.si...(a)gmail.com> wrote:
> I am calling a fortran DLL from excel which passes an array to fortran
> program.
>
> The array values are incrimented by 10.
>
> Its crishing the program. Why? and how do i resolve this.

Arguments must agree in number, type, calling convention and name
decoration.

>
> Full code presented below
>
> Subroutine FortranDLL( Array1, upbound )
> !DEC$ ATTRIBUTES DLLEXPORT, ALIAS: 'FORTRANDLL' :: FortranDLL

Try adding ,STDCALL here.

> !DEC$ ATTRIBUTES REFERENCE :: Array1
> !DEC$ ATTRIBUTES REFERENCE :: upbound
> Implicit None
> ! ...argument declarations
> Integer :: upbound
> Integer :: Array1(1:upbound)
>
> ! Local variables
> Integer :: i
>
> do i=1,upbound
> Array1(i)=Array1(i)+10
> end do
>
> End Subroutine FortranDLL
>
> In Excel i did this
>
> Declare Sub FortranDLL Lib "C:\TEMP\FcallA.dll" Alias
> "FORTRANDLL" (Array1() As Long, ByVal upbound As Long)

Try ByRef (or omit it).

>
> Sub CommandButton1_Click()
> Dim II As Long
> Dim test(5) As Long
>
> For I = 0 To 5
> test(I) = I
> Next I
> II = 6
>
> Call FortranDLL(test, II)
> For I = 0 To 5
> Debug.Print test(I)
> Next I
>
> End Sub
>
> I am using Visual studio  and Intel Fortran Compiler 10.0
>
> On execution Excel aborts and kills itself.

Excel / VBA is fussy. Often the error message you see is not at all
related to the actual problem.

If Fortran passes a descriptor for the array along with the array,
then you have to account for it on the VBA side. The REFERENCE keyword
may eliminate the descriptor. Otherwise, the old fashioned way is to
pass ARRAY(1) instead of ARRAY.

> Why? and how do i resolve this. am i doing something wrong?
>
> Thanks for your time.

Mixed language programming is magic!

--- e

From: aeroguy on
On Dec 4, 9:01 pm, e p chandler <e...(a)juno.com> wrote:
> On Dec 4, 10:13 am, aeroguy <sukhbinder.si...(a)gmail.com> wrote:
>
> > I am calling a fortran DLL from excel which passes an array to fortran
> > program.
>
> > The array values are incrimented by 10.
>
> > Its crishing the program. Why? and how do i resolve this.
>
> Arguments must agree in number, type, calling convention and name
> decoration.
>
>
>
> > Full code presented below
>
> > Subroutine FortranDLL( Array1, upbound )
> > !DEC$ ATTRIBUTES DLLEXPORT, ALIAS: 'FORTRANDLL' :: FortranDLL
>
> Try adding ,STDCALL here.
>
>
>
>
>
> > !DEC$ ATTRIBUTES REFERENCE :: Array1
> > !DEC$ ATTRIBUTES REFERENCE :: upbound
> > Implicit None
> > ! ...argument declarations
> > Integer :: upbound
> > Integer :: Array1(1:upbound)
>
> > ! Local variables
> > Integer :: i
>
> > do i=1,upbound
> > Array1(i)=Array1(i)+10
> > end do
>
> > End Subroutine FortranDLL
>
> > In Excel i did this
>
> > Declare Sub FortranDLL Lib "C:\TEMP\FcallA.dll" Alias
> > "FORTRANDLL" (Array1() As Long, ByVal upbound As Long)
>
> Try ByRef (or omit it).
>
>
>
>
>
>
>
> > Sub CommandButton1_Click()
> > Dim II As Long
> > Dim test(5) As Long
>
> > For I = 0 To 5
> > test(I) = I
> > Next I
> > II = 6
>
> > Call FortranDLL(test, II)
> > For I = 0 To 5
> > Debug.Print test(I)
> > Next I
>
> > End Sub
>
> > I am using Visual studio  and Intel Fortran Compiler 10.0
>
> > On execution Excel aborts and kills itself.
>
> Excel / VBA is fussy. Often the error message you see is not at all
> related to the actual problem.
>
> If Fortran passes a descriptor for the array along with the array,
> then you have to account for it on the VBA side. The REFERENCE keyword
> may eliminate the descriptor. Otherwise, the old fashioned way is to
> pass ARRAY(1) instead of ARRAY.
>
> > Why? and how do i resolve this. am i doing something wrong?
>
> > Thanks for your time.
>
> Mixed language programming is magic!
>
> --- e- Hide quoted text -
>
> - Show quoted text -- Hide quoted text -
>
> - Show quoted text -

Mixed language programming is magic! --- Very true..... tried the
options excel throwing errors of type mismatch if i pass array(1)
From: Craig Powers on
aeroguy wrote:
> I am calling a fortran DLL from excel which passes an array to fortran
> program.
>
> The array values are incrimented by 10.
>
> Its crishing the program. Why? and how do i resolve this.
>
> Full code presented below
>
>
>
> Subroutine FortranDLL( Array1, upbound )
> !DEC$ ATTRIBUTES DLLEXPORT, ALIAS: 'FORTRANDLL' :: FortranDLL

As noted, you also need the STDCALL attribute for VB.

> !DEC$ ATTRIBUTES REFERENCE :: Array1
> !DEC$ ATTRIBUTES REFERENCE :: upbound
> Implicit None
> ! ...argument declarations
> Integer :: upbound
> Integer :: Array1(1:upbound)
>
> ! Local variables
> Integer :: i
>
> do i=1,upbound
> Array1(i)=Array1(i)+10
> end do
>
> End Subroutine FortranDLL
>
>
> In Excel i did this
>
>
> Declare Sub FortranDLL Lib "C:\TEMP\FcallA.dll" Alias
> "FORTRANDLL" (Array1() As Long, ByVal upbound As Long)

Change Array1() to Array1 (no parens), change the second ByVal to ByRef
(or omitted).

> Sub CommandButton1_Click()
> Dim II As Long
> Dim test(5) As Long
>
> For I = 0 To 5
> test(I) = I
> Next I
> II = 6
>
> Call FortranDLL(test, II)

Pass test(1) instead of test. VB arrays are Windows SafeArrays, unless
you want to handle them explicitly on the Fortran side, you need to pass
the first element by reference.

> On execution Excel aborts and kills itself.

By "aborts and kills itself" do you mean that it simply dies? If it
gives you an error message, and you're asking questions about why stuff
doesn't work, ALWAYS ALWAYS ALWAYS post the EXACT content of the error
message. Even if it looks completely unhelpful to you, someone else
with sufficient experience may be able to glean useful information out
of it.
From: aeroguy on
On Dec 5, 1:40 am, Craig Powers <craig.pow...(a)invalid.invalid> wrote:
> aeroguy wrote:
> > I am calling a fortran DLL from excel which passes an array to fortran
> > program.
>
> > The array values are incrimented by 10.
>
> > Its crishing the program. Why? and how do i resolve this.
>
> > Full code presented below
>
> > Subroutine FortranDLL( Array1, upbound )
> > !DEC$ ATTRIBUTES DLLEXPORT, ALIAS: 'FORTRANDLL' :: FortranDLL
>
> As noted, you also need the STDCALL attribute for VB.
>
>
>
>
>
> > !DEC$ ATTRIBUTES REFERENCE :: Array1
> > !DEC$ ATTRIBUTES REFERENCE :: upbound
> > Implicit None
> > ! ...argument declarations
> > Integer :: upbound
> > Integer :: Array1(1:upbound)
>
> > ! Local variables
> > Integer :: i
>
> > do i=1,upbound
> > Array1(i)=Array1(i)+10
> > end do
>
> > End Subroutine FortranDLL
>
> > In Excel i did this
>
> > Declare Sub FortranDLL Lib "C:\TEMP\FcallA.dll" Alias
> > "FORTRANDLL" (Array1() As Long, ByVal upbound As Long)
>
> Change Array1() to Array1 (no parens), change the second ByVal to ByRef
> (or omitted).
>
> > Sub CommandButton1_Click()
> > Dim II As Long
> > Dim test(5) As Long
>
> > For I = 0 To 5
> > test(I) = I
> > Next I
> > II = 6
>
> > Call FortranDLL(test, II)
>
> Pass test(1) instead of test.  VB arrays are Windows SafeArrays, unless
> you want to handle them explicitly on the Fortran side, you need to pass
> the first element by reference.
>
> > On execution Excel aborts and kills itself.
>
> By "aborts and kills itself" do you mean that it simply dies?  If it
> gives you an error message, and you're asking questions about why stuff
> doesn't work, ALWAYS ALWAYS ALWAYS post the EXACT content of the error
> message.  Even if it looks completely unhelpful to you, someone else
> with sufficient experience may be able to glean useful information out
> of it.
There is no error message from excel. it just closes and i get the
standard warning that excel gives, asking me if i want to restart
excel.

and when i put the breakpoint at the fortran call. this is where excel
just closes..