15 мая 2023 года "Исходники.РУ" отмечают своё 23-летие!
Поздравляем всех причастных и неравнодушных с этим событием!
И огромное спасибо всем, кто был и остаётся с нами все эти годы!

Главная Форум Журнал Wiki DRKB Discuz!ML Помощь проекту


Как записать\прочитать ключ в реестр

 

'Registry keys
Public Const HKEY_CLASSES_ROOT = &H80000000
Public Const HKEY_CURRENT_USER = &H80000001
Public Const HKEY_LOCAL_MACHINE = &H80000002
Public Const HKEY_USERS = &H80000003
Public Const HKEY_PERFORMANCE_DATA = &H80000004
Public Const HKEY_CURRENT_CONFIG = &H80000005
Public Const HKEY_DYN_DATA = &H80000006

'Registry access constants
Public Const KEY_QUERY_VALUE = &H1 'Permission to query subkey data.
Public Const KEY_SET_VALUE = &H2 'Permission to set subkey data.
Public Const KEY_CREATE_SUB_KEY = &H4
Public Const KEY_ENUMERATE_SUB_KEYS = &H8
Public Const KEY_NOTIFY = &H10
Public Const KEY_CREATE_LINK = &H20
Public Const KEY_READ = KEY_QUERY_VALUE Or KEY_ENUMERATE_SUB_KEYS Or KEY_NOTIFY
Public Const KEY_WRITE = KEY_SET_VALUE Or KEY_CREATE_SUB_KEY
Public Const KEY_ALL_ACCESS = KEY_QUERY_VALUE Or KEY_ENUMERATE_SUB_KEYS Or KEY_NOTIFY Or KEY_CREATE_SUB_KEY Or KEY_CREATE_LINK Or KEY_SET_VALUE

Public Const REG_OPTION_NON_VOLATILE = 0&
Public Const REG_OPTION_VOLATILE = &H1

Public Type SECURITY_ATTRIBUTES
      nLength As Long
      lpSecurityDescriptor As Long
      bInheritHandle As Long
End Type

Public Enum RegTypes
      RegNonee = 0
      RegSZ = 1
      RegExpandSz = 2
      RegBinary = 3
      RegDword = 4
      RegDwordLittleEndian = 4
      RegDwordBigEndian = 5
      RegLink = 6
      RegMultiSz = 7
      RegResourceList = 8
      RegFulResourceDesc = 9
End Enum

Declare Function RegOpenKeyEx Lib "advapi32" Alias "RegOpenKeyExA" (ByVal hKey As Long, ByVal lpSubKey As String, ByVal ulOptions As Long, ByVal samDesired As Long, phkResult As Long) As Long

Declare Function RegSetValueEx Lib "advapi32" Alias "RegSetValueExA" (ByVal hKey As Long, ByVal lpValueName As String, ByVal Reserved As Long, ByVal dwType As Long, ByVal szData As String, ByVal cbData As Long) As Long

Declare Function RegCloseKey Lib "advapi32" (ByVal hKey As Long) As Long

Declare Function RegQueryValueEx Lib "advapi32" Alias "RegQueryValueExA" (ByVal hKey As Long, ByVal lpValueName As String, ByVal lpReserved As Long, ByRef lpType As Long, ByVal szData As String, ByRef lpcbData As Long) As Long

Declare Function RegCreateKeyEx Lib "advapi32" Alias "RegCreateKeyExA" (ByVal hKey As Long, ByVal lpSubKey As String, ByVal Reserved As Long, ByVal lpClass As String, ByVal dwOptions As Long, ByVal samDesired As Long, ByRef lpSecurityAttributes As SECURITY_ATTRIBUTES, phkResult As Long, lpdwDisposition As Long) As Long

Public Function RegGetValue(Root As Long, SubKey As String, Key As String) As String
      Dim Buffer As String, hKey As Long, nType As Long, nSize As Long
      RegGetValue = ""
      If Not RegOpenKeyEx(Root, SubKey, 0, KEY_READ, hKey) Then
            nSize = 0
            RegQueryValueEx hKey, Key, 0, nType, Buffer, nSize
            If hKey And nSize > 0 And nType = RegSZ Then
                  Buffer = Space(nSize + 1)
                  RegQueryValueEx hKey, Key, 0, nType, Buffer, nSize
                  RegGetValue = Left(Buffer, nSize - 1)
                  RegCloseKey hKey
            End If
      End If
End Function

Public Sub RegSetValue(Root As Long, SubKey As String, Key As String, value As String)
      Dim hKey As Long, sa As SECURITY_ATTRIBUTES, nDisp As Long
      If Not RegCreateKeyEx(Root, SubKey, 0, vbNull, REG_OPTION_NON_VOLATILE, KEY_ALL_ACCESS, sa, hKey, nDisp) Then
            RegSetValueEx hKey, Key, 0, RegSZ, value, Len(value) + 1
            RegCloseKey hKey
      End If
End Sub

Использование:
sUser = RegGetValue(HKEY_LOCAL_MACHINE, "Software\Microsoft\Windows\CurrentVersion", "RegisteredOwner")

RegSetValue HKEY_LOCAL_MACHINE, "Software\Microsoft\Windows\CurrentVersion", "RegisteredOwner", "Darth Vader"

Примечание:Эти функции работают только с текстовыми ключами (те, что в RegEdit'e помечены символом 'ab').