Pass Fortran array of unknown size to Julia

I need to read some binary data (a few arrays, stored in, say, WS2.HSX) that is handled by a Fortran module. I would like to call the Fortran module from Julia. However, the sizes of the arrays are not known in advance.

Right now, my solution is to read the data twice: the first time the sizes of the arrays are returned to julia and the second time to actually read the data. A simple example would be

dimensions = zeros(Int32, 6)
ccall(
    (:julia_interface_mp_get_dimensions_, "./julia_interface.so"),
    Cvoid,
    (Ref{Int32},),
    dimensions
)

nspin = dimensions[5]
nh = dimensions[6]

hamiltonian = zeros(Float64, nh, nspin)
ccall(
    (:julia_interface_mp_read_hsx_file_jl_, "./julia_interface.so"),
    Cvoid,
    (Ref{Float64}, Ref{Int32}, Ref{Int32}),
    hamiltonian, nh, nspin
)
# There are many other arrays

The Fortran subroutines are

subroutine get_dimensions(dimensions)
    integer, dimension(6), intent(out) :: dimensions
    type(hsx_t) :: hsx
    call read_hsx_file(hsx, "WS2.HSX")
    dimensions(1) = hsx%nspecies
    dimensions(2) = hsx%na_u
    dimensions(3) = hsx%no_u
    dimensions(4) = hsx%no_s
    dimensions(5) = hsx%nspin
    dimensions(6) = hsx%nh
end subroutine get_dimensions

subroutine read_hsx_file_jl(hamilt, nh, nspin)
    real(dp), dimension(nh, nspin), intent(out) :: hamilt
    type(hsx_t) :: hsx
    integer, intent(in) :: nh
    integer, intent(in) :: nspin
    
    call read_hsx_file(hsx, "WS2.HSX")
    hamilt = hsx%hamilt
    
end subroutine read_hsx_file_jl

This works, but I am wondering whether there is a better or more elegant solution.

Seems to be no better way at the time I was doing the test.
The Fortran subroutine

subroutine foo(w)
    real(kind=8), dimension(:), intent(in) :: w
end

has to be wrapped into

subroutine foo1(w, n)
    integer, intent(in) :: n
    real(kind=8), intent(in) :: w(n)
end

to be called from Julia.
Wondering if there is an alternative now :face_exhaling:

I am expecting something like Fortran passes the size of the array and the pointer to the array to Julia, and the array can be reconstructed using the above information.