Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ jobs:
if: ${{ startsWith(github.repository, 'ruby/') || github.event_name != 'schedule' }}
uses: ruby/actions/.github/workflows/ruby_versions.yml@master
with:
engine: cruby
engine: cruby-jruby
min_version: 3.3

build:
Expand Down
33 changes: 18 additions & 15 deletions lib/win32/registry.rb
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ def initialize(code)
buff = WCHAR_NUL * 1024
lang = 0
begin
len = FormatMessageW.call(0x1200, 0, code, lang, buff, 1024, 0)
len = FormatMessageW.call(0x1200, nil, code, lang, buff, 1024, nil)
msg = buff.byteslice(0, len * WCHAR_SIZE)
msg.delete!(WCHAR_CR)
msg.chomp!
Expand All @@ -198,7 +198,7 @@ def initialize(code)
#
class PredefinedKey < Registry
def initialize(hkey, keyname)
@hkey = hkey
@hkey = Fiddle::Pointer.new(hkey)
@parent = nil
@keyname = keyname
@disposition = REG_OPENED_EXISTING_KEY
Expand Down Expand Up @@ -238,7 +238,7 @@ module API
"long RegDeleteKeyW(void *, void *)",
"long RegFlushKey(void *)",
"long RegCloseKey(void *)",
"long RegQueryInfoKey(void *, void *, void *, void *, void *, void *, void *, void *, void *, void *, void *, void *)",
"long RegQueryInfoKeyW(void *, void *, void *, void *, void *, void *, void *, void *, void *, void *, void *, void *)",
].each do |fn|
cfunc = extern fn, :stdcall
const_set cfunc.name.intern, cfunc
Expand Down Expand Up @@ -285,7 +285,7 @@ def unpackqw(qw)
end

def make_wstr(str)
str.encode(WCHAR)
(str+"\0").encode(WCHAR)
end

def OpenKey(hkey, name, opt, desired)
Expand All @@ -298,32 +298,32 @@ def CreateKey(hkey, name, opt, desired)
result = packhandle(0)
disp = packdw(0)
check RegCreateKeyExW.call(hkey, make_wstr(name), 0, 0, opt, desired,
0, result, disp)
nil, result, disp)
[ unpackhandle(result), unpackdw(disp) ]
end

def EnumValue(hkey, index)
name = WCHAR_NUL * Constants::MAX_KEY_LENGTH
size = packdw(Constants::MAX_KEY_LENGTH)
check RegEnumValueW.call(hkey, index, name, size, 0, 0, 0, 0)
check RegEnumValueW.call(hkey, index, name, size, nil, nil, nil, nil)
name.byteslice(0, unpackdw(size) * WCHAR_SIZE)
end

def EnumKey(hkey, index)
name = WCHAR_NUL * Constants::MAX_KEY_LENGTH
size = packdw(Constants::MAX_KEY_LENGTH)
wtime = ' ' * 8
check RegEnumKeyExW.call(hkey, index, name, size, 0, 0, 0, wtime)
check RegEnumKeyExW.call(hkey, index, name, size, nil, nil, nil, wtime)
[ name.byteslice(0, unpackdw(size) * WCHAR_SIZE), unpackqw(wtime) ]
end

def QueryValue(hkey, name)
type = packdw(0)
size = packdw(0)
name = make_wstr(name)
check RegQueryValueExW.call(hkey, name, 0, type, 0, size)
check RegQueryValueExW.call(hkey, name, nil, type, nil, size)
data = "\0".b * unpackdw(size)
check RegQueryValueExW.call(hkey, name, 0, type, data, size)
check RegQueryValueExW.call(hkey, name, nil, type, data, size)
[ unpackdw(type), data[0, unpackdw(size)] ]
end

Expand Down Expand Up @@ -360,7 +360,7 @@ def QueryInfoKey(hkey)
maxvaluelen = packdw(0)
secdescs = packdw(0)
wtime = ' ' * 8
check RegQueryInfoKey.call(hkey, 0, 0, 0, subkeys, maxsubkeylen, 0,
check RegQueryInfoKeyW.call(hkey, 0, 0, 0, subkeys, maxsubkeylen, 0,
values, maxvaluenamelen, maxvaluelen, secdescs, wtime)
[ unpackdw(subkeys), unpackdw(maxsubkeylen), unpackdw(values),
unpackdw(maxvaluenamelen), unpackdw(maxvaluelen),
Expand Down Expand Up @@ -430,7 +430,7 @@ def self.time2wtime(time)
# If block is given, the key is closed automatically.
def self.open(hkey, subkey, desired = KEY_READ, opt = REG_OPTION_RESERVED)
subkey = subkey.chomp('\\')
newkey = API.OpenKey(hkey.hkey, subkey, opt, desired)
newkey = API.OpenKey(hkey.instance_variable_get(:@hkey), subkey, opt, desired)
obj = new(newkey, hkey, subkey, REG_OPENED_EXISTING_KEY)
if block_given?
begin
Expand All @@ -457,7 +457,7 @@ def self.open(hkey, subkey, desired = KEY_READ, opt = REG_OPTION_RESERVED)
# If block is given, the key is closed automatically.
#
def self.create(hkey, subkey, desired = KEY_ALL_ACCESS, opt = REG_OPTION_RESERVED)
newkey, disp = API.CreateKey(hkey.hkey, subkey, opt, desired)
newkey, disp = API.CreateKey(hkey.instance_variable_get(:@hkey), subkey, opt, desired)
obj = new(newkey, hkey, subkey, disp)
if block_given?
begin
Expand All @@ -479,16 +479,14 @@ def self.create(hkey, subkey, desired = KEY_ALL_ACCESS, opt = REG_OPTION_RESERVE
# initialize
#
def initialize(hkey, parent, keyname, disposition)
@hkey = hkey
@hkey = Fiddle::Pointer.new(hkey)
@parent = parent
@keyname = keyname
@disposition = disposition
@hkeyfinal = [ hkey ]
ObjectSpace.define_finalizer self, @@final.call(@hkeyfinal)
end

# Returns key handle value.
attr_reader :hkey
# Win32::Registry object of parent key, or nil if predefeined key.
attr_reader :parent
# Same as subkey value of Registry.open or
Expand All @@ -497,6 +495,11 @@ def initialize(hkey, parent, keyname, disposition)
# Disposition value (REG_CREATED_NEW_KEY or REG_OPENED_EXISTING_KEY).
attr_reader :disposition

# Returns key handle value.
def hkey
@hkey.to_i
end

#
# Returns if key is created ((*newly*)).
# (see Registry.create) -- basically you call create
Expand Down
4 changes: 3 additions & 1 deletion test/win32/test_registry.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
# frozen_string_literal: true

if /mswin|mingw|cygwin/ =~ RUBY_PLATFORM
require "rbconfig"

if /mswin|mingw|cygwin/ =~ RbConfig::CONFIG['host_os']
begin
require 'win32/registry'
rescue LoadError
Expand Down