zugbruecke icon indicating copy to clipboard operation
zugbruecke copied to clipboard

How to change dll search path?

Open nstbayless opened this issue 1 year ago • 2 comments

I want to load a dll /path/to/library.dll, but I'm working directory /some/other/path. This is okay, unless library.dll requires /path/to/dependency.dll, in which case it doesn't load at all.

This works:

os.chdir("/path/to/")
import zugbruecke.ctypes as ctypes
ctypes.windll.LoadLibrary("library.dll")

However, this does not work (!):

import zugbruecke.ctypes as ctypes
os.chdir("/path/to/")
ctypes.windll.LoadLibrary("library.dll")

-- It results in an an OSError. As do the following:

os.environ["PATH"] += ":/path/to"
import zugbruecke.ctypes as ctypes
ctypes.windll.LoadLibrary("library.dll")
import zugbruecke.ctypes as ctypes
k32 = ctypes.windll.LoadLibrary("kernel32.dll")
k32.SetDllDirectoryA.argtypes = [ctypes.c_char_p]
k32.SetDllDirectoryA.restype = ctypes.c_int
k32.SetDllDirectoryA.memsync = [dict(pointer=[0], null=True)]
k32.SetDllDirectoryA("/path/to")
ctypes.windll.LoadLibrary("library.dll")

export PATH="$PATH:/path/to" before running this script also results in an OSError.

The solution I found only partially works. Ideally, I don't want to have to change the working directory (those DLL's won't be able to load files as expected, I think?). Also, it doesn't work if I want to load DLL's from multiple paths.

nstbayless avatar Apr 24 '23 01:04 nstbayless

Resolved: os.environ["WINEPATH"]="/path/to" works.

nstbayless avatar Apr 24 '23 02:04 nstbayless

Actually -- it would be nice to be able to edit the dll search path after importing zugbruecke, which os.environ["WINEPATH"]="/path/to" doesn't do.

nstbayless avatar Apr 24 '23 02:04 nstbayless