Community
cancel
Showing results for 
Search instead for 
Did you mean: 
Highlighted
Beginner
2 Views

Call Fortran Dll contains allocatable array in VB.NET

Jump to solution

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

Accepted Solutions
Highlighted
2 Views

Can't be done. Sorry. Fortran

Jump to solution

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.

Retired 12/31/2016

View solution in original post

0 Kudos
4 Replies
Highlighted
3 Views

Can't be done. Sorry. Fortran

Jump to solution

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.

Retired 12/31/2016

View solution in original post

0 Kudos
Highlighted
Beginner
2 Views

Thanks for the clarification

Jump to solution

Thanks for the clarification Steve.

0 Kudos
Highlighted
New Contributor III
2 Views

Hi Arash,

Jump to solution

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
Highlighted
Beginner
2 Views

Hi Greg,

Jump to solution

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