3

I am having problems importing a DLL into my current Fortran project. The DLL file I am trying to import, fdlltest.dll, has the following functions defined when I do dumpbin /exports:

C:\temp>dumpbin /exports fdlltest.dll
Microsoft (R) COFF/PE Dumper Version 12.00.40629.0
Copyright (C) Microsoft Corporation.  All rights reserved.

Dump of file fdlltest.dll

File Type: DLL

  Section contains the following exports for fdlltest.dll

    00000000 characteristics
    560ED478 time date stamp Fri Oct 02 14:01:12 2015
        0.00 version
           1 ordinal base
           3 number of functions
           3 number of names

    ordinal hint RVA      name

          1    0 00001000 add2i
          2    1 00001010 add2r
          3    2 00001040 simpson

  Summary

        1000 .data
        1000 .rdata
        1000 .reloc
        1000 .rsrc
        1000 .text

Next, I need to know step-by-step how to import the add2i, add2r, and simpson functions from this DLL located in C:\temp into my current Fortran project. I do not want examples with *.lib, since the actual DLL I want to use once I get past this example does not have a companion *.lib. This is my Fortran code which is supposed to generate an EXE that is linked to the DLL file:

program fdllrun
 implicit none
INTERFACE
 INTEGER FUNCTION add2i(a,b) 
!DEC$ ATTRIBUTES DLLIMPORT, ALIAS:"add2i" :: add2i
!DEC$ ATTRIBUTES REFERENCE::a, b
  INTEGER, intent(in), value:: a, b
 END FUNCTION add2i

 REAL FUNCTION add2r(a,b)
!DEC$ ATTRIBUTES DLLIMPORT, ALIAS:"add2r" :: add2r
!DEC$ ATTRIBUTES REFERENCE::a, b
  REAL, intent(in), value:: a, b
 END FUNCTION add2r

 REAL FUNCTION simpson(f, a, b, n) 
!DEC$ ATTRIBUTES DLLIMPORT, ALIAS:"simpson" :: simpson
!DEC$ ATTRIBUTES REFERENCE::f, a, b, n
  EXTERNAL f
  real, intent(in), value ::  a, b
  integer, intent(in), value :: n
 END FUNCTION simpson
END INTERFACE 

 ! Variables
 INTEGER :: i1, i2, ians
 i1=1
 i2=2

 ! Body of fdllrun
 ians = add2i(i1, i2)
 print '(I3)', ians

end program fdllrun

In Intel Visual Studio Fortran, I tried to right click on my project and do Add -> Existing Item, then browse to the DLL file located in C:\temp, then Build All. It does not work. The errors I get when I try to compile are:

Error 1  error LNK2019: unresolved external symbol __imp_add2i referenced in function _MAIN__ fdllrun.obj
Error 2  fatal error LNK1120: 1 unresolved externals Release\fdllrun.exe 

I have tried several other Fortran compilers out there, including g95 and gfortran (both MinGW and TDM-GCC varieties), but have not had any luck with anything I have tried. TDM-GCC gfortran is perhaps the closest I got, as I could run a DLL created from gfortran, but not in C. In fact, in TDM-GCC gfortran, after I created a Fortran DLL that was referencing a C DLL, all function calls to the Fortran generated DLL within Excel VBA would immediately return 0, even after no warnings were given after compiling the Fortran DLL! In other cases, it said it could not find the function name in the DLL, even though I made them without decorations or underscores. Excluding the C DLL from that build would eliminate that problem, but of course, I need to reference the C DLL as it has required 3rd party functions.

I looked at other posts and could not find an answer to this question:

Please provide a step-by-step guide to getting Fortran to work with DLLIMPORT, with both gfortran and Intel Visual Studio Fortran, if possible. I just want something that works...

Community
  • 1
  • 1
  • 1
    do you have to use DLLIMPORT (restricting yourself to Intel Visual Fortran) or can you use the modern Fortran `iso_c_binding` module instead? – casey Oct 02 '15 at 21:01
  • Did you use `DLLEXPORT` for those symbols when you compiled `fdlltest.dll`? – bdforbes Oct 03 '15 at 23:46
  • Ok, I went and asked the same question on Intel Fortran's board, and got a solution that worked. Here is the link: [link](https://software.intel.com/en-us/forums/intel-visual-fortran-compiler-for-windows/topic/594972#comment-1842547) – Jesse Brumbaugh Oct 05 '15 at 14:21

0 Answers0