主题:关于vb调用Fortran的问题(小弟求求各位了)
joserfstalin
[专家分:0] 发布于 2010-04-01 21:03:00
我用vb调用dll
下面是Fortran程序:
SUBROUTINE arraytest(n,arr,s)
$ATTRIBUTES DLLEXPORT :: arraytest
INTEGER n
REAL arr(n),s
s=0.
DO i=1,n
s=s+arr(i)
END DO
END SUBROUTINE
vb代码:
Private Declare Sub ARRAYTEST Lib "E:\ss.dll" Alias "_ARRAYTEST@12" (X As Long, Y As Double, z As Double)
Private Sub form_Click()
Dim n As Long
Dim ab(100) As Double
n = 3
For i = 1 To n
ab(i) = i
Next i
Call ARRAYTEST(n, ab(1), s)
Print s
End Sub
最后的结果我想得到的是6,但是是0
小弟百思不得其解,求大侠相助
最后更新于:2010-04-02 09:34:00
回复列表 (共9个回复)
沙发
forcal [专家分:80] 发布于 2010-04-01 21:18:00
vb是不是默认按值传递参数,改成按地址传递试试。
3 楼
forcal [专家分:80] 发布于 2010-04-01 21:37:00
SUBROUTINE arraytest(n,arr,s)
$ATTRIBUTES DLLEXPORT :: arraytest
INTEGER n
REAL arr(n),s
s=0.
DO i=1,n
s=s+arr(i)
END DO
END SUBROUTINE
5 楼
trimtrim [专家分:1640] 发布于 2010-04-02 09:49:00
最好申明变量中的参数是按地址传递,还是按照值传递,附上一个例子函数的头例子:
SUBROUTINE wave (ITYPE,DEPTH,WT,WH,NP,NS,NF,FLAG,PULSE,TRAN,SP1)
!DEC$ ATTRIBUTES DLLEXPORT::wave
!DEC$ ATTRIBUTES STDCALL,ALIAS::'wave'::wave !标准调用,导出名为wave
!DEC$ ATTRIBUTES VALUE::ITYPE,DEPTH,WT,WH,NP,NS,NF !规定参数为值传递
!DEC$ ATTRIBUTES REFERENCE::PULSE,TRAN,SP1,FLAG !规定参数为引用传递
!以下为变量声明
REAL*4 FREQ(NF),SP1(NF),DRIV(NF)
REAL*4 COR(NF),TRAN(NF)
REAL*4 RAN0(NF),WLEN(NF),WT,WH,DEPTH
REAL*4 AR(NP),ARR(NP),AMP,ANGLE
INTEGER PULSE(NP),FLAG,ITYPE,NP
6 楼
joserfstalin [专家分:0] 发布于 2010-04-02 15:29:00
我这样也不行啊:
Private Declare Sub ARRAYTEST Lib "E:\ss.dll" Alias "_ARRAYTEST@12" (ByRef X As Long, ByRef Y As Double, ByRef z As Double)
Private Sub form_Click()
Dim n As Long
Dim ab(100) As Double
n = 3
For i = 1 To n
ab(i) = i
Print ab(i)
Next i
Call ARRAYTEST(n, ab(1), s)
Print s
End Sub
SUBROUTINE arraytest(n,arr,s)
$ATTRIBUTES DLLEXPORT :: arraytest
!DEC$ ATTRIBUTES REFERENCE::n,arr,s !规定参数为引用传递
INTEGER,intent(in)::n
REAL*4,intent(in)::arr(n)
real*4,intent(out)::s
s=0.
DO i=1,n
s=s+arr(i)
END DO
END SUBROUTINE
7 楼
trimtrim [专家分:1640] 发布于 2010-04-02 18:30:00
Dim ab(100) As Double
REAL*4,intent(in)::arr(n)
有问题。
REAL*4,intent(in)::arr(n) -〉REAL*8,intent(in)::arr(n)
9 楼
joserfstalin [专家分:0] 发布于 2010-04-04 12:56:00
这样最后结果是6,好人啊,叫你一声大哥
我来回复