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:



237
238
239
# File 'lib/win32/registry.rb', line 237

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

.CloseKey(hkey) ⇒ Object



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

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

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



281
282
283
284
285
286
287
# File 'lib/win32/registry.rb', line 281

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



327
328
329
# File 'lib/win32/registry.rb', line 327

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

.DeleteValue(hkey, name) ⇒ Object



323
324
325
# File 'lib/win32/registry.rb', line 323

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

.EnumKey(hkey, index) ⇒ Object



296
297
298
299
300
301
302
# File 'lib/win32/registry.rb', line 296

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[0, unpackdw(size)], unpackqw(wtime) ]
end

.EnumValue(hkey, index) ⇒ Object



289
290
291
292
293
294
# File 'lib/win32/registry.rb', line 289

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[0, unpackdw(size)]
end

.FlushKey(hkey) ⇒ Object



331
332
333
# File 'lib/win32/registry.rb', line 331

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

.make_wstr(str) ⇒ Object



271
272
273
# File 'lib/win32/registry.rb', line 271

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

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



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

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



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

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

.packhandle(h) ⇒ Object



245
246
247
# File 'lib/win32/registry.rb', line 245

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

.packqw(qw) ⇒ Object



262
263
264
# File 'lib/win32/registry.rb', line 262

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

.QueryInfoKey(hkey) ⇒ Object



339
340
341
342
343
344
345
346
347
348
349
350
351
352
# File 'lib/win32/registry.rb', line 339

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



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

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



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

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



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

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

.unpackhandle(h) ⇒ Object



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

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

.unpackqw(qw) ⇒ Object



266
267
268
269
# File 'lib/win32/registry.rb', line 266

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

.win64?Boolean

Returns:

  • (Boolean)


241
242
243
# File 'lib/win32/registry.rb', line 241

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