Module: Win32::Registry::API

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

Overview

Win32 APIs

Constant Summary collapse

TEMPLATE_HANDLE =
'J<'
TEMPLATE_DWORD =
'V'
TEMPLATE_QWORD =
'Q<'

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:


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

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

.CloseKey(hkey) ⇒ Object


351
352
353
# File 'lib/win32/registry.rb', line 351

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

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


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

def CreateKey(hkey, name, opt, desired)
  result = packhandle(0)
  disp = packdw(0)
  check RegCreateKeyExW.call(hkey, make_wstr(name), 0, 0, opt, desired,
                             nil, result, disp)
  [ unpackhandle(result), unpackdw(disp) ]
end

.DeleteKey(hkey, name) ⇒ Object


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

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

.DeleteValue(hkey, name) ⇒ Object


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

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

.EnumKey(hkey, index) ⇒ Object


312
313
314
315
316
317
318
# File 'lib/win32/registry.rb', line 312

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

.EnumValue(hkey, index) ⇒ Object


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

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

.FlushKey(hkey) ⇒ Object


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

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

.make_wstr(str) ⇒ Object


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

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

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


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

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


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

def packdw(dw)
  [dw].pack(TEMPLATE_DWORD)
end

.packhandle(h) ⇒ Object


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

def packhandle(h)
  [h].pack(TEMPLATE_HANDLE)
end

.packqw(qw) ⇒ Object


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

def packqw(qw)
  [qw].pack(TEMPLATE_QWORD)
end

.QueryInfoKey(hkey) ⇒ Object


355
356
357
358
359
360
361
362
363
364
365
366
367
368
# File 'lib/win32/registry.rb', line 355

def QueryInfoKey(hkey)
  subkeys = packdw(0)
  maxsubkeylen = packdw(0)
  values = packdw(0)
  maxvaluenamelen = packdw(0)
  maxvaluelen = packdw(0)
  secdescs = packdw(0)
  wtime = ' ' * 8
  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),
    unpackdw(secdescs), unpackqw(wtime) ]
end

.QueryValue(hkey, name) ⇒ Object


320
321
322
323
324
325
326
327
328
# File 'lib/win32/registry.rb', line 320

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

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


330
331
332
333
334
335
336
337
# File 'lib/win32/registry.rb', line 330

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

.unpackdw(dw) ⇒ Object


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

def unpackdw(dw)
  (dw + [0].pack(TEMPLATE_DWORD)).unpack1(TEMPLATE_DWORD)
end

.unpackhandle(h) ⇒ Object


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

def unpackhandle(h)
  (h + [0].pack(TEMPLATE_HANDLE)).unpack1(TEMPLATE_HANDLE)
end

.unpackqw(qw) ⇒ Object


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

def unpackqw(qw)
  (qw + [0].pack(TEMPLATE_QWORD)).unpack1(TEMPLATE_QWORD)
end

.win64?Boolean

Returns:

  • (Boolean)

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

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