Project

General

Profile

Actions

Bug #17657

closed

Starting from ruby 2.3.0, LoadLibraryExA called in extension won't use PATH or current directory to find library and/or it's dependencies

Bug #17657: Starting from ruby 2.3.0, LoadLibraryExA called in extension won't use PATH or current directory to find library and/or it's dependencies

Added by egzi (Krzysztof Egzmont) over 4 years ago. Updated over 4 years ago.

Status:
Rejected
Assignee:
-
Target version:
-
ruby -v:
ruby 2.7.2p137 (2020-10-01 revision 5445e04352) [x64-mingw32]
[ruby-core:102606]

Description

I'm on windows 10 pro. The sample code runs successfully on ruby up to 2.2.5.
On newer versions I get "cannot load such file". The fallowing documentation lists PATH environment variable as one of the search paths, that are used to locate DLL modules, but it gets ignored.
https://docs.microsoft.com/en-us/windows/win32/dlls/dynamic-link-library-search-order#standard-search-order-for-desktop-applications
I suspect some permission issues but I read LoadLibraryExA and I can't see why this might be happening. I tried changing dwFlags, doesn't help. If I missed something obvious then I'm sorry.
I tried to boil down the problem to minimal sample code. It needs those two files and a lib directory with any DLL that would be loaded with load_lib global function.

I know that passing absolute path will make it work, but there is still problem when the library has some dependencies that are located in a different directory, those can't be find neither.

test.rb:

require 'devkit'
require 'mkmf'
create_makefile 'foobar'
`make`
require_relative 'foobar'

ENV['PATH'] = (Dir.pwd + '\lib;').gsub('/', '\\') + ENV['PATH']

load_lib('bz2.dll')

foobar.c:

#include "ruby.h"

VALUE rb_load_lib(VALUE self, VALUE path)
{
    HMODULE mod;
    printf("%s\n\n", getenv("PATH"));
    mod = LoadLibraryExA(RSTRING_PTR(path), NULL, 0);
    printf("LoadLibraryExA: %s\n", mod != 0 ? "SUCCESS" : "FAIL");
    return Qnil;
}

void Init_foobar()
{
    rb_define_global_function("load_lib", rb_load_lib, 1);
}

Updated by egzi (Krzysztof Egzmont) over 4 years ago Actions #1 [ruby-core:102617]

Well, it turns out I did miss one thing
https://github.com/oneclick/rubyinstaller2/wiki/For-gem-developers#-dll-loading

Sorry, please close this issue.

Updated by hsbt (Hiroshi SHIBATA) over 4 years ago Actions #2

  • Status changed from Open to Rejected
Actions

Also available in: PDF Atom