Silverfrost Forums

Welcome to our forums

FTN95 Express and VB 2010 Express Compatibility

15 Oct 2012 12:14 #10836

I have the need to update a .NET application that I wrote in Visual Basic 2008 Express a couple years ago that calls a Win32 DLL that I compiled using Silverfrost FTN95 Express. The problem quickly arises as soon as the Fortran DLL subroutine is called. A sample error that I get is:

PInvokeStackImbalance was detected Message: A call to PInvoke function 'Test Functions!Test_Functions.Form1::FSUB' has unbalanced the stack. This is likely because the managed PInvoke signature does not match the unmanaged target signature. Check that the calling convention and parameters of the PInvoke signature match the target unmanaged signature.

This is confusing to me, as it worked before; why doesn’t it work now? What changed? It took me more than a few moments to realize that I had upgraded to Visual Basic 2010 Express about a year or so ago. The error occurs when running the VS2010 (Visual Basic 2010 Express) debugger. If I run the application without using the debugger (Ctrl+F5), the application and DLL seem to run fine. (Obviously, I can’t see VB problems that may exist when running in this mode.)

Before identifying that the newer IDE seemed to be causing my problem, I also tried compiling the Fortran code as a .NET DLL and calling the subroutine that way. In this case I get the following error:

CallbackOnCollectedDelegate was detected Message: A callback was made on a garbage collected delegate of type 'ftn95lib.mdl!Salford.Fortran.RTLibrary+clearwin_callback::Invoke'. This may cause application crashes, corruption and data loss. When passing delegates to unmanaged code, they must be kept alive by the managed application until it is guaranteed that they will never be called.

As in the case of the Win32 DLL, I can get the .NET DLL to run properly if I turn off the VS2010 debugger.

These errors occur even when the simplest of Fortran code is called, such as the below, where I test passing Integer, Floating Point, String and Array values:

      SUBROUTINE fsub (x, z)
        INTEGER*2, Intent(inout):: x
        INTEGER*4, Intent(inout):: z
        x = x + x
        z = z * z
      END
      
      REAL FUNCTION ffunc (y)
        REAL, Intent(inout):: y
        ffunc = y + 1.23
      END

      SUBROUTINE fstring (fstr)
        CHARACTER*20, Intent(inout):: fstr
        fstr = 'Jack Be Nimble'
      END
      
      SUBROUTINE farray(farr)
        REAL, Intent(inout):: farr(:)
        DO 10 n= 1,10
          farr(n)=n+.123
   10   CONTINUE
      END

(Note: The above code is the Win32 version; the .NET version has the Assembly_Interface(name= ) declaration lines added.)

A call to just the FSTRING subroutine (Win32), which simply receives and passes back a string, results in the following error:

System.StackOverflowException was unhandled Message: An unhandled exception of type 'System.StackOverflowException' occurred in mscorlib.dll

In Visual Basic 2010 Express, the declarations and calls seem to be correct:

   Declare Sub FSUB Lib 'C:\\… \\Release\\Win32\\TestFunctions.dll' (ByRef x As Short, ByRef z As Integer)
   Declare Function FFUNC Lib 'C:\\… \\Release\\Win32\\TestFunctions.dll' (ByRef y As Single) As Single
   Declare Sub FSTRING Lib 'C:\\…\\Release\\Win32\\TestFunctions.dll' (ByVal fstr As String)
   Declare Sub FARRAY Lib 'C:\\… \\Release\\Win32\\TestFunctions.dll' (ByVal farr() As Single)

…

   Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
      Dim x As Short
      Dim z As Integer
      x = 10
      z = 12
      FSUB(x, z)
      MsgBox(x & '  ' & z)    ' Display the results.

      Dim y As Single
      y = 3
      MsgBox(FFUNC(y))    ' Display the results.

      Dim fstr As String
      fstr = Space(20)
      Call FSTRING(fstr)
      MsgBox(fstr)        ' Display the results.

      Dim farr(9) As Single
      Call FARRAY(farr)
      For n = 0 To 9
         MsgBox(farr(n))   ' Display the results.
      Next n
   End Sub

In looking around various web forums, the VS2010 debugger is evidently less forgiving or more strict/restrictive than VS2008 was when calling external DLLs. Is there some known incompatibility with FTN95 Express (v5.4) compiled code and VS2010? Am I doing something wrong in the way I am coding the above test subroutines and functions? Any help or advice to be able to make all of this work again (under VB 2010 Express) would be appreciated. This is driving me crazy!

Please login to reply.