5-5 VB与Fortran的混合编程 下载本文

内容发布更新时间 : 2024/5/14 13:37:54星期一 下面是文章的全部内容请认真阅读。

!MS$ attributes alias :'subplus'::subplus !MS$ attributes value::a,b !MS$ attributes reference::c

subroutine subsubtract(a,b,c) !MS$ attributes dllexport :: subsubtract !MS$ attributes alias :'subsubtract'::subsubtract !MS$ attributes value::a,b !MS$ attributes reference::c

subroutine submultiply(a,b,c) real(8)::a,b,c c=a-b return

end subroutine subsubtract real(8)::a,b,c c=a+b return

end subroutine subplus

!MS$ attributes dllexport :: submultiply !MS$ attributes alias :'submultiply'::submultiply !MS$ attributes value::a,b !MS$ attributes reference::c

11

real(8)::a,b,c c=a*b return

end subroutine submultiply

subroutine subdivide(a,b,c)

!MS$ attributes dllexport :: subdivide !MS$ attributes alias :'subdivide'::subdivide !MS$ attributes value::a,b !MS$ attributes reference::c

real(8)::a,b,c c=a/b return

end subroutine subdivide

*************subroutine********************

而后在项目中另添加一个名为mathfunction2的固定格式源文件,添加如下代码: *************function******************** function funplus(a,b) !MS$ attributes dllexport :: funplus !MS$ attributes alias :'funplus'::funplus !MS$ attributes value::a,b

function funsubtract(a,b) !MS$ attributes dllexport :: funsubtract !MS$ attributes alias :'funsubtract'::funsubtract !MS$ attributes value::a,b

12

real(8)::a,b,funplus funplus=a+b return

end function funplus

function funmultiply(a,b) real(8)::a,b,funsubtract funsubtract=a-b return

end function funsubtract

!MS$ attributes dllexport :: funmultiply !MS$ attributes alias :'funmultiply'::funmultiply !MS$ attributes value::a,b

function fundivide(a,b) !MS$ attributes dllexport :: fundivide !MS$ attributes alias :'fundivide'::fundivide !MS$ attributes value::a,b

real(8)::a,b,fundivide fundivide=a/b return

end function fundivide real(8)::a,b,funmultiply funmultiply=a*b return

end function funmultiply

*************function********************

这些函数和过程都很简单,主要的运算代码都只有一条。函数和对应的过程可以完成同

13

样的功能,这样重复写只是为了说明在VB中对过程和函数在声明格式和调用方法上的细微差异。这两个文件也可以写在一个文件里,或者每个函数或过程独立写一个文件,这只是为了编写程序的方便,对其在VB的调用没有任何影响。

编写好源代码后编译生成Dll文件。随后我们会在该项目所在目录下的debug文件夹中找到这个名为mathfunction.dll的文件。将这个Dll文件拷贝到C:\\WINDOWS\\system。

2 在VB2005中新建一个名为vb_fortran的Windows Application项目,设计界面,添加三个Label,三个Textbox和四个Button,将其 text属性分别设置如下图所示:

图中的文本框从上到下依次为textbox1,textbox2,textbox3,按钮从上到下依次为button1,button2,button3,button4,在代码编辑器中输入以下代码:

Public Class Form1

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click Dim a As Double Dim b As Double Dim c As Double

a = CDbl(TextBox1.Text) b = CDbl(TextBox2.Text) 'Call subplus(a, b, c) c = funplus(a, b) TextBox3.Text = c End Sub

14

Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click Dim a As Double Dim b As Double Dim c As Double

a = CDbl(TextBox1.Text) b = CDbl(TextBox2.Text) 'Call subsubtract(a, b, c) c = funsubtract(a, b) TextBox3.Text = c End Sub

Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click Dim a As Double Dim b As Double Dim c As Double

a = CDbl(TextBox1.Text) b = CDbl(TextBox2.Text) 'Call submultiply(a, b, c) c = funmultiply(a, b) TextBox3.Text = c End Sub

Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click Dim a As Double Dim b As Double Dim c As Double

a = CDbl(TextBox1.Text) b = CDbl(TextBox2.Text) 'Call subdivide(a, b, c) c = fundivide(a, b) TextBox3.Text = c End Sub

End Class

上面的代码也很简单。代码中将对过程的调用注释掉了,因为这里对过程和对函数的调用结果是一样的,任选其一都可以,注意调用的格式就可以了。

在项目文件中添加一个模块文件,输入以下代码: Module Module1

Public Declare Sub subplus Lib \ (ByVal a As Double, ByVal b As Double, ByRef c As Double)

15