Module: Win32::Registry::API

Extended by:
Importer
Includes:
Constants
Defined in:
lib/win32/registry.rb

Overview

Win32 APIs

Constant Summary

Constants included from Constants

Constants::HKEY_CLASSES_ROOT, Constants::HKEY_CURRENT_CONFIG, Constants::HKEY_CURRENT_USER, Constants::HKEY_DYN_DATA, Constants::HKEY_LOCAL_MACHINE, Constants::HKEY_PERFORMANCE_DATA, Constants::HKEY_PERFORMANCE_NLSTEXT, Constants::HKEY_PERFORMANCE_TEXT, Constants::HKEY_USERS, Constants::KEY_ALL_ACCESS, Constants::KEY_CREATE_LINK, Constants::KEY_CREATE_SUB_KEY, Constants::KEY_ENUMERATE_SUB_KEYS, Constants::KEY_EXECUTE, Constants::KEY_NOTIFY, Constants::KEY_QUERY_VALUE, Constants::KEY_READ, Constants::KEY_SET_VALUE, Constants::KEY_WRITE, Constants::MAX_KEY_LENGTH, Constants::MAX_VALUE_LENGTH, Constants::REG_BINARY, Constants::REG_CREATED_NEW_KEY, Constants::REG_DWORD, Constants::REG_DWORD_BIG_ENDIAN, Constants::REG_DWORD_LITTLE_ENDIAN, Constants::REG_EXPAND_SZ, Constants::REG_FORCE_RESTORE, Constants::REG_FULL_RESOURCE_DESCRIPTOR, Constants::REG_LEGAL_OPTION, Constants::REG_LINK, Constants::REG_MULTI_SZ, Constants::REG_NONE, Constants::REG_NO_LAZY_FLUSH, Constants::REG_OPENED_EXISTING_KEY, Constants::REG_OPTION_BACKUP_RESTORE, Constants::REG_OPTION_CREATE_LINK, Constants::REG_OPTION_NON_VOLATILE, Constants::REG_OPTION_OPEN_LINK, Constants::REG_OPTION_RESERVED, Constants::REG_OPTION_VOLATILE, Constants::REG_QWORD, Constants::REG_QWORD_LITTLE_ENDIAN, Constants::REG_REFRESH_HIVE, Constants::REG_RESOURCE_LIST, Constants::REG_RESOURCE_REQUIREMENTS_LIST, Constants::REG_SZ, Constants::REG_WHOLE_HIVE_VOLATILE, Constants::STANDARD_RIGHTS_READ, Constants::STANDARD_RIGHTS_WRITE

Class Method Summary collapse

Class Method Details

.check(result) ⇒ Object

Raises:



248
249
250
# File 'lib/win32/registry.rb', line 248

def check(result)
  raise Error, result, caller(1) if result != 0
end

.CloseKey(hkey) ⇒ Object



346
347
348
# File 'lib/win32/registry.rb', line 346

def CloseKey(hkey)
  check RegCloseKey.call(hkey)
end

.CreateKey(hkey, name, opt, desired) ⇒ Object



292
293
294
295
296
297
298
# File 'lib/win32/registry.rb', line 292

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)
  [ unpackhandle(result), unpackdw(disp) ]
end

.DeleteKey(hkey, name) ⇒ Object



338
339
340
# File 'lib/win32/registry.rb', line 338

def DeleteKey(hkey, name)
  check RegDeleteKey.call(hkey, make_wstr(name))
end

.DeleteValue(hkey, name) ⇒ Object



334
335
336
# File 'lib/win32/registry.rb', line 334

def DeleteValue(hkey, name)
  check RegDeleteValue.call(hkey, make_wstr(name))
end

.EnumKey(hkey, index) ⇒ Object



307
308
309
310
311
312
313
# File 'lib/win32/registry.rb', line 307

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)
  [ name.byteslice(0, unpackdw(size) * WCHAR_SIZE), unpackqw(wtime) ]
end

.EnumValue(hkey, index) ⇒ Object



300
301
302
303
304
305
# File 'lib/win32/registry.rb', line 300

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)
  name.byteslice(0, unpackdw(size) * WCHAR_SIZE)
end

.FlushKey(hkey) ⇒ Object



342
343
344
# File 'lib/win32/registry.rb', line 342

def FlushKey(hkey)
  check RegFlushKey.call(hkey)
end

.make_wstr(str) ⇒ Object



282
283
284
# File 'lib/win32/registry.rb', line 282

def make_wstr(str)
  str.encode(WCHAR)
end

.OpenKey(hkey, name, opt, desired) ⇒ Object



286
287
288
289
290
# File 'lib/win32/registry.rb', line 286

def OpenKey(hkey, name, opt, desired)
  result = packhandle(0)
  check RegOpenKeyExW.call(hkey, make_wstr(name), opt, desired, result)
  unpackhandle(result)
end

.packdw(dw) ⇒ Object



264
265
266
# File 'lib/win32/registry.rb', line 264

def packdw(dw)
  [dw].pack('V')
end

.packhandle(h) ⇒ Object



256
257
258
# File 'lib/win32/registry.rb', line 256

def packhandle(h)
  win64? ? packqw(h) : packdw(h)
end

.packqw(qw) ⇒ Object



273
274
275
# File 'lib/win32/registry.rb', line 273

def packqw(qw)
  [ qw & 0xFFFFFFFF, qw >> 32 ].pack('VV')
end

.QueryInfoKey(hkey) ⇒ Object



350
351
352
353
354
355
356
357
358
359
360
361
362
363
# File 'lib/win32/registry.rb', line 350

def QueryInfoKey(hkey)
  subkeys = packdw(0)
  maxsubkeylen = packdw(0)
  values = packdw(0)
  maxvaluenamelen = packdw(0)
  maxvaluelen = packdw(0)
  secdescs = packdw(0)
  wtime = ' ' * 8
  check RegQueryInfoKey.call(hkey, 0, 0, 0, subkeys, maxsubkeylen, 0,
    values, maxvaluenamelen, maxvaluelen, secdescs, wtime)
  [ unpackdw(subkeys), unpackdw(maxsubkeylen), unpackdw(values),
    unpackdw(maxvaluenamelen), unpackdw(maxvaluelen),
    unpackdw(secdescs), unpackqw(wtime) ]
end

.QueryValue(hkey, name) ⇒ Object



315
316
317
318
319
320
321
322
323
# File 'lib/win32/registry.rb', line 315

def QueryValue(hkey, name)
  type = packdw(0)
  size = packdw(0)
  name = make_wstr(name)
  check RegQueryValueExW.call(hkey, name, 0, type, 0, size)
  data = "\0".force_encoding('ASCII-8BIT') * unpackdw(size)
  check RegQueryValueExW.call(hkey, name, 0, type, data, size)
  [ unpackdw(type), data[0, unpackdw(size)] ]
end

.SetValue(hkey, name, type, data, size) ⇒ Object



325
326
327
328
329
330
331
332
# File 'lib/win32/registry.rb', line 325

def SetValue(hkey, name, type, data, size)
  case type
  when REG_SZ, REG_EXPAND_SZ, REG_MULTI_SZ
    data = data.encode(WCHAR)
    size ||= data.size + 1
  end
  check RegSetValueExW.call(hkey, make_wstr(name), 0, type, data, size)
end

.unpackdw(dw) ⇒ Object



268
269
270
271
# File 'lib/win32/registry.rb', line 268

def unpackdw(dw)
  dw += [0].pack('V')
  dw.unpack('V')[0]
end

.unpackhandle(h) ⇒ Object



260
261
262
# File 'lib/win32/registry.rb', line 260

def unpackhandle(h)
  win64? ? unpackqw(h) : unpackdw(h)
end

.unpackqw(qw) ⇒ Object



277
278
279
280
# File 'lib/win32/registry.rb', line 277

def unpackqw(qw)
  qw = qw.unpack('VV')
  (qw[1] << 32) | qw[0]
end

.win64?Boolean

Returns:

  • (Boolean)


252
253
254
# File 'lib/win32/registry.rb', line 252

def win64?
  /^(?:x64|x86_64)/ =~ RUBY_PLATFORM
end