Intel® Fortran Compiler
Build applications that can scale for the future with optimized code designed for Intel® Xeon® and compatible processors.

combined 2 strings in vb with fortran dll

Lu_Zhang
Beginner
831 Views

Hi, guys, I try to combine 2 strings using VB with fortran dll, but it does not work. How can I make it?  Thanks!

//vb code

Public Declare Sub DLL_ROUTSTR Lib "D:\Debug\Dll4.dll" _
    (ByVal A As String, ByVal B As String, ByRef C As String)
    Public Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        Dim A As String
        Dim B As String
        Dim C As String
        A = TextBox1.Text
        B = TextBox2.Text
        Call DLL_ROUTSTR(A, B, C)
        TextBox3.Text = C
    End Sub

 

//fortran code

SUBROUTINE DLL_ROUTSTR (A, B, C)
IMPLICIT NONE
! Specify that DLL_ROUTSTR is exported to a DLL
! and that the external name is 'DLL_ROUTSTR'
!DEC$ ATTRIBUTES DLLEXPORT, STDCALL :: DLL_ROUTSTR
!DEC$ ATTRIBUTES ALIAS:'DLL_ROUTSTR' :: DLL_ROUTSTR
!DEC$ ATTRIBUTES REFERENCE :: A,B,C

CHARACTER(10), INTENT(IN) :: A, B
CHARACTER(10), INTENT(OUT) :: C
C = A // B
RETURN
END 

0 Kudos
6 Replies
Steve_Lionel
Honored Contributor III
831 Views

You can't do it this simply - strings in VB are a strange beast. The provided sample VB.NET-SafeArrays demonstrates the use of the BSTR library routines to convert Fortran strings and do concatenation.

I will Also comment that your Fortran code, as is, will simply assign A to C, ignoring B, because all 10 characters of A are used, leaving none left over for B. C=TRIM(A)//TRIM(B) might be more what you had in mind, but I doubt this will translate back to VB as it will have no idea what the new length is to be.

0 Kudos
FortranFan
Honored Contributor III
831 Views

.. I try to combine 2 strings using VB with fortran dll, but it does not work. How can I make it?   ..

Same concept as your other thread: see Quote #7. https://software.intel.com/en-us/forums/intel-visual-fortran-compiler-for-windows/topic/807421

But this time, use StringBuilder class instead of String in Microsoft .NET and explicitly pass object lengths to Fortran to be "safe".  On the Fortran side, interoperate with .NET using the C interoperability feature in standard Fortran (which is also explained in books such as this: https://books.google.com/books?id=sB1rDwAAQBAJ&source=gbs_similarbooks)

Fdll.f90:

module m

   use, intrinsic :: iso_c_binding, only : c_char, c_int, c_f_pointer, c_loc

   implicit none

contains

   subroutine DLL_ROUT(A, lenA, B, lenB, C, lenC) bind(C, name="DLL_ROUT")
   !DEC$ ATTRIBUTES DLLEXPORT::DLL_ROUT

      !.. Argument list
      character(kind=c_char,len=1), intent(in), target    :: A(*)
      integer(c_int), intent(in), value                   :: lenA
      character(kind=c_char,len=1), intent(in), target    :: B(*)
      integer(c_int), intent(in), value                   :: lenB
      character(kind=c_char,len=1), intent(inout), target :: C(*)
      integer(c_int), intent(in), value                   :: lenC

      block
         character(kind=c_char,len=lenA), pointer :: pA => null()
         character(kind=c_char,len=lenB), pointer :: pB => null()
         character(kind=c_char,len=lenC), pointer :: pC => null()
         call c_f_pointer( cptr=c_loc(A), fptr=pA )
         call c_f_pointer( cptr=c_loc(B), fptr=pB )
         call c_f_pointer( cptr=c_loc(C), fptr=pC )
         pC = pA // pB
         pA => null()
         pB => null()
         pC => null()
      end block

      return

   end subroutine DLL_ROUT

end module m

VB file:

Imports System.IO
Imports System.Text
Imports System.Runtime.InteropServices

Namespace Fortran

   Public Class Fdll

      <DllImport("c:\Debug\Fdll.dll", CallingConvention:=CallingConvention.Cdecl)> _
      Public Shared Sub DLL_ROUT (A As StringBuilder, lenA As Integer, B As StringBuilder, lenB as Integer, _
                                  C As StringBuilder, lenC As Integer)
      end Sub

   End Class

   NotInheritable Class Test

      Private Sub New()
      End Sub

      Public Shared Sub Main()

         Dim a As StringBuilder
         Dim b As StringBuilder
         Dim c As StringBuilder

         ' Write header
         Console.WriteLine("*** Test Fortran Interop using VB ***" + vbLf)

         Try

            a = New StringBuilder("Hello ")
            b = New StringBuilder("World!")
            c = New StringBuilder
            c.Length = a.Length + b.Length
            Fdll.DLL_ROUT(a, a.Length, b, b.Length, c, c.Length)

            Console.WriteLine("c = " + c.ToString())

         Catch ex As Exception

            Console.WriteLine(ex.Message)

         Finally

            Console.WriteLine("Press any key to continue..")
            Console.ReadKey()

         End Try

      End Sub

   End Class

End Namespace
C:\Debug>ifort /dll /libs:static Fdll.f90
Intel(R) Visual Fortran Intel(R) 64 Compiler for applications running on Intel(R) 64, Version 19.0.3.203 Build 20190206
Copyright (C) 1985-2019 Intel Corporation.  All rights reserved.

Microsoft (R) Incremental Linker Version 14.16.27027.1
Copyright (C) Microsoft Corporation.  All rights reserved.

-out:Fdll.dll
-dll
-implib:Fdll.lib
Fdll.obj
   Creating library Fdll.lib and object Fdll.exp

C:\Debug>vbc -platform:x64 test.vb
Microsoft (R) Visual Basic Compiler version 2.10.0.0 (b9fb1610)
Copyright (C) Microsoft Corporation. All rights reserved.


C:\Debug>test.exe
*** Test Fortran Interop using VB ***

c = Hello World!
Press any key to continue..

C:\Debug>
C:\Debug>ifort /dll /libs:static Fdll.f90
Intel(R) Visual Fortran Intel(R) 64 Compiler for applications running on IA-32, Version 19.0.3.203 Build 20190206
Copyright (C) 1985-2019 Intel Corporation.  All rights reserved.

Microsoft (R) Incremental Linker Version 14.16.27027.1
Copyright (C) Microsoft Corporation.  All rights reserved.

-out:Fdll.dll
-dll
-implib:Fdll.lib
Fdll.obj
   Creating library Fdll.lib and object Fdll.exp

C:\Debug>vbc -platform:x86 test.vb
Microsoft (R) Visual Basic Compiler version 2.10.0.0 (b9fb1610)
Copyright (C) Microsoft Corporation. All rights reserved.


C:\Debug>test.exe
*** Test Fortran Interop using VB ***

c = Hello World!
Press any key to continue..

C:\Debug>
0 Kudos
Lu_Zhang
Beginner
831 Views

Thank you Steve.

0 Kudos
Lu_Zhang
Beginner
831 Views

Thank you FortranFan. Did you run it in Visual Studio? I build a VB console project in VS15, and run the code below, it gives me some errors, see attached picture. Looks like it doesnot recognize NAMESPACE.

Module Module1
    Imports System.IO
    Imports System.Text
    Imports System.Runtime.InteropServices
Namespace Fortran
    Public Class Fdll
        <DllImport("D:\Debug\Dll5.dll", CallingConvention:=CallingConvention.Cdecl)>

        Public Shared Sub DLL_ROUT(A As StringBuilder, lenA As Integer, B As StringBuilder, lenB As Integer, _

            C As StringBuilder, lenC As Integer)
      End Sub
    End Class

    NotInheritable Class Test
        Private Sub New()
        End Sub

        Public Shared Sub Main()
            Dim a As StringBuilder
            Dim b As StringBuilder
            Dim c As StringBuilder
            ' Write header
            Console.WriteLine("*** Test Fortran Interop using VB ***" + vbLf)
            Try
                a = New StringBuilder("Hello ")
                b = New StringBuilder("World!")
                c = New StringBuilder
                c.Length = a.Length + b.Length
                Fdll.DLL_ROUT(a, a.Length, b, b.Length, c, c.Length)
                Console.WriteLine("c = " + c.ToString())
            Catch ex As Exception
                Console.WriteLine(ex.Message)
            Finally
                Console.WriteLine("Press any key to continue..")
                Console.ReadKey()
            End Try
        End Sub
    End Class
End Namespace

End Module

0 Kudos
FortranFan
Honored Contributor III
831 Views

Lu Zhang wrote:

Thank you FortranFan. Did you run it in Visual Studio? ..

See Quote #3 again, there are 2 {..}code paste sections in it showing exactly the steps that were followed from an appropriate command line where all the required configurations and paths have been set up.  You can try to mimic the same.

Please note this is a Fortran forum.  What you are asking in Quote #5 is how to setup a Visual Basic .NET console application solution and a project in Visual Studio - there are near INFINITE resources available, particularly online, as to how to accomplish this task, please refer to one or more of them - you can start with a web search.

0 Kudos
Lu_Zhang
Beginner
831 Views

Got it. Thanks again for your big help.

0 Kudos
Reply