我必须处理一些非常旧的FORTRAN代码,但我想在C++中使用FORTRAN中的一些函数。现在我有一个小项目要练习导出FORTRAN dll并在C中导入它。我在Windows上用FORTRAN和Visual C++的FTN95编译器来完成这项工作。我的fortran源包含以下函数:
F_STDCALL integer function test_fort(a)
implicit none
integer, intent(in) :: a
test_fort = 2*a
end function
我把它编译成FORT.dll,然后把它放在C++项目的输出文件夹中。C++源代码:
#include<stdlib.h>
#include<Windows.h>
#include<stdio.h>
typedef int(__stdcall *test_fort)(int* a);
int main()
{
HMODULE hFortDll = LoadLibraryW(L"FORT.dll");
if (hFortDll == NULL)
wprintf(L"Error loading FORT.dll");
else
{
wprintf(L"Loading successfulrn");
FARPROC result = GetProcAddress(hFortDll, "test_fort");
test_fort fortSubroutine = (test_fort)result;
if (fortSubroutine == NULL)
wprintf(L"Function not foundrn");
else
wprintf(L"Function successfully loadedrn");
FreeLibrary(hFortDll);
}
getchar();
return 0;
}
如果我运行这个代码,我会得到以下输出:
Loading successful
Function not found
调试器显示结果包含一个零地址(0x00000000)。我不知道我做错了什么,像这样的线程也不能提供答案。
提前谢谢。
因此,由于响应速度非常快,并链接到一个非常有用的工具Dependency Walker,我发现问题出在函数名上。尽管我花了一些时间更改"test_fort"的大小写并添加了"_"之类的符号,但我还是错过了"test_fort"变体——这是.dll中"test_foort"FORTRAN函数的别名。所以,为了让它发挥作用,我只需要更改一行代码:
FARPROC result = GetProcAddress(hFortDll, "TEST_FORT");