Option Explicit
Private Const MyHKEY =[COLOR=#ff0000] [B]&H80000001[/B][/COLOR] ' HKEY_CURRENT_USER
Private Const MyKey_Name =[COLOR=#ff0000] "[B]Control Panel\GPE[/B]"[/COLOR]
Private Const MyData_Type = [COLOR=#ff0000]4[/COLOR] '"[B]REG_DWORD[/B]" - Kieu so 32 bit
'Private Const MyData_Type = [COLOR=#ff0000]1[/COLOR] '[B]"REG_SZ[/B]" - Kieu chuoi Unicode
Private Const MyData_Name =[COLOR=#ff0000] "[B]Mien cat trang[/B]"[/COLOR]
Private Const KEY_ALL_ACCESS = &H2003F
Private Declare Function RegOpenKeyEx Lib "advapi32" Alias "RegOpenKeyExA" ( _
ByVal hKey As Long, ByVal lpSubKey As String, ByVal ulOptions As Long, _
ByVal samDesired As Long, ByRef phkResult As Long) As Long
Private Declare Function RegCreateKey Lib "advapi32.dll" Alias "RegCreateKeyA" ( _
ByVal hKey As Long, ByVal lpSubKey As String, phkResult As Long) 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, lpData As Any, ByVal cbData As Long) As Long
Private Declare Function RegDeleteValue Lib "advapi32.dll" Alias "RegDeleteValueA" ( _
ByVal hKey As Long, ByVal lpValueName As String) As Long
Private Declare Function RegCloseKey Lib "advapi32" (ByVal hKey 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 RegQueryValueEx Lib "advapi32" Alias "RegQueryValueExA" ( _
ByVal hKey As Long, ByVal lpValueName As String, ByVal lpReserved As Long, _
ByRef lpType As Long, ByVal lpData As String, ByRef lpcbData As Long) As Long
Public Function GetKeyDataValue(RegKeyRoot As Long, RegKeyName As String, KeyDateType As Long, KeyValueName As String)
Dim OpenKey As Long, hKey As Long, strTempVal As String, KeyValSize As Long, lngI As Long
OpenKey = RegOpenKeyEx(RegKeyRoot, RegKeyName, 0, KEY_ALL_ACCESS, hKey)
strTempVal = String$(1024, 0)
KeyValSize = 1024
OpenKey = RegQueryValueEx(hKey, KeyValueName, 0, KeyDateType, strTempVal, KeyValSize)
If (Asc(Mid(strTempVal, KeyValSize, 1)) = 0) Then
strTempVal = Left(strTempVal, KeyValSize - 1)
Else
strTempVal = Left(strTempVal, KeyValSize)
End If
Select Case KeyDateType
Case 1, 3:
GetKeyDataValue = strTempVal
Case 4:
For lngI = Len(strTempVal) To 1 Step -1
GetKeyDataValue = Format(Hex(Asc(Mid(strTempVal, lngI, 1))), "00")
Next
GetKeyDataValue = Format$("&h" + GetKeyDataValue)
End Select
OpenKey = RegCloseKey(hKey)
End Function
Private Sub SetKeyDataValue(RegKeyRoot As Long, RegKeyName As String, KeyDataType As Long, KeyValueName As String, KeyValueDate As Variant)
Dim OpenKey As Long, SetValue As Long, hKey As Long
OpenKey = RegOpenKeyEx(RegKeyRoot, RegKeyName, 0, KEY_ALL_ACCESS, hKey)
If (OpenKey <> 0) Then
Call RegCreateKey(RegKeyRoot, RegKeyName, hKey)
End If
Select Case KeyDataType
Case 1:
SetValue = RegSetValueEx(hKey, KeyValueName, 0&, KeyDataType, ByVal CStr(KeyValueDate & Chr$(0)), Len(KeyValueDate))
Case 3:
SetValue = RegSetValueEx(hKey, KeyValueName, 0&, KeyDataType, ByVal CStr(KeyValueDate & Chr$(0)), Len(KeyValueDate))
Case 4:
SetValue = RegSetValueEx(hKey, KeyValueName, 0&, KeyDataType, CLng(KeyValueDate), 4)
End Select
SetValue = RegCloseKey(hKey)
End Sub
Public Sub DeleteRegKey(RegKeyRoot As Long, RegKeyName As String)
Dim DeleteKey As Long
DeleteKey = RegDeleteKey(RegKeyRoot, RegKeyName)
End Sub
Public Sub DeleteRegValue(RegKeyRoot As Long, RegKeyName As String, KeyValueName As String)
Dim DeleteKeyValue As Long, hKey As Long
DeleteKeyValue = RegOpenKeyEx(RegKeyRoot, RegKeyName, 0, KEY_ALL_ACCESS, hKey)
DeleteKeyValue = RegDeleteValue(hKey, KeyValueName)
End Sub