Skip to content

How to change dll search path? #97

@nstbayless

Description

@nstbayless

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.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions