【发布时间】:2015-04-10 12:09:03
【问题描述】:
我必须处理一些非常古老的 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 successful\r\n");
FARPROC result = GetProcAddress(hFortDll, "test_fort");
test_fort fortSubroutine = (test_fort)result;
if (fortSubroutine == NULL)
wprintf(L"Function not found\r\n");
else
wprintf(L"Function successfully loaded\r\n");
FreeLibrary(hFortDll);
}
getchar();
return 0;
}
如果我运行这段代码,我会得到以下输出:
Loading successful
Function not found
调试器显示 result 包含零地址 (0x00000000)。我不知道我做错了什么,像this 这样的线程没有提供答案。
提前致谢。
【问题讨论】:
-
使用 depends (dependencywalker.com) 找出实际调用的 fortran 例程。它可能有前导或尾随下划线,也可能完全大写。
-
还要小心你使用的是 Unicode - Fortran 通常是 ASCII。
-
我知道我使用的是 Unicode。但是据我所知,使用 Unicode 加载库是可以的,并且 GetProcAddress 具有 LPCSTR 类型的第二个参数,而不是 LPCWSTR。所以应该没问题,但我会检查