VB icon

______A Edit Registry

Email
Submitted on: 1/8/2015 6:39:00 AM
By: Agent Smith (from psc cd)  
Level: Intermediate
User Rating: By 22 Users
Compatibility: VB 5.0, VB 6.0
Views: 3092
 
     How to Read and Write to the Registry using all HKEYS
 

Windows API/Global Declarations:

Can't Copy and Paste this?
Click here for a copy-and-paste friendly version of this code!
'**************************************
'Windows API/Global Declarations for :______A Edit Registry
'**************************************
Declare Function RegEnumValue Lib "advapi32.dll" Alias "RegEnumValueA" (ByVal hKey As Long, ByVal dwIndex As Long, ByVal lpValueName As String, lpcbValueName As Long, ByVal lpReserved As Long, lpType As Long, ByVal lpData As String, lpcbData As Long) As Long
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 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, lpSecurityAttributes As SECURITY_ATTRIBUTES, phkResult As Long, lpdwDisposition As Long) As Long
#If Win32 Then
 
 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 KEY_ALL_ACCESS = &H3F
 Public Const REG_OPTION_NON_VOLATILE = 0&
 Public Const REG_CREATED_NEW_KEY = &H1
 Public Const REG_OPENED_EXISTING_KEY = &H2
 Public Const ERROR_SUCCESS = 0&
 Public Const REG_SZ = (1)
#End If
Type SECURITY_ATTRIBUTES
 
 nLength As Long
 lpSecurityDescriptor As Long
 bInheritHandle As Boolean
End Type
code:
Can't Copy and Paste this?
Click here for a copy-and-paste friendly version of this code!
				
'**************************************
' Name: ______A Edit Registry
' Description:How to Read and Write to the Registry using all HKEYS
' By: Agent Smith (from psc cd)
'**************************************

Public Function bSetRegValue(ByVal hKey As Long, ByVal lpszSubKey As String, ByVal sSetValue As String, ByVal sValue As String) As Boolean
 
 On Error Resume Next
 Dim phkResult As Long
 Dim lResult As Long
 Dim SA As SECURITY_ATTRIBUTES
 Dim lCreate As Long
 RegCreateKeyEx hKey, lpszSubKey, 0, "", REG_OPTION_NON_VOLATILE, KEY_ALL_ACCESS, SA, phkResult, lCreate
 lResult = RegSetValueEx(phkResult, sSetValue, 0, REG_SZ, sValue, CLng(Len(sValue) + 1))
 RegCloseKey phkResult
 bSetRegValue = (lResult = ERROR_SUCCESS)
 
End Function
Public Function bGetRegValue(ByVal hKey As Long, ByVal sKey As String, ByVal sSubKey As String) As String
 
 Dim lResult As Long
 Dim phkResult As Long
 Dim dWReserved As Long
 Dim szBuffer As String
 Dim lBuffSize As Long
 Dim szBuffer2 As String
 Dim lBuffSize2 As Long
 Dim lIndex As Long
 Dim lType As Long
 Dim sCompKey As String
 
 lIndex = 0
 lResult = RegOpenKeyEx(hKey, sKey, 0, 1, phkResult)
 Do While lResult = ERROR_SUCCESS And Not (bFound)
szBuffer = Space(255)
lBuffSize = Len(szBuffer)
szBuffer2 = Space(255)
lBuffSize2 = Len(szBuffer2)
lResult = RegEnumValue(phkResult, lIndex, szBuffer, lBuffSize, dWReserved, lType, szBuffer2, lBuffSize2)
If (lResult = ERROR_SUCCESS) Then
 sCompKey = Left(szBuffer, lBuffSize)
 If (sCompKey = sSubKey) Then
bGetRegValue = Left(szBuffer2, lBuffSize2 - 1)
 End If
End If
lIndex = lIndex + 1
 
 Loop
 RegCloseKey phkResult
End Function


Other 4 submission(s) by this author

 


Report Bad Submission
Use this form to tell us if this entry should be deleted (i.e contains no code, is a virus, etc.).
This submission should be removed because:

Your Vote

What do you think of this code (in the Intermediate category)?
(The code with your highest vote will win this month's coding contest!)
Excellent  Good  Average  Below Average  Poor (See voting log ...)
 

Other User Comments


 There are no comments on this submission.
 

Add Your Feedback
Your feedback will be posted below and an email sent to the author. Please remember that the author was kind enough to share this with you, so any criticisms must be stated politely, or they will be deleted. (For feedback not related to this particular code, please click here instead.)
 

To post feedback, first please login.