I need help to call a Fortran shared library (DLL) from a QGIS python plugin.

For this purpose I created a simple Fortran dll with the function to add two integers.

Also I create a simple python code that invokes the Fortran dll to perform the addition.

This python code successfully got executed using python.exe in the Python installed path (and the Python.exe in the QGIS Bin directory)

But on trying to load this dll from QGIS > Plugin > Python console, with the below two lines,

from ctypes import *

self.gtflib = cdll.LoadLibrary('fadd.dll')

the following error is displayed:

Traceback (most recent call last): File "", line 1, in File "C:\PROGRA~2\QGISWI~1\apps\Python27\lib\ctypes__init__.py", line 443, in LoadLibrary return self._dlltype(name) File "C:\PROGRA~2\QGISWI~1\apps\Python27\lib\ctypes__init__.py", line 365, in init self._handle = _dlopen(self._name, mode) WindowsError: [Error 126] The specified module could not be found

The same error happens in my actual plugin too.

I have the fadd.dll copied to the 'C:\PROGRA~2\QGISWI~1\bin'.

Is there anything specific needed in QGIS to load the fortran dll.

The Fortran source code:

integer function add2i(x, y)
  integer, intent(in) :: x, y
  add2i = x + y
end function add2i

This is compiled with gfortan on windows to create fadd.dll.

The Python code:

from ctypes import *
libadd = cdll.LoadLibrary("./fadd.dll")

method = libadd.add2i_
x = c_int(47)
y = c_int(11)
print "x = %d, y = %d" % (x.value, y.value)
result = method( byref(x), byref(y) )
print "x = %d, y = %d, z = %d" % (x.value, y.value, result)

, and the Python file to invoke the dll is attached.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.