我创建了一个由我的 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 将数组传递给 x64 中的 Fortran DLL
但我无法修复它。
这是我的代码:
福兰:
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