title: Calling a FORTRAN DLL using ctypes
body: <p>I am trying to learn how to complie FORTRAN code into a DLL that I can call from Python using ctypes. Even a simple example is not working, can anyone help?</p>
  
  <p>I have a single procedure in FORTRAN:</p>
  
  <pre><code>  subroutine ex(i)
    integer i
    i=i+1
    return
    end 
  </code></pre>
  
  <p>Then I try to run this from Python</p>
  
  <p>I compile it with the MinGW complier as follows  </p>
  
  <pre><code>gfortran -c test.f
  gfortran -shared -mrtd -o test.dll test.o
  </code></pre>
  
  <p>Looking at the DLL created I see</p>
  
  <pre><code>Microsoft (R) COFF/PE Dumper Version 12.00.30723.0
  Copyright (C) Microsoft Corporation.  All rights reserved.
  
  Dump of file test.dll
  
  File Type: DLL
  
  Section contains the following exports for test.dll
  
  00000000 characteristics
         0 time date stamp Thu Jan 01 13:00:00 1970
      0.00 version
         1 ordinal base
         1 number of functions
         1 number of names
  
  ordinal hint RVA      name
  
        1    0 00001280 ex_
  
  Summary
  
      1000 .CRT
      1000 .bss
      1000 .data
      1000 .edata
      1000 .eh_fram
      1000 .idata
      1000 .rdata
      1000 .reloc
      1000 .text
      1000 .tls
  </code></pre>
  
  <p>Then I try to access this from Python</p>
  
  <pre><code>from ctypes import *
  
  DLL = windll.test
  print DLL
  
  print getattr(DLL,'ex_')
  print DLL[1]
  print DLL.ex_
  
  x = pointer( c_int(3) )
  DLL.ex_( x )
  </code></pre>
  
  <p>The output is</p>
  
  <pre><code>&lt;WinDLL 'test', handle 6bec0000 at 2143850&gt;
  
  &lt;_FuncPtr object at 0x020E88A0&gt;
  &lt;_FuncPtr object at 0x020E8918&gt;
  &lt;_FuncPtr object at 0x020E88A0&gt;
  Traceback (most recent call last):
    File "C:\proj_py\test.py", line 20, in &lt;module&gt;
      DLL.ex_( x )
  ValueError: Procedure probably called with too many arguments (4 bytes in excess) 
  </code></pre>
  
  <p>So although the function is there, I'm not calling it correctly. I'm stumped.</p>
  
  <p>I am using python 2.7.10 (32 bit) on a 64-bit Windows-7 machine, I have a recent version of the MinGW compiler:</p>
  
  <pre><code>$ gfortran -v
  Using built-in specs.
  COLLECT_GCC=c:\mingw\bin\gfortran.exe
  COLLECT_LTO_WRAPPER=c:/mingw/bin/../libexec/gcc/mingw32/4.8.1/lto-wrapper.exe
  Target: mingw32
  Configured with: ../gcc-4.8.1/configure --prefix=/mingw --host=mingw32 --build=mingw32 --without-pic --enable-shared --e
  nable-static --with-gnu-ld --enable-lto --enable-libssp --disable-multilib --enable-languages=c,c++,fortran,objc,obj-c++
  ,ada --disable-sjlj-exceptions --with-dwarf2 --disable-win32-registry --enable-libstdcxx-debug --enable-version-specific
  -runtime-libs --with-gmp=/usr/src/pkg/gmp-5.1.2-1-mingw32-src/bld --with-mpc=/usr/src/pkg/mpc-1.0.1-1-mingw32-src/bld --
  with-mpfr= --with-system-zlib --with-gnu-as --enable-decimal-float=yes --enable-libgomp --enable-threads --with-libiconv
  -prefix=/mingw32 --with-libintl-prefix=/mingw --disable-bootstrap LDFLAGS=-s CFLAGS=-D_USE_32BIT_TIME_T
  Thread model: win32
  gcc version 4.8.1 (GCC) 
  </code></pre>
  
  <p>Can anyone offer a solution?</p>
  
  <p>Thanks</p>