title: Importing DLL into Fortran using DLLIMPORT
body: <p>I am having problems importing a DLL into my current Fortran project. The DLL file I am trying to import, <code>fdlltest.dll</code>, has the following functions defined when I do <code>dumpbin /exports</code>:</p>
  
  <pre><code>C:\temp&gt;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
  </code></pre>
  
  <p>Next, I need to know step-by-step how to import the  <code>add2i</code>, <code>add2r</code>, and <code>simpson</code> functions from this DLL located in <code>C:\temp</code> 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:</p>
  
  <pre><code>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
  </code></pre>
  
  <p>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:</p>
  
  <pre><code>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 
  </code></pre>
  
  <p>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.</p>
  
  <p>I looked at other posts and could not find an answer to this question:</p>
  
  <ul>
  <li><p>My question is not <a href="https://stackoverflow.com/questions/22134259/fortran-dll-import">importing a Fortran DLL into c++ or C#</a></p></li>
  <li><p>I do not get the <a href="https://stackoverflow.com/questions/10381739/run-fortran-dll-with-visual-studio">"Add as Link" option when right clicking
  Add->Existing Item</a></p></li>
  <li><p>The answer from <a href="https://stackoverflow.com/questions/1237129/how-to-call-c-sharp-code-from-fortran-dll">this post</a> did not solve my question</p></li>
  <li><p>A similar question <a href="https://stackoverflow.com/questions/23433508/fortran-calling-a-c-dll-which-in-turn-calls-another-fortran-dll">here</a> was not answered adequately</p></li>
  </ul>
  
  <p>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...</p>