Skip to content

Commit

Permalink
Issue python-cffi#139: tweak the logic for LoadLibraryEx() calls foll…
Browse files Browse the repository at this point in the history
…owing ctypes
  • Loading branch information
arigo committed Oct 29, 2024
1 parent 6870f5a commit c915708
Showing 1 changed file with 14 additions and 4 deletions.
18 changes: 14 additions & 4 deletions src/c/misc_win32.h
Original file line number Diff line number Diff line change
Expand Up @@ -191,13 +191,19 @@ static PyObject *b_getwinerror(PyObject *self, PyObject *args, PyObject *kwds)
#define RTLD_GLOBAL 0
#define RTLD_LOCAL 0

#define MAX_BUF_FULLPATH 32768

static void *dlopen(const char *filename, int flags)
{
char buffer[MAX_BUF_FULLPATH];
if (flags == 0) {
flags = LOAD_LIBRARY_SEARCH_DEFAULT_DIRS;
for (const char *p = filename; *p != 0; p++)
if (*p == '\\' || *p == '/') {
flags = LOAD_LIBRARY_SEARCH_DEFAULT_DIRS |
LOAD_LIBRARY_SEARCH_DLL_LOAD_DIR;
DWORD r = GetFullPathNameA(filename, MAX_BUF_FULLPATH, buffer, NULL);
if (r > 0 && r < MAX_BUF_FULLPATH)
filename = buffer;
flags |= LOAD_LIBRARY_SEARCH_DLL_LOAD_DIR;
break;
}
}
Expand All @@ -206,11 +212,15 @@ static void *dlopen(const char *filename, int flags)

static void *dlopenWinW(const wchar_t *filename, int flags)
{
wchar_t buffer[MAX_BUF_FULLPATH];
if (flags == 0) {
flags = LOAD_LIBRARY_SEARCH_DEFAULT_DIRS;
for (const wchar_t *p = filename; *p != 0; p++)
if (*p == '\\' || *p == '/') {
flags = LOAD_LIBRARY_SEARCH_DEFAULT_DIRS |
LOAD_LIBRARY_SEARCH_DLL_LOAD_DIR;
DWORD r = GetFullPathNameW(filename, MAX_BUF_FULLPATH, buffer, NULL);
if (r > 0 && r < MAX_BUF_FULLPATH)
filename = buffer;
flags |= LOAD_LIBRARY_SEARCH_DLL_LOAD_DIR;
break;
}
}
Expand Down

0 comments on commit c915708

Please sign in to comment.