尝试从 C# 控制台应用程序调用存储在 Fortran DLL 中的子例程时,不断收到以下错误:“name.exe 中发生类型为‘System.EntryPointNotFoundException’的未处理异常附加信息:无法找到名为‘Fortran_Subroutine’的入口点' 在 DLL 'Fortran.dll' 中” 几乎所有与此错误消息相关的帖子都与 C#/C++ 相关。Fortran 项目具有将 DLL 复制到 C# 项目 (CSharp_proj\bin\debug) 的构建后事件。
Fortran 代码对 !DEC$ 使用了两次调用,它们看起来好吗?:
C
MODULE MF_DLL
C
CONTAINS
C
SUBROUTINE MFNWT_INIT()
C
!DEC$ ATTRIBUTES DLLEXPORT :: MFNWT_INIT
!DEC$ ATTRIBUTES DECORATE, ALIAS: "MFNWT_INIT"
C
USE GLOBAL
...(do stuff)
RETURN
END SUBROUTINE MFNWT_INIT
调用 fortran 的 C# 代码,DLLImport 调用看起来好吗?:
using System.Runtime.InteropServices;
public static class CustomMODSIM
{
public static Model myModel = new Model();
private static SortedList myStreamNodes;
public static void Main(string[] CmdArgs)
{
string FileName = CmdArgs[0];
myModel.Init += OnInitialize;
...(do more stuff)...
//Function call that will invoke "OnInitialize" below
myProgram.RunSolver(myModel);
}
private static void OnInitialize()
{
//call Fortran function
MFNWT_INIT();
//Initialize list of stream nodes
myStreamNodes = new SortedList();
Node m_Node = myModel.FindNode("NonStorage1");
...(do more stuff)
}
//Fortran DLL interface
[DllImport("MF_DLL.dll", CallingConvention=CallingConvention.Cdecl)]
public static extern void MFNWT_INIT();
}