如何在Visual Basic中使用Fortran dll [英] How to use a Fortran dll in Visual Basic

查看:79
本文介绍了如何在Visual Basic中使用Fortran dll的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

亲爱的所有人,
我有一个关于在Visual Basic项目中使用Fortran DLL的问题. Fortran DLL调用用户指定的外部函数(fcn).这是(简化的)DLL的Fortran代码(请注意,完整的DLL使用fcn函数进行一些计算,但我对其进行了简化以使DLL易于阅读):

Dear all,
I''ve got a question regarding the use of a Fortran DLL in a Visual Basic project. The Fortran DLL calls an external function (fcn) specified by the user. Here is the code in Fortran of the (simplified) DLL (please note that the full DLL do some calculations with the fcn function but I simplified it to make the DLL easy to read):

subroutine SOMME ( fcn, a,b,c, info)<br />
!  Parameters:<br />
!<br />
!    Input, <br />
!    external FCN, the name of the user-supplied subroutine which<br />
!    calculates the functions.  The routine should have the form:<br />
!<br />
!      subroutine fcn ( a, b, c, iflag )<br />
!<br />
!      real a<br />
!      real b<br />
!      real c<br />
!      integer ( kind = 4 ) iflag<br />
!<br />
!    The value of IFLAG should not be changed by FCN unless<br />
!    the user wants to terminate execution of the routine.<br />
!    In this case set IFLAG to a negative integer.<br />
!<br />
!    Input, real  a, number to sum.<br />
!<br />
!    Input, real  b, number to sum.<br />
!<br />
!    Ouput, real c, the functions evaluated at (a,b) -> c = a+b.<br />
!<br />
!    Output, integer  INFO, error flag.  If the user has terminated <br />
!    execution, INFO is set to the (negative) value of IFLAG. See the<br />
!    description of FCN. <br />
!<br />
  !DEC$ ATTRIBUTES DLLEXPORT, ALIAS : "SOMME" :: SOMME<br />
  implicit none<br />
  real    ( kind = 8 ) a<br />
  real    ( kind = 8 ) b<br />
  real    ( kind = 8 ) c<br />
  external             fcn<br />
  integer ( kind = 4 ) info<br />
  integer ( kind = 4 ) iflag<br />
  info = 0<br />
  iflag = 10<br />
  call fcn (a,b,c ,iflag)<br />
 <br />
  info = iflag	<br />
  return<br />
end


现在,在VB项目中,我想定义fcn并调用使用该fcn函数的fortran dll.这是VB代码:


Now, in the VB project, I would like to define the fcn and call the fortran dll that uses this fcn function. Here is the VB code :

Public Class Form1
    Public Msg As String
    Delegate Sub deleguefvecJCH(ByRef ad As Double, ByRef bd As Double, ByRef cd As Double, ByRef iflagd As Integer)
    Declare Sub SOMME Lib "fonction_dll.dll" (ByRef fcn As deleguefvecJCH, ByRef ad As Double, ByRef bd As Double, ByRef cd As Double, ByRef INFO As Integer)
    Public Sub fvecJCH(ByRef a As Double, ByRef b As Double, ByRef c As Double, ByRef iflag As Integer)
        ' eternal function to be calculate in the dll
        On Error GoTo Err_fvecJCH
        c = a + b
        Exit Sub
Err_fvecJCH:
        iflag = -1
        Msg = "fvecJCH : " & Err.Description
        MsgBox(Msg, 16, "Error")
    End Sub
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Dim a = 2, b = 1.5
        Dim c As Double
        Dim INFO As Integer = 20
        Dim adressefvecJCH As New deleguefvecJCH(AddressOf fvecJCH)
        Dim iflag = 20
        ' dll call
        SOMME(adressefvecJCH, a, b, c, INFO)
        a = 1
    End Sub
End Class


当我尝试编译VB代码时,会弹出以下错误消息试图读取或写入受保护的内存".
有人知道如何调试吗?
谢谢您的帮助,
Juliette


When I try to compile the VB code, the following error message pops up "attempted to read or write protected memory".
Does anyone know how to debug that?
Thank you your help,
Juliette

推荐答案

属性DLLEXPORT,别名:"SOMME" :: SOMME< br/> 隐式none< br/> 实数(kind = 8)a< br/> 实数(kind = 8)b< br/> 实数(kind = 8)c< br/> 外部fcn< br/> 整数(kind = 4)info< br/> 整数(kind = 4)iflag< br/> 信息= 0< br/> iflag = 10< br/> 呼叫fcn(a,b,c,iflag)< br/> < br/> 信息= iflag< br/> return< br/> 结束
ATTRIBUTES DLLEXPORT, ALIAS : "SOMME" :: SOMME<br /> implicit none<br /> real ( kind = 8 ) a<br /> real ( kind = 8 ) b<br /> real ( kind = 8 ) c<br /> external fcn<br /> integer ( kind = 4 ) info<br /> integer ( kind = 4 ) iflag<br /> info = 0<br /> iflag = 10<br /> call fcn (a,b,c ,iflag)<br /> <br /> info = iflag <br /> return<br /> end


现在,在VB项目中,我想定义fcn并调用使用该fcn函数的fortran dll.这是VB代码:


Now, in the VB project, I would like to define the fcn and call the fortran dll that uses this fcn function. Here is the VB code :

Public Class Form1
    Public Msg As String
    Delegate Sub deleguefvecJCH(ByRef ad As Double, ByRef bd As Double, ByRef cd As Double, ByRef iflagd As Integer)
    Declare Sub SOMME Lib "fonction_dll.dll" (ByRef fcn As deleguefvecJCH, ByRef ad As Double, ByRef bd As Double, ByRef cd As Double, ByRef INFO As Integer)
    Public Sub fvecJCH(ByRef a As Double, ByRef b As Double, ByRef c As Double, ByRef iflag As Integer)
        ' eternal function to be calculate in the dll
        On Error GoTo Err_fvecJCH
        c = a + b
        Exit Sub
Err_fvecJCH:
        iflag = -1
        Msg = "fvecJCH : " & Err.Description
        MsgBox(Msg, 16, "Error")
    End Sub
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Dim a = 2, b = 1.5
        Dim c As Double
        Dim INFO As Integer = 20
        Dim adressefvecJCH As New deleguefvecJCH(AddressOf fvecJCH)
        Dim iflag = 20
        ' dll call
        SOMME(adressefvecJCH, a, b, c, INFO)
        a = 1
    End Sub
End Class


当我尝试编译VB代码时,会弹出以下错误消息试图读取或写入受保护的内存".
有人知道如何调试吗?
谢谢您的帮助,
Juliette


When I try to compile the VB code, the following error message pops up "attempted to read or write protected memory".
Does anyone know how to debug that?
Thank you your help,
Juliette


我的第一个怀疑是DLL
My first suspicion would be the recursive call in the DLL
call fcn (a,b,c ,iflag)


它可能会耗尽连续的内存块,而Windows阻止了溢出到受保护的内存中.

问候


It could be eating up contiguous blocks of memory and Windows is preventing an overflow into protected memory.

Regards


这篇关于如何在Visual Basic中使用Fortran dll的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆