Skip to content

Instantly share code, notes, and snippets.

@djberg96
Last active August 29, 2015 14:14
Show Gist options
  • Save djberg96/83dc8bc3060c81668623 to your computer and use it in GitHub Desktop.
Save djberg96/83dc8bc3060c81668623 to your computer and use it in GitHub Desktop.
Windows registry test, MRI vs JRuby
require 'Win32API'
hkey = 'SYSTEM\CurrentControlSet\Services\Tcpip\Parameters'
WCHAR = Encoding::UTF_16LE
HKEY_LOCAL_MACHINE = 0x80000002
STANDARD_RIGHTS_READ = 0x00020000
KEY_QUERY_VALUE = 0x0001
KEY_ENUMERATE_SUB_KEYS = 0x0008
KEY_NOTIFY = 0x0010
KEY_READ = STANDARD_RIGHTS_READ |
KEY_QUERY_VALUE |
KEY_ENUMERATE_SUB_KEYS |
KEY_NOTIFY
RegOpenKeyExA = Win32API.new('advapi32', 'RegOpenKeyExA', 'LPLLP', 'L')
RegQueryValueExA = Win32API.new('advapi32', 'RegQueryValueExA', 'LPLPPP', 'L')
RegQueryValueExW = Win32API.new('advapi32', 'RegQueryValueExW', 'LPLPPP', 'L')
handle = [0].pack('L')
rv = RegOpenKeyExA.call(
HKEY_LOCAL_MACHINE,
hkey,
0,
KEY_READ,
handle
)
if rv != 0
raise "RegOpenKeyExA failed"
end
handle = handle.unpack('L').first
name = "DataBasePath"
wname = name.encode(WCHAR)
# First call, get size
size = [0].pack('L')
type = [0].pack('L')
#RegQueryValueExA.call(handle, name, 0, type, 0, size)
rv = RegQueryValueExW.call(handle, wname, 0, type, 0, size)
# JRuby fails here
if rv != 0 && rv != ERROR_MORE_DATA
raise SystemCallError.new('RegQueryValueEx', rv)
end
# Now use that data for second call
#data = 0.chr * size.unpack('L').first
data = "\0".force_encoding('ASCII-8BIT') * size.unpack('L').first
#RegQueryValueExA.call(handle, name, 0, type, data, size)
rv = RegQueryValueExW.call(handle, wname, 0, type, data, size)
if rv != 0
raise "RegQueryValueEx failed"
end
p data
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment