如何用Ruby小提琴加载Windows DLL文件?

问题描述 投票:1回答:1

我正在尝试为红宝石创建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)
  • Windows 10
  • Ruby 2.6.5 + DevKit

ruby-ffi运作良好。

require 'ffi'

module M
   extend FFI::Library
   ffi_lib File.expand_path('gr/bin/libGR.dll').gsub("/", "\\")
end

但是这次我想用小提琴代替ruby-ffi。接下来我该怎么办?

ruby windows dll fiddle ruby-ffi
1个回答
0
投票

我回答我自己的问题。

1。使用RubyInstaller::Runtime.add_dll_directory

DLL加载https://github.com/oneclick/rubyinstaller2/wiki/For-gem-developers#-dll-loading

  • PATH环境变量将被所有DLL搜索忽略。
  • 环境变量RUBY_DLL_PATH在ruby启动时被解释-以后的更改不会影响正在运行的进程。
  • 使用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))
© www.soinside.com 2019 - 2024. All rights reserved.