【问题标题】:Fortran dll in C# gives Unable to find an entry point errorC# 中的 Fortran dll 给出无法找到入口点错误
【发布时间】:2019-03-10 17:17:38
【问题描述】:

在这里请求一些帮助。我正在尝试通过 Windows 10 上的 Visual Studio 2017 在我的 C# 应用程序中使用 fortran dll。我一生中从未见过 fortran 代码。所以我在notepad++中用这样一个简单的程序创建了一个示例dll。

subroutine Add(x, y, total)BIND(C,NAME="Add")

!DEC$ ATTRIBUTES DLLEXPORT, ALIAS: 'Add'::Add

integer, intent (in) :: x
integer, intent (in) :: y
integer, intent (inout) :: total   

total = x + y
end subroutine 

我使用 MingW fortran 编译器和以下命令在 Windows 10 中编译了这段代码(1 个名为 console.f90 的文件):

gfortran -c -o C:\Users\Desktop\Temp\console.o C:\Users\Desktop\Temp\console.f90
gfortran -shared -mrtd -o C:\Users\Desktop\Temp\console.dll C:\Users\Desktop\Temp\console.o

这给了我 console.dll。

现在我在 VS 2017 中打开了一个新的 C# 控制台项目。将架构更改为 x86(x64 给我错误的格式错误)。将 fortran dll (console.dll) 放在与我的项目相同的文件夹中并包含在项目中,并将属性设置为“如果较新则复制”,以便将其复制到 bin\debug。

为 P/Invoke 写了这一行

[DllImport("console.dll", EntryPoint = "Add", CallingConvention = CallingConvention.StdCall)]
public static extern void Add([In] int x, [In] int y, [In, Out] int t);

然后在我的 Main 函数中使用了这个

int a = 12;
int b = 30;
int t = 0;
Add(a, b, t);

虽然当我运行应用程序时,执行 Add(a,b,t) 的那一刻我得到一个“EntryPointNotFound”异常。于是我用了DependencyWalker和Dumpbin,看看函数是否导出,名字是否正确。结果如下:

Dumpbin screen

Dependency Walker

我看到我的 dll 是 x86 而其他的是 x64 是问题吗?依赖walker还显示了3个dll依赖'lib ...'。我也将它们放在项目文件夹中并应用了相同的属性。仍然没有运气。任何帮助在这里表示赞赏。谢谢

【问题讨论】:

  • Dependency Walker 输出具有误导性。使用 dumpbin 找出导出的名称。你做了什么。然后名字就好了。所以 C# 程序显然在加载不同的 DLL。

标签: c# visual-studio-2017 fortran dllimport


【解决方案1】:

我已将平台选择设置为导致问题的调试。通过配置管理器添加 x86 选项并选择相同的选项解决了我的问题。

【讨论】:

    猜你喜欢
    • 2011-06-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-04-14
    • 1970-01-01
    • 2017-10-22
    • 1970-01-01
    • 2015-07-19
    相关资源
    最近更新 更多