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.
ここに示されているサンプルコードに従いました: x64 で配列を Fortran DLL に渡す Visual Basic
しかし、私はそれを修正できませんでした。
ここに私のコードがあります:
フォートラン:
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