<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: x64 string passing in Intel® Fortran Compiler</title>
    <link>https://community.intel.com/t5/Intel-Fortran-Compiler/x64-string-passing/m-p/1381191#M161156</link>
    <description>&lt;P&gt;Hi FortranFan,&lt;/P&gt;
&lt;P&gt;I need to continue with the .for files because there is minimal time to upgrade this; the objective of the current project is to migrate to the 64-bit platform only. Thanks again!&lt;/P&gt;</description>
    <pubDate>Mon, 02 May 2022 13:48:53 GMT</pubDate>
    <dc:creator>HarryWelten</dc:creator>
    <dc:date>2022-05-02T13:48:53Z</dc:date>
    <item>
      <title>x64 string passing</title>
      <link>https://community.intel.com/t5/Intel-Fortran-Compiler/x64-string-passing/m-p/1373313#M160844</link>
      <description>&lt;P&gt;Hi,&lt;/P&gt;
&lt;P&gt;I have a VB.Net application calling Fortran dll's and I needed to migrate to x64. I use VS2019 and IVF V19.&amp;nbsp;It crashes in the Fortran call where I need a returned string. I changed the string length parameter to a Long in following sample. Also tried ISO binding, but it crashes when entering the DLL.&amp;nbsp;Sample VB.Net source frmTis.vb:&lt;/P&gt;
&lt;P&gt;Declare Sub CMPNMS Lib "TISdlld_x64.dll" (ByVal IND As Integer, ByRef str As String, ByVal lang As Long)&lt;BR /&gt;&lt;BR /&gt;Dim i As Integer&lt;BR /&gt;Dim BigLen As Long&lt;BR /&gt;Dim cmpnam as String&lt;BR /&gt;i = 1&lt;BR /&gt;cmpnam = "------"&lt;BR /&gt;BigLen = Len(cmpnam)&lt;BR /&gt;Call CMPNMS(i, cmpnam, BigLen) ' Call Fortran procedure cmpnam to return string 1, but crashes.&lt;/P&gt;
&lt;P&gt;Sample Fortran file Tis.for:&lt;/P&gt;
&lt;P&gt;subroutine CMPNMS (COUNT, NAME, LNG) bind(C)&lt;BR /&gt;!DEC$ ATTRIBUTES DLLEXPORT::CMPNMS&lt;BR /&gt;use, intrinsic :: ISO_C_BINDING&lt;BR /&gt;character(1,C_CHAR),target ::NAME(*)&lt;BR /&gt;integer(C_INT32_T),value::COUNT ! 4-byte integer&lt;BR /&gt;integer(C_SIZE_T),value::LNG ! 8-byte Long&lt;BR /&gt;character(LNG,C_CHAR), pointer :: fptr &lt;BR /&gt;type(C_PTR) cptr &lt;BR /&gt;integer*8 LNG&lt;BR /&gt;&lt;BR /&gt;cptr = C_LOC(NAME(1)) &lt;BR /&gt;call C_F_POINTER(cptr,fptr) &lt;BR /&gt;print *, fptr ! content of string of caller&lt;BR /&gt;NAME = 'NewStr' ! return a new string; always 6 char&lt;BR /&gt;LNG = LEN(NAME)&lt;BR /&gt;return&lt;/P&gt;
&lt;P&gt;The Fortran calling convention was in x86 STDCALL, now default&lt;BR /&gt;String Length Argument passing is After Individual String Argument.&lt;BR /&gt;&lt;BR /&gt;It gives runtime AccessViolationException at CMPNMS(Int32 IND, String&amp;amp; str, Int64 lang)&lt;BR /&gt;Any help is appreciated. Kind regards, Harry.&lt;/P&gt;</description>
      <pubDate>Thu, 31 Mar 2022 14:45:59 GMT</pubDate>
      <guid>https://community.intel.com/t5/Intel-Fortran-Compiler/x64-string-passing/m-p/1373313#M160844</guid>
      <dc:creator>HarryWelten</dc:creator>
      <dc:date>2022-03-31T14:45:59Z</dc:date>
    </item>
    <item>
      <title>Re: x64 string passing</title>
      <link>https://community.intel.com/t5/Intel-Fortran-Compiler/x64-string-passing/m-p/1373606#M160855</link>
      <description>&lt;P&gt;&lt;a href="https://community.intel.com/t5/user/viewprofilepage/user-id/169749"&gt;@HarryWelten&lt;/a&gt;&amp;nbsp;,&lt;/P&gt;
&lt;P&gt;Immediately I am unable to spend the time to read through your code and point you to any changes.&amp;nbsp; But I had this simple example for a colleague who previously had a similar question.&amp;nbsp; Perhaps you can try out this simple case and see if you can reproduce what is shown below&amp;nbsp; If yes, that might point you as to how to proceed with your code?&lt;/P&gt;
&lt;P&gt;Fortran "library" code:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;LI-CODE lang="fortran"&gt;module Fdll_m

   use, intrinsic :: iso_c_binding, only : c_char, c_size_t, c_loc, c_f_pointer

contains

   subroutine Fstr(str, lenstr) bind(C, name="Fstr")
   !DEC$ ATTRIBUTES DLLEXPORT::Fstr

      ! Argument list
      character(kind=c_char,len=1), intent(in), target :: str(*)
      integer(c_size_t), intent(in), value             :: lenstr

      ! Elided are any checks for the lenstr value

      block

         character(kind=c_char,len=lenstr), pointer :: pstr

         call c_f_pointer( cptr=c_loc(str), fptr=pstr )
         
         pstr = c_char_"Hello World!"
         pstr =&amp;gt; null()
         
      end block

      return

   end subroutine

end module
&lt;/LI-CODE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Visual Basic calling program in .NET:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;LI-CODE lang="fortran"&gt;Imports System.IO
Imports System.Text
Imports System.Runtime.InteropServices

Namespace Fortran

   Public Class Fdll

      &amp;lt;DllImport("Fdll.dll", CallingConvention:=CallingConvention.Cdecl)&amp;gt; _
      Public Shared Sub Fstr (A As StringBuilder, lenA As UIntPtr)
      end Sub

   End Class

   NotInheritable Class Test

      Private Sub New()
      End Sub

      Public Shared Sub Main()

         Dim s As StringBuilder

         ' Write header
         Console.WriteLine("*** Test Fortran Interop using VB ***" + vbLf)

         Try

            s = New StringBuilder(12)
            Fdll.Fstr(s, s.Capacity)

            Console.WriteLine("s = " + s.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
&lt;/LI-CODE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Program execution using Intel Fortran:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;LI-CODE lang="none"&gt;C:\Temp&amp;gt;ifort /dll /libs:static Fdll.f90
Intel(R) Fortran Intel(R) 64 Compiler Classic for applications running on Intel(R) 64, Version 2021.3.0 Build 20210609_000000
Copyright (C) 1985-2021 Intel Corporation.  All rights reserved.

Microsoft (R) Incremental Linker Version 14.29.30038.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:\Temp&amp;gt;vbc -platform:x64 test.vb
Microsoft (R) Visual Basic Compiler version 3.10.0-4.21318.11 (7ceb6331)
Copyright (C) Microsoft Corporation. All rights reserved.


C:\Temp&amp;gt;test.exe
*** Test Fortran Interop using VB ***

s = Hello World!
Press any key to continue..
&lt;/LI-CODE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 01 Apr 2022 13:35:17 GMT</pubDate>
      <guid>https://community.intel.com/t5/Intel-Fortran-Compiler/x64-string-passing/m-p/1373606#M160855</guid>
      <dc:creator>FortranFan</dc:creator>
      <dc:date>2022-04-01T13:35:17Z</dc:date>
    </item>
    <item>
      <title>Re: x64 string passing</title>
      <link>https://community.intel.com/t5/Intel-Fortran-Compiler/x64-string-passing/m-p/1374037#M160875</link>
      <description>&lt;P&gt;Thanks so much for your reply FortranFan. I'm in the process of converting the include files (14) and sources to Fortan-90. The compiler does not seem to like the line:&amp;nbsp;&amp;nbsp; integer(C_INT32_T),value:: COUNT ! 4-byte integer.&amp;nbsp; It complains that the parameter must be a compile-time constant. I will keep you up-to-date with the result.&lt;/P&gt;</description>
      <pubDate>Mon, 04 Apr 2022 08:54:10 GMT</pubDate>
      <guid>https://community.intel.com/t5/Intel-Fortran-Compiler/x64-string-passing/m-p/1374037#M160875</guid>
      <dc:creator>HarryWelten</dc:creator>
      <dc:date>2022-04-04T08:54:10Z</dc:date>
    </item>
    <item>
      <title>Re: x64 string passing</title>
      <link>https://community.intel.com/t5/Intel-Fortran-Compiler/x64-string-passing/m-p/1374038#M160876</link>
      <description>&lt;P&gt;It appears that the constant is satisfied by extending the USE clause.&lt;/P&gt;</description>
      <pubDate>Mon, 04 Apr 2022 09:00:59 GMT</pubDate>
      <guid>https://community.intel.com/t5/Intel-Fortran-Compiler/x64-string-passing/m-p/1374038#M160876</guid>
      <dc:creator>HarryWelten</dc:creator>
      <dc:date>2022-04-04T09:00:59Z</dc:date>
    </item>
    <item>
      <title>Re: x64 string passing</title>
      <link>https://community.intel.com/t5/Intel-Fortran-Compiler/x64-string-passing/m-p/1376149#M160947</link>
      <description>&lt;P&gt;Hi,&lt;/P&gt;
&lt;P&gt;I built this example that you gave, modified it slightly, run the x64 debug mode and that also crashes entering into the Fortran routine.&lt;/P&gt;
&lt;P&gt;The VB part:&lt;/P&gt;
&lt;P&gt;&amp;lt;DllImport("TISdlld_x64.dll", CallingConvention:=CallingConvention.Cdecl)&amp;gt;&lt;BR /&gt;Public Sub CMPNMS(ByVal IND As Integer, ByRef str As String, ByVal lang As Long)&lt;BR /&gt;End Sub&lt;/P&gt;
&lt;P class="sub_section_element_selectors"&gt;&lt;SPAN class="sub_section_element_selectors"&gt;Dim i As Integer&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN class="sub_section_element_selectors"&gt;Dim BigLen As Long&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN class="sub_section_element_selectors"&gt;Dim cmpnam as String&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN class="sub_section_element_selectors"&gt;i = 1&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN class="sub_section_element_selectors"&gt;cmpnam = "------"&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN class="sub_section_element_selectors"&gt;BigLen = Len(cmpnam)&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN class="sub_section_element_selectors"&gt;Call CMPNMS(i, cmpnam, BigLen) ' Call Fortran procedure cmpnam to return string 1, but crashes.&lt;/SPAN&gt;&lt;/P&gt;
&lt;P class="sub_section_element_selectors"&gt;&lt;SPAN class="sub_section_element_selectors"&gt;Sample Fortran file Tis.for:&lt;/SPAN&gt;&lt;/P&gt;
&lt;P class="sub_section_element_selectors"&gt;&lt;SPAN class="sub_section_element_selectors"&gt;subroutine CMPNMS (COUNT, NAME, LNG) bind(C)&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN class="sub_section_element_selectors"&gt;!DEC$ ATTRIBUTES DLLEXPORT::CMPNMS&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN class="sub_section_element_selectors"&gt;use, intrinsic :: ISO_C_BINDING&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN class="sub_section_element_selectors"&gt;character(1,C_CHAR),target ::NAME(*)&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN class="sub_section_element_selectors"&gt;integer(C_INT32_T),value::COUNT ! 4-byte integer&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN class="sub_section_element_selectors"&gt;integer(C_SIZE_T),value::LNG ! 8-byte Long&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN class="sub_section_element_selectors"&gt;character(LNG,C_CHAR), pointer :: fptr&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN class="sub_section_element_selectors"&gt;type(C_PTR) cptr&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN class="sub_section_element_selectors"&gt;integer*8 LNG&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN class="sub_section_element_selectors"&gt;cptr = C_LOC(NAME(1))&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN class="sub_section_element_selectors"&gt;call C_F_POINTER(cptr,fptr)&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN class="sub_section_element_selectors"&gt;print *, fptr ! content of string of caller&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN class="sub_section_element_selectors"&gt;NAME = 'NewStr' ! return a new string; always 6 char&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN class="sub_section_element_selectors"&gt;LNG = LEN(NAME)&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN class="sub_section_element_selectors"&gt;return&lt;/SPAN&gt;&lt;/P&gt;</description>
      <pubDate>Mon, 11 Apr 2022 14:17:41 GMT</pubDate>
      <guid>https://community.intel.com/t5/Intel-Fortran-Compiler/x64-string-passing/m-p/1376149#M160947</guid>
      <dc:creator>HarryWelten</dc:creator>
      <dc:date>2022-04-11T14:17:41Z</dc:date>
    </item>
    <item>
      <title>Re: x64 string passing</title>
      <link>https://community.intel.com/t5/Intel-Fortran-Compiler/x64-string-passing/m-p/1376163#M160948</link>
      <description>&lt;P&gt;Look at FortranFan's post on 4/1. His VB argument specification for the string was to pass&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp;A As StringBuilder&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Yours is passing&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp; &amp;nbsp; ByRef str As String&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;"String" is a container (containing several member variables one of which is the base address of the buffer for the string) whereas StringBuilder returns what amounts to the address of a null terminated string (IOW the string itself as opposed to the address of container).&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Jim Dempsey&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 11 Apr 2022 14:41:24 GMT</pubDate>
      <guid>https://community.intel.com/t5/Intel-Fortran-Compiler/x64-string-passing/m-p/1376163#M160948</guid>
      <dc:creator>jimdempseyatthecove</dc:creator>
      <dc:date>2022-04-11T14:41:24Z</dc:date>
    </item>
    <item>
      <title>Re: x64 string passing</title>
      <link>https://community.intel.com/t5/Intel-Fortran-Compiler/x64-string-passing/m-p/1376486#M160955</link>
      <description>&lt;P&gt;Jim, thanks for your remark. I assumed it was a detail, but assumption is the mother of all f*. I will try it soon. Regards, Harry.&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 12 Apr 2022 09:12:33 GMT</pubDate>
      <guid>https://community.intel.com/t5/Intel-Fortran-Compiler/x64-string-passing/m-p/1376486#M160955</guid>
      <dc:creator>HarryWelten</dc:creator>
      <dc:date>2022-04-12T09:12:33Z</dc:date>
    </item>
    <item>
      <title>Re: x64 string passing</title>
      <link>https://community.intel.com/t5/Intel-Fortran-Compiler/x64-string-passing/m-p/1377370#M160975</link>
      <description>&lt;P&gt;Thanks a lot FortranFan and JimDempseyAtTheCove for the solution to the problem. I cannot emphasize enough how valuable your replies are.&lt;/P&gt;</description>
      <pubDate>Fri, 15 Apr 2022 07:54:51 GMT</pubDate>
      <guid>https://community.intel.com/t5/Intel-Fortran-Compiler/x64-string-passing/m-p/1377370#M160975</guid>
      <dc:creator>HarryWelten</dc:creator>
      <dc:date>2022-04-15T07:54:51Z</dc:date>
    </item>
    <item>
      <title>Re: x64 string passing</title>
      <link>https://community.intel.com/t5/Intel-Fortran-Compiler/x64-string-passing/m-p/1378218#M161024</link>
      <description>&lt;P&gt;&lt;a href="https://community.intel.com/t5/user/viewprofilepage/user-id/169749"&gt;@HarryWelten&lt;/a&gt;&amp;nbsp;, glad you were able to solve your problem.&lt;/P&gt;
&lt;P&gt;A question if you are willing to share your feedback: do you plan to do further work on the Fortran side and if so, will you be considering modern Fortran (starting with free-form source and so much more)?&lt;/P&gt;
&lt;P&gt;Or is it the case for you of needing to do continue with .for files as in "&lt;SPAN&gt;Tis.for&lt;/SPAN&gt;" (that are usually FORTRAN 77 style fixed form source) and doing minimal to no changes in some existing codebase?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;By the way, I had reused the Fortran code I had posted in this thread toward another recent thread that involved part of a namesake i.e., Visual Basic for Applications as opposed to Visual Basic .NET though there are major differences between the two platforms.&amp;nbsp; It appears that user too was able to solve the problem, so for me it is good to see Fortran&amp;nbsp;"libraries" remaining in use with different solutions, especially on Windows:&lt;/P&gt;
&lt;P&gt;&lt;A href="https://community.intel.com/t5/Intel-Fortran-Compiler/Help-with-passing-VBA-string-to-Fortran/m-p/1375029/highlight/true#M160909" target="_blank"&gt;https://community.intel.com/t5/Intel-Fortran-Compiler/Help-with-passing-VBA-string-to-Fortran/m-p/1375029/highlight/true#M160909&lt;/A&gt;&lt;/P&gt;</description>
      <pubDate>Tue, 19 Apr 2022 18:32:15 GMT</pubDate>
      <guid>https://community.intel.com/t5/Intel-Fortran-Compiler/x64-string-passing/m-p/1378218#M161024</guid>
      <dc:creator>FortranFan</dc:creator>
      <dc:date>2022-04-19T18:32:15Z</dc:date>
    </item>
    <item>
      <title>Re: x64 string passing</title>
      <link>https://community.intel.com/t5/Intel-Fortran-Compiler/x64-string-passing/m-p/1381191#M161156</link>
      <description>&lt;P&gt;Hi FortranFan,&lt;/P&gt;
&lt;P&gt;I need to continue with the .for files because there is minimal time to upgrade this; the objective of the current project is to migrate to the 64-bit platform only. Thanks again!&lt;/P&gt;</description>
      <pubDate>Mon, 02 May 2022 13:48:53 GMT</pubDate>
      <guid>https://community.intel.com/t5/Intel-Fortran-Compiler/x64-string-passing/m-p/1381191#M161156</guid>
      <dc:creator>HarryWelten</dc:creator>
      <dc:date>2022-05-02T13:48:53Z</dc:date>
    </item>
  </channel>
</rss>

