Click here to Skip to main content
14,932,748 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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

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 :
VB
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

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
Posted
Comments
   
(Sigh...) In what kind of universe do you leave?
--SA

1 solution

My first suspicion would be the recursive call in the DLL
VB
call fcn (a,b,c ,iflag)

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

Regards
   

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900