locked
migrate from VB.Net to C# - calling fortran codes dlls via C#- visual fortran compiler RRS feed

  • Question

  • I want to migrate from VB.Net to C#.I want to use asp.net C# web application instead of vb.net as above for calling dlls via fortran codes.These are my codes:

    Module1.vb file:

    Module Module1
        REM Use ByVal to pass strings unless the called routine expects BSTR structures
    
        Public Declare Auto Sub electrostatica3D Lib "FCALL.DLL" _
      (ByVal DBL_IN() As Double, ByVal STR_IN As String, ByVal DBL_OUT() As Double)
    
    End Module

    fcall.f90 fortran file:

    subroutine electrostatica3D(DBL_IN, STRING_IN, DBL_OUT)
    
      use malla_3DP1
      use electros3D
      use external_electros3D
      use derivados3D
      use resolucion_sistema_lineal
    
      implicit none
    
      ! Specify that DLL_ROUT is exported to a DLL
    ! and that the external name is 'DLL_ROUT'
    !DEC$ ATTRIBUTES DLLEXPORT, STDCALL :: electrostatica3D
    !DEC$ ATTRIBUTES ALIAS:'electrostatica3D' :: electrostatica3D
    
    REAL(8), INTENT(IN) :: DBL_IN(0:3)
    CHARACTER(10), INTENT(IN) :: STRING_IN
    !DEC$ ATTRIBUTES REFERENCE :: STRING_IN
    ! When VB passes in a "ByVal String", it passes the address
    ! of a NUL-terminated string, similar to what C would do,
    ! and no separate length.  The REFERENCE attribute tells
    ! Fortran not to expect a length.  In order to use a function
    ! such as INDEX, we need to supply some maximum length to Fortran
    ! which should be at least as long as the longest expected string.
    REAL(8), INTENT(OUT) :: DBL_OUT(4)
    
    REAL(8) STRVAL
    INTEGER IOS, STRLEN
    
      integer          :: info
      double precision :: suma
      integer          :: i
    
    
      STRLEN = INDEX(STRING_IN, CHAR(0)) - 1
    
    ! Convert STRING_IN to a double.  If we get an error, we'll
    ! supply 1.0 as a default
    
    READ (STRING_IN(1:STRLEN), *, IOSTAT=IOS) STRVAL
    IF (IOS /= 0) STRVAL = 1.0
    DBL_OUT = STRVAL * DBL_IN
    
    call arint3D ()
    
     return
    end

    Form1.vb file:

    Public Class Form1
        Inherits System.Windows.Forms.Form
    
        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            Dim DBL_IN(4) As Double
            Dim DBL_OUT(4) As Double
            Dim MULTIPLIER As String
    
            DBL_IN(0) = 1.0
            DBL_IN(1) = 2.0
            DBL_IN(2) = 3.0
            DBL_IN(3) = 4.0
            MULTIPLIER = "2.0"
    
            Call electrostatica3D(DBL_IN, MULTIPLIER, DBL_OUT)
    
            TextBox1.Text = DBL_OUT(0)
            TextBox2.Text = DBL_OUT(1)
            TextBox3.Text = DBL_OUT(2)
            TextBox4.Text = DBL_OUT(3)
        End Sub
    End Class

    use C# code as :

    using System;
    
    using System.Runtime.InteropServices;
    namespace Console_use_integrate{
        class Program
        {
    
    
            [UnmanagedFunctionPointer(CallingConvention.Cdecl)]
            public delegate float ActionRefInt(ref float n);
    
            public ActionRefInt callbackHandler;
    
            public Program()
            {
                callbackHandler = new ActionRefInt(square);
            }
    
            public float square(ref float n)
            {
                return n*n;
            }
    
    
            static void Main(string[] args)
            {
                Program myProg2 = new Program();
    
                float a = 1f;
                float b = 3f;
                int step = null;
                float result;
                Console.WriteLine("Interval  from {0} to {1} ", a, b);
    
    result = integrate_Dll(myProg2.callbackHandler, a,  b,  System.IntPtr.Zero)
                Console.WriteLine("integration result is {0}",result);
                Console.ReadKey();
            }
    
    
           [DllImport(@"FCALL.DLL", CallingConvention = CallingConvention.Cdecl, 
    EntryPoint = "electrostatica3D")]
    public static extern float electrostatica3D([MarshalAs(UnmanagedType.FunctionPtr)] ActionRefInt callBack,
    float a, float b, ref int c);
    [DllImport(@"FCALL.DLL", CallingConvention = CallingConvention.Cdecl, 
    EntryPoint = "electrostatica3D")]
    public static extern float electrostatica3D([MarshalAs(UnmanagedType.FunctionPtr)] ActionRefInt callBack,
    float a, float b, System.IntPtr c);
    
        }
    
        }

    and changing 

    float a, float b, System.IntPtr c


    to something like below but in C# format

    ByVal DBL_IN() As Double, ByVal STR_IN As String, ByVal DBL_OUT() As Double

    and a little modification maybe needed.

    so what should I do?

    Regards

    Thursday, August 17, 2017 11:12 PM

All replies

  • float is single precision floating point type and double is double precision type so they won't match. Just use double instead for a and b.
    Friday, August 18, 2017 1:47 AM
  • Hello John Crown, 

    >>ByVal DBL_IN() As Double, ByVal STR_IN As String, ByVal DBL_OUT() As Double

    For the 'ByVal' keyword , you could achive it in c# by the following link,

    https://stackoverflow.com/questions/3040488/how-to-do-byval-in-c-sharp

    And for the vb statement as below.

     DBL_IN() As Double, STR_IN As String, DBL_OUT() As Double 

    It could convert to c# statement .

    Double[] DBL-IN; 
    String STR_IN;
    Double[] DBL_OUT;

    And there is useful code convert . It could convert code to each other between C# and VB . Note that it just provides a reference or suggestion for you.

    http://converter.telerik.com/

    Best regards,
    feih_7


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Edited by Fei Hu Friday, August 18, 2017 7:50 AM
    Friday, August 18, 2017 7:50 AM
  • Hi John Crown,

    According to your question is more related to ASP.NET , you could post a new thread to ASP.NET forum  for suitable support.

    The Visual C# discuss and ask the C# programming language, IDE, libraries, samples and tools . If you have some grammar or code errors, please feel free to contact us. We will try our best to give you a solution .

    Best Regards,
    Feih-7


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Edited by Fei Hu Friday, September 1, 2017 2:26 AM
    Friday, September 1, 2017 2:19 AM