I'm trying to convert a local package written for python 2.7 to python 3.x. The package is a wrapper around a series of fortran DLLs. The package work fine on my computer when running in 2.7 however when loading the DLL in 3.11 I get
OSError: [WinError 1114] A dynamic link library (DLL) initialization routine failed
My issue can be replicated with the following code (simplified 2.7 code)
from ctypes.util import find_library
from ctypes import CDLL
library_path = find_library("libSEMFEM")
clib = CDLL(library_path,0x102)
The directory to libSEMFEM.dll in in my PATH variable.
Following through the ctypes implementation, ctypes eventually calls
self._handle = _dlopen(self._name, mode)
In python 2.7 the mode becomes 0x102
, in Python 3.11 (if winmode=None) the mode becomes 0x1100
. According to the LoadLibraryExA documentation 0x1100 is LOAD_LIBRARY_SEARCH_DEFAULT_DIRS | LOAD_LIBRARY_SEARCH_DLL_LOAD_DIR
.
If I execute the code in python 2.7 it just works end the Windows Event Log in clear, if I do the same in 3.11 it gives the
OSError: [WinError 1114] A dynamic link library (DLL) initialization routine failed
and in the Event log i get:
Faulting application name: python.exe, version: 3.11.2150.1013, time stamp: 0x63e27f1e
Faulting module name: libgfortran_64-3.dll, version: 0.0.0.0, time stamp: 0x52472432
Exception code: 0xc0000005
Fault offset: 0x0000000000017e9b
Faulting process id: 0x3804
Faulting application start time: 0x01d9609c52389bf3
Faulting application path: C:\Program Files\Python311\python.exe
Faulting module path: C:\SEMFEM\lib64\libgfortran_64-3.dll
Report Id: cf0bf001-762a-4112-81f5-568e0eb0859f
Faulting package full name:
Faulting package-relative application ID:
I followed the ctype implementation in 3.11 and saw mode
is ignored and build a new value based on winmode
. Setting winmode=0x102
the DLL loads however I can't access any functions and get an AttributeError: function 'FUNCTION_NAME' not found
.
I would have expected that the same DLL on the same computer being called from the newer version of python would work basically the same.
I saw that the event log error points to libgfortran_64-3.dll. I tried loading that in stead and get the exact same error in the event log.
I ran dependency walker on libSEMFEM.dll and libgfortran_64-3.dll does show up. So I don't know what I'm missing.
EDIT: based on @CristiFati comment I've added some more detail if I successfully call the DLL from python 2.7 it looks like this
The only way I can get python 3.11 not to fail on CDLL it to pass in winmode=0x1
which doesn't load dependencies and the other dll dont load but at least the base is the same.
I can only think it has to do with the search path being used but in my mind mode = 0x1100
should 'just' work