Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

windows10 cannot load library #154

Open
Smawexi opened this issue Jan 12, 2025 · 6 comments
Open

windows10 cannot load library #154

Smawexi opened this issue Jan 12, 2025 · 6 comments

Comments

@Smawexi
Copy link

Smawexi commented Jan 12, 2025

There is a corresponding DLL file in the directory, but the corresponding file cannot be found

import cffi
ffi = cffi.FFI()
lib = ffi.dlopen("D:/Work/produce/HuiHua25/tests/libapi.dll")

Execution error, output:

Traceback (most recent call last):
  File "D:\Work\produce\HuiHua25\tests\test.py", line 38, in <module>
    lib = ffi.dlopen("D:/Work/produce/HuiHua25/tests/libapi.dll")
  File "D:\fsy\Anaconda3\lib\site-packages\cffi\api.py", line 150, in dlopen
    lib, function_cache = _make_ffi_library(self, name, flags)
  File "D:\fsy\Anaconda3\lib\site-packages\cffi\api.py", line 834, in _make_ffi_library
    backendlib = _load_backend_lib(backend, libname, flags)
  File "D:\fsy\Anaconda3\lib\site-packages\cffi\api.py", line 830, in _load_backend_lib
    return backend.load_library(path, flags)
OSError: cannot load library 'D:/Work/produce/HuiHua25/tests/libapi.dll': error 0x7e

D:\Work\produce\HuiHua25\tests>dir
 驱动器 D 中的卷是 Data
 卷的序列号是 7E57-7B60

 D:\Work\produce\HuiHua25\tests 的目录

2025/01/13  00:46    <DIR>          .
2025/01/13  00:46    <DIR>          ..
2025/01/13  00:00    <DIR>          build
2025/01/13  00:00           326,301 key.c
2025/01/13  00:00            52,224 key.cp39-win_amd64.pyd
2025/01/13  00:29             4,017 key.pyx
2025/01/12  20:56         7,833,177 libapi.dll
@arigo
Copy link
Contributor

arigo commented Jan 13, 2025

Can you check if #64 helps?

@Smawexi
Copy link
Author

Smawexi commented Jan 13, 2025

Can you check if #64 helps?

Found a temporary solution, this can load normally.
Without adding the winmode=0 parameter, an error will occur

import ctypes
ctypes.CDLL("D:/Work/produce/HuiHua25/tests/libapi.dll", winmode=0)

@Smawexi
Copy link
Author

Smawexi commented Jan 13, 2025

I don't know how to solve it on CFFi

@arigo
Copy link
Contributor

arigo commented Jan 13, 2025

Are you running the latest version of CFFI?

@Smawexi
Copy link
Author

Smawexi commented Jan 13, 2025

Are you running the latest version of CFFI?

CFFI version: 1.17.1

@arigo
Copy link
Contributor

arigo commented Jan 13, 2025

I don't understand how the ctypes winmode=0 can help fix the problem. When called with a full path, the default value of winmode should be nt._LOAD_LIBRARY_SEARCH_DEFAULT_DIRS | nt._LOAD_LIBRARY_SEARCH_DLL_LOAD_DIR. If you specify 0 explicitly, then these two flags are not present and Windows does not search for dependent DLLs elsewhere. How doing that makes it work is beyond me.

I'd recommend to try to understand why you get this Windows error 0x7e in ctypes or not depending on the flags you pass in ctypes' winmode argument.

In recent versions of CFFI you can also pass flags by calling ffi.dlopen("some/path", flags), but the value flags=0 is the default here and it will be replaced with real flags like in the winmode=None case of ctypes. Try with another value of flags, e.g. 16.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants