예의 초기 화면이다.



소스이다. 레지스트리 이용 16,32,64비트에 따라 사용하는 함수와,방법이 약간 차이가 난다. 아래는 32비트 소스이다.
' 레지스트리 값 생성,읽기,쓰기,삭제
Option Explicit

Private Declare Function RegOpenKeyEx Lib "advapi32.dll" Alias "RegOpenKeyExA" (ByVal hKey As Long, ByVal lpSubKey As String, ByVal ulOptions As Long, ByVal samDesired As Long, phkResult As Long) As Long
Private Declare Function RegCloseKey Lib "advapi32.dll" (ByVal hKey As Long) As Long
Private Declare Function RegCreateKeyEx Lib "advapi32.dll" 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, lpSecurityAttributes As Long, phkResult As Long, lpdwDisposition As Long) As Long
Private Declare Function RegQueryValueEx Lib "advapi32.dll" Alias "RegQueryValueExA" (ByVal hKey As Long, ByVal lpValueName As String, ByVal lpReserved As Long, lpType As Long, lpData As Any, lpcbData As Long) As Long
Private Declare Function RegDeleteKey Lib "advapi32.dll" Alias "RegDeleteKeyA" (ByVal hKey As Long, ByVal lpSubKey As String) As Long
Private Declare Function RegDeleteValue Lib "advapi32.dll" Alias "RegDeleteValueA" (ByVal hKey As Long, ByVal lpValueName As String) As Long
Private Declare Function RegSetValueEx Lib "advapi32.dll" Alias "RegSetValueExA" (ByVal hKey As Long, ByVal lpValueName As String, ByVal Reserved As Long, ByVal dwType As Long, ByVal lpData As Any, ByVal cbData As Long) As Long


Private Const HKEY_CLASSES_ROOT = &H80000000
Private Const HKEY_CURRENT_USER = &H80000001
Private Const HKEY_LOCAL_MACHINE = &H80000002
Private Const HKEY_USERS = &H80000003
Private Const HKEY_CURRENT_CONFIG = &H80000005
Private Const ERROR_SUCCESS = 0
Private Const KEY_ALL_ACCESS = &HF003F
Private Const REG_CREATED_NEW_KEY = &H1
Private Const REG_OPTION_NON_VOLATILE = 0
Private Const REG_SZ = 1

Private Enum ButtonType
    BT_Creadte ' 생성,쓰기
    BT_Read ' 읽기
    BT_Remove ' 삭제
End Enum

Private Sub Form_Load()
    AddItemToCombo "HKEY_CLASSES_ROOT", HKEY_CLASSES_ROOT
    AddItemToCombo "HKEY_CURRENT_USER", HKEY_CURRENT_USER
    AddItemToCombo "HKEY_LOCAL_MACHINE", HKEY_LOCAL_MACHINE
    AddItemToCombo "HKEY_USERS", HKEY_USERS
    AddItemToCombo "HKEY_CURRENT_CONFIG", HKEY_CURRENT_CONFIG
    cmbRootKey.Text = "HKEY_LOCAL_MACHINE"
    txtPath.Text = "Software\Microsoft\Windows\CurrentVersion\Run\Test"
    txtValueName.Text = "Test"
    txtValue.Text = "Test"
End Sub

Private Sub AddItemToCombo(ByVal Str As String, ByVal Val As Long) ' 콤보박스에 아이템을 추가한다.
    With cmbRootKey
        .AddItem Str
        .ItemData(.ListCount - 1) = Val
    End With
End Sub

Private Sub cmdResitry_Click(Index As Integer) ' 생성,읽기,쓰기,제거 버튼 클릭시
    Dim Result As Long ' 반환값
    Dim RetVal As Long ' 오픈된 키의 핸들

    RetVal = RegOpenKeyEx(cmbRootKey.ItemData(cmbRootKey.ListIndex), txtPath.Text, 0, KEY_ALL_ACCESS, Result) ' 레지스트리 키를 오픈한다.
    If Not RetVal = ERROR_SUCCESS Then ' 열기 실패,존재하지 않는 키라면
        Select Case Index
            Case BT_Creadte ' 생성,쓰기
                RetVal = RegCreateKeyEx(cmbRootKey.ItemData(cmbRootKey.ListIndex), txtPath.Text, 0, vbNullString, REG_OPTION_NON_VOLATILE, KEY_ALL_ACCESS, 0, Result, REG_CREATED_NEW_KEY)
            Case BT_Read, BT_Remove ' 읽기,삭제
                MsgBox "존재하지 않는 키입니다."
                Exit Sub
        End Select
    End If
    Select Case Index
        Case BT_Creadte ' 생성,쓰기
            RetVal = RegSetValueEx(Result, txtValueName, 0&, REG_SZ, txtValue.Text, CLng(Len(txtValue.Text) + 1))
        Case BT_Read ' 읽기
            Dim ReadDataType As Long
            Dim ReadData As String
            Dim ReadDataLen As Long
           
            RetVal = RegQueryValueEx(Result, txtValueName.Text, 0&, ReadDataType, 0&, ReadDataLen)
            If Not ReadDataLen = 0 Then
                ReadData = String(100, " ")
                ReadDataLen = 100
                RetVal = RegQueryValueEx(Result, txtValueName.Text, 0&, 0&, ByVal ReadData, ReadDataLen)
                txtValue.Text = Left(ReadData, InStr(1, ReadData, Chr(0)) - 1)
            End If
        Case BT_Remove ' 삭제
            If txtValueName.Text = vbNullString Then ' 값명이 지정되지 않았다면
                RetVal = RegDeleteKey(Result, vbNullString)
            Else ' 값명이 지정되었다면
                RetVal = RegDeleteValue(Result, txtValueName.Text)
            End If
    End Select
    RetVal = RegCloseKey(Result) ' 레지스트리 키를 닫는다
End Sub

+ Recent posts