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

Call Fortran Dll contains allocatable array in VB.NET

Arash_Z_
Beginner
512 Views

Hi,

I have a Fortran subroutine which allocates an array and assign values to components:

      subroutine ExampleSub (array)
 
!DEC$ ATTRIBUTES STDCAll, DLLEXPORT::ExampleSub
!DEC$ ATTRIBUTES  ALIAS:"ExampleSub" :: ExampleSub
!DEC$ ATTRIBUTES REFERENCE :: array 
      
      integer, allocatable, dimension(:), intent (out):: array

      allocate (array(3))
      
      array(1) = 111
      array(2) = 222
      array(3) = 333
      
      return
      end subroutine

I've created a DLL from this subroutine and try to call it in VB.NET. My VB code looks like this:

    Declare Sub ExampleSub Lib "C:\Users\Myaddress\Dll1.dll" Alias "ExampleSub" _
                (ByVal array() As Integer)

    Sub Main()

        Dim array1(2) As Integer
        Call ExampleSub(array1)

    End Sub

 

The VB scripts runs without any errors however it produces wrong values for the array. Each time I run, it gives different results for the array.
Is it even possible to make this work in VB.NET while the Fortran DLL contains an allocatable array?

Thanks,

Arash

 

0 Kudos
1 Solution
Steven_L_Intel1
Employee
512 Views

Can't be done. Sorry. Fortran deferred-shape arrays are not compatible with VB, and you certainly can't reallocate a VB array in Fortran.

View solution in original post

0 Kudos
4 Replies
Steven_L_Intel1
Employee
513 Views

Can't be done. Sorry. Fortran deferred-shape arrays are not compatible with VB, and you certainly can't reallocate a VB array in Fortran.

0 Kudos
Arash_Z_
Beginner
512 Views

Thanks for the clarification Steve.

0 Kudos
Greg_T_
Valued Contributor I
512 Views

Hi Arash,

I'm curious if your application can obtain the needed array size first, allocate it in VB, then call a Fortran routine?  Our approach has been to call a Fortran routine from VB or C# to get the data size, such as reading a file or counting data to return an array size.  We then allocate the array in the VB or C# program, and pass that array to the second Fortran routine to do the calculations and return the results.  Would this approach work for you?

Regards,
Greg

0 Kudos
Arash_Z_
Beginner
512 Views

Hi Greg,

Thanks for your comment. Yes the approach of allocating the array in VB and pass it to Fortran works. I was wondering if the size of array in the second Fortran subroutine can be set based the passed array size. I can define the size in the second Fortran subroutine to work for many possible sizes. I thought if I could use allocatable arrays in the second subroutine I would save some memory.

Thanks,

Arash

0 Kudos
Reply