我正在尝试为红宝石创建GR framework绑定。我用小提琴。小提琴是默认的扩展,用于使用ruby翻译外部功能接口(FFI)。它在Linux和Mac上运行良好。但是在Windows上,出现以下错误。
code hoge.rb
require 'fiddle/import'
module M
extend extend Fiddle::Importer
dlload File.expand_path('gr/bin/libGR.dll').gsub("/", "\\")
end
错误
Traceback (most recent call last):
7: from hoge.rb:3:in `<main>'
6: from hoge.rb:5:in `<module:M>'
5: from C:/Ruby26-x64/lib/ruby/2.6.0/fiddle/import.rb:77:in `dlload'
4: from C:/Ruby26-x64/lib/ruby/2.6.0/fiddle/import.rb:77:in `collect'
3: from C:/Ruby26-x64/lib/ruby/2.6.0/fiddle/import.rb:87:in `block in dlload'
2: from C:/Ruby26-x64/lib/ruby/2.6.0/fiddle.rb:47:in `dlopen'
1: from C:/Ruby26-x64/lib/ruby/2.6.0/fiddle.rb:47:in `new'
C:/Ruby26-x64/lib/ruby/2.6.0/fiddle.rb:47:in `initialize': No such file or directory (Fiddle::DLError)
5: from hoge.rb:3:in `<main>'
4: from hoge.rb:5:in `<module:M>'
3: from C:/Ruby26-x64/lib/ruby/2.6.0/fiddle/import.rb:77:in `dlload'
2: from C:/Ruby26-x64/lib/ruby/2.6.0/fiddle/import.rb:77:in `collect'
1: from C:/Ruby26-x64/lib/ruby/2.6.0/fiddle/import.rb:86:in `block in dlload'
C:/Ruby26-x64/lib/ruby/2.6.0/fiddle/import.rb:89:in `rescue in block in dlload': can't load C:\Users\kojix2\gr\bin\libgr.dll (Fiddle::DLError)
ruby-ffi运作良好。
require 'ffi'
module M
extend FFI::Library
ffi_lib File.expand_path('gr/bin/libGR.dll').gsub("/", "\\")
end
但是这次我想用小提琴代替ruby-ffi。接下来我该怎么办?
我回答我自己的问题。
1。使用RubyInstaller::Runtime.add_dll_directory
DLL加载https://github.com/oneclick/rubyinstaller2/wiki/For-gem-developers#-dll-loading
PATH
环境变量将被所有DLL搜索忽略。RUBY_DLL_PATH
在ruby启动时被解释-以后的更改不会影响正在运行的进程。 add_dll_directory
对DLL搜索路径进行运行时更改。2。使用SetDllDirectory()
或者,您可以运行SetDllDirectory。
require 'fiddle/import'
require 'fiddle/types'
module WinAPI
extend Fiddle::Importer
dlload 'kernel32.dll'
include Fiddle::Win32Types
extern 'int SetDllDirectory(LPCSTR)'
end
WinAPI.SetDllDirectory(File.expand_path(path))