如何从vb代码调用fortran函数?

时间:2013-05-28 15:34:23

标签: vb.net dll fortran

我创建了一个由我的fortran函数组成的dll文件,我想直接从visual basic中使用它们。但无论我做什么,我总会得到错误:

A call to PInvoke function 'Interface!Interface.Form1::Area2D' has unbalanced the stack. This is likely because the managed PInvoke signature does not match the unmanaged target signature. Check that the calling convention and parameters of the PInvoke signature match the target unmanaged signature.

我按照此处给出的示例代码:Visual Basic passing array to Fortran DLL in x64

但我无法修复它。

这是我的代码:

FORTRAN:

subroutine Area2D(polygon,A,s)


  !DEC$ ATTRIBUTES DLLEXPORT, ALIAS: "AREA2D" ::Area2D
  !DEC$ ATTRIBUTES REFERENCE::polygon, A,s
    integer(4)::s
    real::polygon(s,2)

    real:: A



    A=0
    do i=1,s-1
        A = A + ( polygon(i,1) - polygon(i+1,1) )*( polygon(i,2) + polygon(i+1,2) )*0.5
    end do
    A = A + ( polygon(s,1) - polygon(1,1) )*( polygon(s,2) + polygon(1,2) )*0.5
end subroutine Area2D



subroutine Centroid2D(polygon,C,A,s)
!DEC$ ATTRIBUTES DLLEXPORT, ALIAS: "CENTROID2D" ::Centroid2D
!DEC$ ATTRIBUTES REFERENCE::polygon, C,A,s
    integer(4)::s
    real::polygon(s,2)
    real::C(:)
    real:: A


    do i=1,s-1
       C(1)=C(1) + 1./6./A*(polygon(i+1,2)-polygon(i,2))*(polygon(i,1)*polygon(i,1)+polygon(i,1)*polygon(i+1,1)+polygon(i+1,1)*polygon(i+1,1))
       C(2)=C(2) + 1./6./A*(polygon(i,1) - polygon(i+1,1)  )* ( polygon(i,2)*polygon(i,2) + polygon(i,2)*polygon(i+1,2) + polygon(i+1,2)*polygon(i+1,2) )

    end do

    C(1)=C(1) + 1./6./A*(polygon(1,2)-polygon(s,2))*(polygon(s,1)*polygon(s,1)+polygon(s,1)*polygon(1,1)+polygon(1,1)*polygon(1,1))
    C(2)=C(2) + 1./6./A*(polygon(s,1) - polygon(1,1)  )* ( polygon(s,2)*polygon(s,2) + polygon(s,2)*polygon(1,2) + polygon(1,2)*polygon(1,2) )
end subroutine Centroid2D

VB:

    Imports System.Runtime.InteropServices
Public Class Form1
    Private Declare Sub Area2D Lib "EngineX.dll" Alias "AREA2D" (<[In](), Out()> ByRef Polygon(,) As Single, <[In](), Out()> ByRef A As Single, ByRef Count As Integer)
    Dim nodes(,) As Single
    Dim nodeCount As Integer
    Dim A As Single = 0




    Private Sub DataGridView1_Leave(sender As Object, e As EventArgs) Handles DataGridView1.Leave
        If DataGridView1.RowCount >= 4 Then

            nodeCount = DataGridView1.RowCount - 1

            ReDim nodes(nodeCount, 2)


            For i As Integer = 0 To nodeCount - 1 Step 1

                nodes(i, 1) = CSng(DataGridView1.Rows(i).Cells(1).Value)
                nodes(i, 2) = CSng(DataGridView1.Rows(i).Cells(2).Value)

                ...

            Next

            Area2D(nodes, A, nodeCount)
            TextBox1.Text = A.ToString
        End If


    End Sub


End Class

0 个答案:

没有答案