Skip to content

Commit 3d09f51

Browse files
author
jaseg
committed
Windows: Improve DLL loading error messages
1 parent ef3f47c commit 3d09f51

File tree

1 file changed

+9
-1
lines changed

1 file changed

+9
-1
lines changed

mpv.py

+9-1
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,15 @@
3838
dll = ctypes.util.find_library('mpv-2.dll') or ctypes.util.find_library('libmpv-2.dll') or ctypes.util.find_library('mpv-1.dll')
3939
if dll is None:
4040
raise OSError('Cannot find mpv-1.dll, mpv-2.dll or libmpv-2.dll in your system %PATH%. One way to deal with this is to ship the dll with your script and put the directory your script is in into %PATH% before "import mpv": os.environ["PATH"] = os.path.dirname(__file__) + os.pathsep + os.environ["PATH"] If mpv-1.dll is located elsewhere, you can add that path to os.environ["PATH"].')
41-
backend = CDLL(dll)
41+
# flags argument: LOAD_LIBRARY_SEARCH_DEFAULT_DIRS | LOAD_LIBRARY_SEARCH_DLL_LOAD_DIR
42+
# cf. https://learn.microsoft.com/en-us/windows/win32/api/libloaderapi/nf-libloaderapi-loadlibraryexa
43+
try:
44+
backend = CDLL(dll, 0x00001000 | 0x00000100)
45+
except Exception as e:
46+
if not os.path.isabs(dll):
47+
raise OSError(f'ctypes.find_library found mpv.dll at {dll}, but ctypes.CDLL could not load it. It looks like find_library found mpv.dll under a relative path entry in %PATH%. Please make sure all paths in %PATH% are absolute. Instead of trying to load mpv.dll from the current working directory, put it somewhere next to your script and add that path to %PATH% using os.environ["PATH"] = os.path.dirname(__file__) + os.pathsep + os.environ["PATH"]') from e
48+
else:
49+
raise OSError(f'ctypes.find_library found mpv.dll at {dll}, but ctypes.CDLL could not load it.') from e
4250
fs_enc = 'utf-8'
4351
else:
4452
import locale

0 commit comments

Comments
 (0)