C#中的Fortran dll导致无法找到入口点错误

时间:2019-03-10 17:17:39

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

在这里寻求帮助。我试图通过Windows 10上的Visual Studio 2017在C#应用程序中使用fortran dll。我从没见过fortran代码。因此,我用记事本++中的简单程序创建了一个示例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中编译了此代码(称为console.f90的1个文件):

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,这是问题吗?依赖项查询器还显示了3个dll依赖项“ lib ...”。我也将它们放置在项目文件夹中,并应用了相同的属性。仍然没有运气。感谢您的帮助。谢谢

1 个答案:

答案 0 :(得分:0)

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