简体   繁体   中英

C++ calling FORTRAN subroutine from dll

I have to deal with some very old FORTRAN code, but I'd like to use some of functions from FORTRAN in C++. Right now I have a small project to practice with exporting FORTRAN dll and importing it in C. I do it on Windows with FTN95 compiler for FORTRAN and Visual C++. My fortran source contains this function:

F_STDCALL integer function test_fort(a)
            implicit none
            integer, intent(in) :: a
            test_fort = 2*a
    end function

I compile it into FORT.dll and then place it into the output folder of my C++ project. C++ source code:

#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;
}

If I run this code, I get the following output:

Loading successful
Function not found

Debugger shows that result contains a zero address (0x00000000). I cant figure out what I am doing wrong and threads like this do not provide the answer.

Thanks in advance.

So thanks to very fast response and a link to a very useful tool Dependency Walker I've found out that the problem was with function name. Though I spent some time changing the case of "test_fort" and adding symbols like "_" to it, I've missed the "TEST_FORT" variant - this is the alias name of "test_fort" FORTRAN function in .dll. So, to get it working, I had to change only one line of code:

FARPROC result = GetProcAddress(hFortDll, "TEST_FORT");

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM