python-cffi / cffi

A Foreign Function Interface package for calling C libraries from Python.
https://cffi.readthedocs.io/en/latest/
Other
124 stars 41 forks source link

Exit code appears when using _ffi.dlopen #132

Open kjdsfbchkadwbcuiwbcfiw opened 1 month ago

kjdsfbchkadwbcuiwbcfiw commented 1 month ago

I recently encountered an issue while executing the following Python code on my Windows 11 system:

> import cffi
> _ffi = cffi.FFI()
> _ffi.cdef("int raise(int);")
> _lib = _ffi.dlopen("api-ms-win-crt-runtime-l1-1-0.dll")

Upon running this script, the process terminated abruptly with an exit code of -1073740791 (0xC0000409). I verified that the 'api-ms-win-crt-runtime-l1-1-0.dll' file is located in the C:\Windows\System32\downlevel directory. Could you please help me identify the cause of this problem?

arigo commented 1 month ago

I have no solid idea, but my guess would be that loading this DLL into the Python process just crashes; this might be because the DLL contains code to make sure that you don't try to load multiple incompatible versions of the basic "crt" runtime.

I'm unsure what you are trying to do. If you want to call a basic function like raise() then it should be provided by the standard C library that the Python process is using, which might be a different one. I'm not sure how you do that, but Google might help. Maybe you can find about it by googling "how to find out which standard C library DLL a process is using on Windows" or something with the keyword "ctypes".

arigo commented 1 month ago

Note: if I remember correctly, on Python 2.7 you could use ffi.dlopen(None) to do exactly that. This doesn't work any more on Python 3.x.

Note 2: if you have the compiler toolchain, then you can just call ffi.setsource("#include <signal.h>") and ffi.compile() and it should work out of the box.

Note 3: if all you want is call raise(), note that you should be able to just use the signal module from Python.

kjdsfbchkadwbcuiwbcfiw commented 1 month ago

This is not my code. I encountered this error when importing the trio library, so I checked the source code of the library and found that the problem was caused by the code above. Thank you for your reply, I will take a closer look.