Option Explicit
'Module level code.
'RegCreateKeyEx
'Creates the specified registry key.
Public 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
'RegOpenKeyEx
'Opens the specified registry key.
Public 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
'RegQueryValueEx
'Retrieves the type and data for a specified value name associated with an open registry key.
Public 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
'RegSetValueEx
'Sets the data and type of a specified value under a registry key.
Public Declare Function RegSetValueEx Lib "advapi32.dll" Alias "RegSetValueExA" _
(ByVal hkey As Long, ByVal lpValueName As String, ByVal Reserved As Long, _
ByVal dwType As Long, lpData As Any, ByVal cbData As Long) As Long
'Note that if you declare the lpData parameter as String, you must pass it By Value.
'RegCloseKey.
'Closes a handle to the specified registry key.
Public Declare Function RegCloseKey Lib "advapi32.dll" (ByVal hkey As Long) As Long
'Custom error handling.
Public Const ERROR_FAILED = -999&
'Registry type constants.
Public Const REG_SZ = 1 'Unicode nul terminated string.
'Registry return values.
Public Const ERROR_SUCCESS = 0&
Public Const ERROR_MORE_DATA = 234
'Registry HKEY constants.
Public Const HKEY_LOCAL_MACHINE = &H80000002
'Registry manipulation
Public Const REG_OPTION_NON_VOLATILE = 0
Public Const KEY_QUERY_VALUE = &H1
Public Const KEY_SET_VALUE = &H2
Public Const KEY_CREATE_SUB_KEY = &H4
'REG_SZ - 1 off each write/read functions.
Public Function RegWriteSZ(lnghKey As Long, strSubKey As String, strValueName As String, strValue As String) As Long
'Create the key and respective REG_SZ value.
Dim lngRetval As Long
Dim lngKeyHandle As Long
'Set the default return value.
RegWriteSZ = ERROR_FAILED
'Create the key.
If RegCreateKeyEx(lnghKey, strSubKey, 0&, 0&, REG_OPTION_NON_VOLATILE, KEY_CREATE_SUB_KEY, _
ByVal 0&, lngKeyHandle, lngRetval) = ERROR_SUCCESS Then
'Open the new key.
If RegOpenKeyEx(lnghKey, strSubKey, 0&, KEY_SET_VALUE, lngKeyHandle) = ERROR_SUCCESS Then
'Write it to the registry, and return a code.
RegWriteSZ = RegSetValueEx(lngKeyHandle, strValueName, 0&, REG_SZ, _
ByVal strValue, Len(strValue))
End If
End If
'Close any key opened with RegCreateKeyEx.
Call RegCloseKey(lngKeyHandle)
End Function
Public Function RegReadSZ(lnghKey As Long, strSubKey As String, strValueName As String, strRetVal As String) As Long
'Read the REG_SZ value.
Dim lngDataType As Long
Dim lngDataSize As Long
Dim lngKeyHandle As Long
'Reset the return variable.
strRetVal = ""
'Set the default return value.
RegReadSZ = ERROR_FAILED
'Open the key.
If RegOpenKeyEx(lnghKey, strSubKey, 0&, KEY_QUERY_VALUE, lngKeyHandle) = ERROR_SUCCESS Then
'Get the key`s data length. It should ALWAYS return ERROR_MORE_DATA.
If RegQueryValueEx(lngKeyHandle, strValueName, 0&, lngDataType, 0&, lngDataSize) = ERROR_MORE_DATA Then
'Test for string value.
If lngDataType = REG_SZ Then
'Size the string.
strRetVal = Space(lngDataSize)
'Get the current value, and return a code.
RegReadSZ = RegQueryValueEx(lngKeyHandle, strValueName, 0&, 0&, _
ByVal strRetVal, lngDataSize)
End If
End If
End If
'Close any key opened with RegOpenKeyEx.
Call RegCloseKey(lngKeyHandle)
End Function
Public Function Encrypt(Password As String) As String
'Your encryption code here...
Encrypt = Password 'Temporary...
End Function
Public Function Decrypt(Password As String) As String
'Your encryption code here...
Decrypt = Password 'Temporary...
End Function