ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   call sub (https://www.excelbanter.com/excel-programming/278687-call-sub.html)

Claudia Dell'Era[_2_]

call sub
 
Hi!
I wrote the code I pasted at the end of the mail to call a
sub from a dll file. Apparently the code looks correct to
me but instead of returning the correct value of z it
gives me only zeros. Does someone of you see any mistake
for example in the use of the call sub? Thank you
Here is the code:

Declare Sub asum Lib "D:\Dll\arr\koedll.dll" (x As Double,
y As Double, z As Double, n As Integer)
Sub koelarr()
'
' koelarr Macro
' Macro written 10/3/2003 by Claudia Dell'Era Try to link
an array
'

'
Dim x() As Double, y() As Double, z() As Double
Dim n As Integer, i As Integer

Worksheets("Sheet2").Activate
n = Cells(2, 4)

ReDim x(1 To n) As Double, y(1 To n) As Double, z(1 To
n) As Double

Worksheets("Sheet2").Activate
For i = 1 To n
x(i) = Cells(i + 1, 1)
Next i

For i = 1 To n
y(i) = Cells(i + 1, 2)
Next i

Call asum(x(1), y(1), z(1), n)

For i = 1 To n
Cells(i + 1, 3) = z(i)
Next i

End Sub

Chip Pearson

call sub
 
Claudia,

Your code looks good to me, and a quick test with a DLL written in C++ bears
this out. How is your 'asum' function declared in the DLL? Remember an
integer in C is a Long in VBA.


--
Cordially,
Chip Pearson
Microsoft MVP - Excel
www.cpearson.com

"Claudia Dell'Era" wrote in message
...
Hi!
I wrote the code I pasted at the end of the mail to call a
sub from a dll file. Apparently the code looks correct to
me but instead of returning the correct value of z it
gives me only zeros. Does someone of you see any mistake
for example in the use of the call sub? Thank you
Here is the code:

Declare Sub asum Lib "D:\Dll\arr\koedll.dll" (x As Double,
y As Double, z As Double, n As Integer)
Sub koelarr()
'
' koelarr Macro
' Macro written 10/3/2003 by Claudia Dell'Era Try to link
an array
'

'
Dim x() As Double, y() As Double, z() As Double
Dim n As Integer, i As Integer

Worksheets("Sheet2").Activate
n = Cells(2, 4)

ReDim x(1 To n) As Double, y(1 To n) As Double, z(1 To
n) As Double

Worksheets("Sheet2").Activate
For i = 1 To n
x(i) = Cells(i + 1, 1)
Next i

For i = 1 To n
y(i) = Cells(i + 1, 2)
Next i

Call asum(x(1), y(1), z(1), n)

For i = 1 To n
Cells(i + 1, 3) = z(i)
Next i

End Sub




Claudia Dell'Era[_2_]

call sub
 
Hi Chip
Thanx for your answer. "asum" is a simple function
(x+y=z) I made to check if the "array passing" from the
dll to VBA works.The real program I have to use in
practice is much bigger but if already doesn't work with
this... The dll is written in Visual Fortran. I paste the
code since it is short. Thank you

! koedll.f90
!
! FUNCTIONS/SUBROUTINES exported from koedll.dll:
! koedll - subroutine
!
subroutine asum(x, y, z, n)

implicit double precision (a-h,o-z)
dimension x(n), y(n), z(n)

! Expose subroutine koedll to users of this DLL
!
!DEC$ ATTRIBUTES DLLEXPORT::asum
!DEC$ ATTRIBUTES ALIAS : "asum" :: asum

! Variables

! Body of koedll

do i = 1, n
z(i) = y(i) + x(i)
end do
return
end subroutine
-----Original Message-----
Claudia,

Your code looks good to me, and a quick test with a DLL

written in C++ bears
this out. How is your 'asum' function declared in the

DLL? Remember an
integer in C is a Long in VBA.


--
Cordially,
Chip Pearson
Microsoft MVP - Excel
www.cpearson.com

"Claudia Dell'Era" wrote in message
...
Hi!
I wrote the code I pasted at the end of the mail to

call a
sub from a dll file. Apparently the code looks correct

to
me but instead of returning the correct value of z it
gives me only zeros. Does someone of you see any mistake
for example in the use of the call sub? Thank you
Here is the code:

Declare Sub asum Lib "D:\Dll\arr\koedll.dll" (x As

Double,
y As Double, z As Double, n As Integer)
Sub koelarr()
'
' koelarr Macro
' Macro written 10/3/2003 by Claudia Dell'Era Try to

link
an array
'

'
Dim x() As Double, y() As Double, z() As Double
Dim n As Integer, i As Integer

Worksheets("Sheet2").Activate
n = Cells(2, 4)

ReDim x(1 To n) As Double, y(1 To n) As Double, z(1

To
n) As Double

Worksheets("Sheet2").Activate
For i = 1 To n
x(i) = Cells(i + 1, 1)
Next i

For i = 1 To n
y(i) = Cells(i + 1, 2)
Next i

Call asum(x(1), y(1), z(1), n)

For i = 1 To n
Cells(i + 1, 3) = z(i)
Next i

End Sub



.


Chip Pearson

call sub
 
Claudia,

I don't know Fortran, so I have no idea whether your Fortran code is correct
or not. Is the Fortran function expecting the variables to be passed "by
reference" (i.e., pointers) or "by value" (i.e., actual values)?


--
Cordially,
Chip Pearson
Microsoft MVP - Excel
www.cpearson.com


"Claudia Dell'Era" wrote in message
...
Hi Chip
Thanx for your answer. "asum" is a simple function
(x+y=z) I made to check if the "array passing" from the
dll to VBA works.The real program I have to use in
practice is much bigger but if already doesn't work with
this... The dll is written in Visual Fortran. I paste the
code since it is short. Thank you

! koedll.f90
!
! FUNCTIONS/SUBROUTINES exported from koedll.dll:
! koedll - subroutine
!
subroutine asum(x, y, z, n)

implicit double precision (a-h,o-z)
dimension x(n), y(n), z(n)

! Expose subroutine koedll to users of this DLL
!
!DEC$ ATTRIBUTES DLLEXPORT::asum
!DEC$ ATTRIBUTES ALIAS : "asum" :: asum

! Variables

! Body of koedll

do i = 1, n
z(i) = y(i) + x(i)
end do
return
end subroutine
-----Original Message-----
Claudia,

Your code looks good to me, and a quick test with a DLL

written in C++ bears
this out. How is your 'asum' function declared in the

DLL? Remember an
integer in C is a Long in VBA.


--
Cordially,
Chip Pearson
Microsoft MVP - Excel
www.cpearson.com

"Claudia Dell'Era" wrote in message
...
Hi!
I wrote the code I pasted at the end of the mail to

call a
sub from a dll file. Apparently the code looks correct

to
me but instead of returning the correct value of z it
gives me only zeros. Does someone of you see any mistake
for example in the use of the call sub? Thank you
Here is the code:

Declare Sub asum Lib "D:\Dll\arr\koedll.dll" (x As

Double,
y As Double, z As Double, n As Integer)
Sub koelarr()
'
' koelarr Macro
' Macro written 10/3/2003 by Claudia Dell'Era Try to

link
an array
'

'
Dim x() As Double, y() As Double, z() As Double
Dim n As Integer, i As Integer

Worksheets("Sheet2").Activate
n = Cells(2, 4)

ReDim x(1 To n) As Double, y(1 To n) As Double, z(1

To
n) As Double

Worksheets("Sheet2").Activate
For i = 1 To n
x(i) = Cells(i + 1, 1)
Next i

For i = 1 To n
y(i) = Cells(i + 1, 2)
Next i

Call asum(x(1), y(1), z(1), n)

For i = 1 To n
Cells(i + 1, 3) = z(i)
Next i

End Sub



.





All times are GMT +1. The time now is 06:00 PM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
ExcelBanter.com