I've seen your thread "Working with Registry" several times. But I've no idea how to create subkeys inside HKEY_LOCAL_MACHINE. You have shown its constants, but don't know how to use it. Please, help me.
It still doesn't help me. Just write a line of code to create 2 Subkeys (FirstKey\SecondKey) under
HKEY_LOCAL_MACHINE\Software\Microsoft\Windows NT\Current Version\Explorer\.
'~~> The RegOpenKeyEx function opens the specified key.
Private Declare Function RegOpenKeyEx Lib "advapi32.dll" Alias "RegOpenKeyExA" _
(ByVal hKey As Long, ByVal lpSubKey As String, ByVal Reserved As Long, ByVal _
samDesired As Long, phkResult As Long) As Long
'~~> The RegCreateKeyEx function creates the specified key. If the key already exists
'~~> in the registry, the function opens it.
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 Any, phkResult As Long, lpdwDisposition As Long) As Long
Dim strKeyBase As String, strKey As String, Result As Long
Const HKEY_LOCAL_MACHINE = &H80000002
Const REG_OPTION_NON_VOLATILE = 0
Const STANDARD_RIGHTS_ALL = &H1F0000
Const KEY_QUERY_VALUE = &H1
Const KEY_SET_VALUE = &H2
Const KEY_CREATE_SUB_KEY = &H4
Const KEY_ENUMERATE_SUB_KEYS = &H8
Const KEY_NOTIFY = &H10
Const SYNCHRONIZE = &H100000
Const READ_CONTROL = &H20000
Const STANDARD_RIGHTS_READ = (READ_CONTROL)
Const STANDARD_RIGHTS_WRITE = (READ_CONTROL)
Const KEY_CREATE_LINK = &H20
Const KEY_ALL_ACCESS = ((STANDARD_RIGHTS_ALL Or KEY_QUERY_VALUE Or _
KEY_SET_VALUE Or KEY_CREATE_SUB_KEY Or KEY_ENUMERATE_SUB_KEYS Or _
KEY_NOTIFY Or KEY_CREATE_LINK) And (Not SYNCHRONIZE))
Private Sub Form_Load()
'~~> Key Path
strKeyBase = "SOFTWARE\Microsoft\Windows NT\CurrentVersion\Explorer\"
'~~> New Key that will be created
strKey = strKeyBase & "rajbdilip"
End Sub
Private Sub Command1_Click()
'~~> Check if the specified key exists under HKEY_LOCAL_MACHINE
RegOpenKeyEx HKEY_LOCAL_MACHINE, strKey, 0, KEY_ALL_ACCESS, Result
'~~> If the key doesn't exist, we create it
If Result = 0 Then
'~~> Create a new key under HKEY_LOCAL_MACHINE
RegCreateKeyEx HKEY_LOCAL_MACHINE, strKey, 0, "REG_DWORD", _
REG_OPTION_NON_VOLATILE, KEY_ALL_ACCESS, ByVal 0&, Result, ret
If Result = 0 Then
MsgBox "Error while creating the Key!!"
Exit Sub
End If
'~~> Inform user that the key has been created
MsgBox "Key Created..."
End If
End Sub
Last edited by Siddharth Rout; Jun 17th, 2010 at 05:17 AM.
Reason: Corrected Typo
A good exercise for the Heart is to bend down and help another up...
Please Mark your Thread "Resolved", if the query is solved
MyGear:
★ CPU ★ Ryzen 5 5800X
★ GPU ★ NVIDIA GeForce RTX 3080 TI Founder Edition
★ RAM ★ G. Skill Trident Z RGB 32GB 3600MHz
★ MB ★ ASUS TUF GAMING X570 (WI-FI) ATX Gaming
★ Storage ★ SSD SB-ROCKET-1TB + SEAGATE 2TB Barracuda IHD
★ Cooling ★ NOCTUA NH-D15 CHROMAX BLACK 140mm + 10 of Noctua NF-F12 PWM
★ PSU ★ ANTEC HCG-1000-EXTREME 1000 Watt 80 Plus Gold Fully Modular PSU
★ Case ★ LIAN LI PC-O11 DYNAMIC XL ROG (BLACK) (G99.O11DXL-X)
★ Monitor ★ LG Ultragear 27" 240Hz Gaming Monitor
★ Keyboard ★ TVS Electronics Gold Keyboard
★ Mouse ★ Logitech G502 Hero
Alright. Creation of Sub Keys it done. Now, I want to set the value of the DEFAULT (REG_SEZ). How can I do this??
Paste this in a module...
Code:
Public lngRetVal As Long
Public Const HKEY_LOCAL_MACHINE = &H80000002
Public Const REG_SZ As Long = 1
Public Declare Function RegSetValueEx Lib "advapi32.dll" Alias "RegSetValueExA" _
(ByVal lngRootKey As Long, ByVal lpValueName As String, ByVal Reserved As Long, _
ByVal dwType As Long, lpData As Any, ByVal cbData As Long) As Long
Public Declare Function RegCreateKey Lib "advapi32.dll" Alias "RegCreateKeyA" _
(ByVal lngRootKey As Long, ByVal lpSubKey As String, phkResult As Long) As Long
Public Declare Function RegCloseKey Lib "advapi32.dll" (ByVal lngRootKey As Long) As Long
'~~> USAGE: regUpdateValue HKEY_LOCAL_MACHINE, strKeyBase, "", strKeyValue
Public Sub regUpdateValue(ByVal lngRootKey As Long, ByVal strRegKeyPath As String, _
ByVal strRegSubKey As String, varRegData As Variant)
Dim lngKeyHandle As Long, lngDataType As Long, lngKeyValue As Long, strKeyValue As String
lngDataType = REG_SZ
'~~> Querying the key path
lngRetVal = RegCreateKey(lngRootKey, strRegKeyPath, lngKeyHandle)
'~~> Making it Null terminated
strKeyValue = Trim(varRegData) & Chr(0)
'~~> Setting the value
lngRetVal = RegSetValueEx(lngKeyHandle, strRegSubKey, 0&, lngDataType, _
ByVal strKeyValue, Len(strKeyValue))
'~~> Closing the key
lngRetVal = RegCloseKey(lngKeyHandle)
'~~> Update User
MsgBox "Key Updated"
End Sub
And then use this in the form...
Code:
Private Sub Command1_Click()
Dim strKeyBase As String, strKeyValue As String
'~~> We created this key Earlier
strKeyBase = "SOFTWARE\Microsoft\Windows NT\CurrentVersion\Explorer\rajbdilip"
'~~> Value for DEFAULT (REG_SEZ)
strKeyValue = "koolsid"
'~~> Note "(Default)" doesn't have a name so we use ""
regUpdateValue HKEY_LOCAL_MACHINE, strKeyBase, "", strKeyValue
End Sub
Hope this helps...
A good exercise for the Heart is to bend down and help another up...
Please Mark your Thread "Resolved", if the query is solved
MyGear:
★ CPU ★ Ryzen 5 5800X
★ GPU ★ NVIDIA GeForce RTX 3080 TI Founder Edition
★ RAM ★ G. Skill Trident Z RGB 32GB 3600MHz
★ MB ★ ASUS TUF GAMING X570 (WI-FI) ATX Gaming
★ Storage ★ SSD SB-ROCKET-1TB + SEAGATE 2TB Barracuda IHD
★ Cooling ★ NOCTUA NH-D15 CHROMAX BLACK 140mm + 10 of Noctua NF-F12 PWM
★ PSU ★ ANTEC HCG-1000-EXTREME 1000 Watt 80 Plus Gold Fully Modular PSU
★ Case ★ LIAN LI PC-O11 DYNAMIC XL ROG (BLACK) (G99.O11DXL-X)
★ Monitor ★ LG Ultragear 27" 240Hz Gaming Monitor
★ Keyboard ★ TVS Electronics Gold Keyboard
★ Mouse ★ Logitech G502 Hero
In the previous post, I found the following API Declaration:
Code:
Public Declare Function RegSetValueEx Lib "advapi32.dll" Alias "RegSetValueExA" _
(ByVal lngRootKey As Long, ByVal lpValueName As String, ByVal Reserved As Long, _
ByVal dwType As Long, lpData As Any, ByVal cbData As Long) As Long
Please combine your previous and current codes which both create the subkeys and sets the value of Default. I got confused. The compiler said ,"Variable Not Defined" for ret and REG_SEZ.
Public lngRetVal As Long
Public Const REG_SZ As Long = 1
Public Declare Function RegSetValueEx Lib "advapi32.dll" Alias "RegSetValueExA" _
(ByVal lngRootKey As Long, ByVal lpValueName As String, ByVal Reserved As Long, _
ByVal dwType As Long, lpData As Any, ByVal cbData As Long) As Long
Public Declare Function RegCreateKey Lib "advapi32.dll" Alias "RegCreateKeyA" _
(ByVal lngRootKey As Long, ByVal lpSubKey As String, phkResult As Long) As Long
Public Declare Function RegCloseKey Lib "advapi32.dll" (ByVal lngRootKey As Long) As Long
'~~> USAGE: regUpdateValue HKEY_LOCAL_MACHINE, strKeyBase, "", strKeyValue
Public Sub regUpdateValue(ByVal lngRootKey As Long, ByVal strRegKeyPath As String, _
ByVal strRegSubKey As String, varRegData As Variant)
Dim lngKeyHandle As Long, lngDataType As Long, lngKeyValue As Long, strKeyValue As String
lngDataType = REG_SZ
'~~> Querying the key path
lngRetVal = RegCreateKey(lngRootKey, strRegKeyPath, lngKeyHandle)
'~~> Making it Null terminated
strKeyValue = Trim(varRegData) & Chr(0)
'~~> Setting the value
lngRetVal = RegSetValueEx(lngKeyHandle, strRegSubKey, 0&, lngDataType, _
ByVal strKeyValue, Len(strKeyValue))
'~~> Closing the key
lngRetVal = RegCloseKey(lngKeyHandle)
'~~> Update User
MsgBox "Key Updated"
End Sub
and this in the form code area
Code:
'~~> The RegOpenKeyEx function opens the specified key.
Private Declare Function RegOpenKeyEx Lib "advapi32.dll" Alias "RegOpenKeyExA" _
(ByVal hKey As Long, ByVal lpSubKey As String, ByVal Reserved As Long, ByVal _
samDesired As Long, phkResult As Long) As Long
'~~> The RegCreateKeyEx function creates the specified key. If the key already exists
'~~> in the registry, the function opens it.
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 Any, phkResult As Long, lpdwDisposition As Long) As Long
Dim strKeyBase As String, strKey As String, strKeyValue As String, Result As Long
Const HKEY_LOCAL_MACHINE = &H80000002
Const REG_OPTION_NON_VOLATILE = 0
Const STANDARD_RIGHTS_ALL = &H1F0000
Const KEY_QUERY_VALUE = &H1
Const KEY_SET_VALUE = &H2
Const KEY_CREATE_SUB_KEY = &H4
Const KEY_ENUMERATE_SUB_KEYS = &H8
Const KEY_NOTIFY = &H10
Const SYNCHRONIZE = &H100000
Const READ_CONTROL = &H20000
Const STANDARD_RIGHTS_READ = (READ_CONTROL)
Const STANDARD_RIGHTS_WRITE = (READ_CONTROL)
Const KEY_CREATE_LINK = &H20
Const KEY_ALL_ACCESS = ((STANDARD_RIGHTS_ALL Or KEY_QUERY_VALUE Or _
KEY_SET_VALUE Or KEY_CREATE_SUB_KEY Or KEY_ENUMERATE_SUB_KEYS Or _
KEY_NOTIFY Or KEY_CREATE_LINK) And (Not SYNCHRONIZE))
Private Sub Form_Load()
'~~> Key Path
strKeyBase = "SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer"
'~~> New Key that will be created
strKey = strKeyBase & "D" & "\DefaultIcon"
'~~> Value for DEFAULT (REG_SEZ)
strKeyValue = "C:\My Icons\D.ico"
End Sub
'~~> Creation Of Key
Private Sub Command1_Click()
'~~> Check if the specified key exists under HKEY_LOCAL_MACHINE
RegOpenKeyEx HKEY_LOCAL_MACHINE, strKey, 0, KEY_ALL_ACCESS, Result
'~~> If the key doesn't exist, we create it
If Result = 0 Then
'~~> Create a new key under HKEY_LOCAL_MACHINE
RegCreateKeyEx HKEY_LOCAL_MACHINE, strKey, 0, "REG_SEZ", _
REG_OPTION_NON_VOLATILE, KEY_ALL_ACCESS, ByVal 0&, Result, ret
If Result = 0 Then
MsgBox "Error while creating the Key!!"
Exit Sub
End If
'~~> Inform user that the key has been created
MsgBox "Key Created..."
End If
End Sub
'~~> Updation of REG_SEZ
Private Sub Command2_Click()
'~~> Note "(Default)" doesn't have a name so we use ""
regUpdateValue HKEY_LOCAL_MACHINE, strKey, "", strKeyValue
End Sub
Hope this helps...
Last edited by Siddharth Rout; Jun 17th, 2010 at 12:06 PM.
Reason: Colored the comments
A good exercise for the Heart is to bend down and help another up...
Please Mark your Thread "Resolved", if the query is solved
MyGear:
★ CPU ★ Ryzen 5 5800X
★ GPU ★ NVIDIA GeForce RTX 3080 TI Founder Edition
★ RAM ★ G. Skill Trident Z RGB 32GB 3600MHz
★ MB ★ ASUS TUF GAMING X570 (WI-FI) ATX Gaming
★ Storage ★ SSD SB-ROCKET-1TB + SEAGATE 2TB Barracuda IHD
★ Cooling ★ NOCTUA NH-D15 CHROMAX BLACK 140mm + 10 of Noctua NF-F12 PWM
★ PSU ★ ANTEC HCG-1000-EXTREME 1000 Watt 80 Plus Gold Fully Modular PSU
★ Case ★ LIAN LI PC-O11 DYNAMIC XL ROG (BLACK) (G99.O11DXL-X)
★ Monitor ★ LG Ultragear 27" 240Hz Gaming Monitor
★ Keyboard ★ TVS Electronics Gold Keyboard
★ Mouse ★ Logitech G502 Hero
Private Sub Command1_Click()
'~~> Check if the specified key exists under HKEY_LOCAL_MACHINE
RegOpenKeyEx HKEY_LOCAL_MACHINE, strKey, 0, KEY_ALL_ACCESS, Result
'~~> If the key doesn't exist, we create it
If Result = 0 Then
'~~> Create a new key under HKEY_LOCAL_MACHINE
RegCreateKeyEx HKEY_LOCAL_MACHINE, strKey, 0, "REG_SEZ", _
REG_OPTION_NON_VOLATILE, KEY_ALL_ACCESS, ByVal 0&, Result, ret
If Result = 0 Then
MsgBox "Error while creating the Key!!"
Exit Sub
End If
'~~> Inform user that the key has been created
MsgBox "Key Created..."
End If
End Sub
'~~> Updation of REG_SEZ
Private Sub Command2_Click()
'~~> Note "(Default)" doesn't have a name so we use ""
regUpdateValue HKEY_LOCAL_MACHINE, strKey, "", strKeyValue
End Sub
On what condition, the highlighted portion's variable RESULT will return value 0?
It shows the fine result. How do you expect this will solve my problem?? But the problem is at next step while creating the key. I got the error number 0 with no description.
1) I want to check if there are unwanted characters being passed.
2) Are you getting the message box MsgBox "Unknown Error occured..."
3) Also I am assuming that the key DriveIcons is already present...
Ya its present. I've done it to change my drive icons. But the previous code highlighted were supposed to check the existence of the key. But the error occurs at succeeding place.
Code:
Private Sub cmdChange_Click()
strKeyBase = "HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\DriveIcons\"
strKey = strKeyBase & txtDrive.Text & "\DefaultIcon"
strKeyValue = txtIconPath.Text
'~~> Check if the specified key exists under HKEY_LOCAL_MACHINE
RegOpenKeyEx HKEY_LOCAL_MACHINE, strKey, 0, KEY_ALL_ACCESS, Result
'~~> If the key doesn't exist, we create it
If Result = 0 Then
'~~> Create a new key under HKEY_LOCAL_MACHINE
RegCreateKeyEx HKEY_LOCAL_MACHINE, strKey, 0, "REG_SEZ", _
REG_OPTION_NON_VOLATILE, KEY_ALL_ACCESS, ByVal 0&, Result, Ret
If Result = 0 Then
' But the error occurs here.
MsgBox "Unknown Error occured while changing drive icon. " & Err.Number & Err.Description, vbCritical, "Drive Icon Changer"
Exit Sub
End If
End If
'~~> Value for DEFAULT (REG_SEZ)
strKeyValue = txtIconPath.Text
'~~> Note "(Default)" doesn't have a name so we use ""
regUpdateValue HKEY_LOCAL_MACHINE, strKey, "", strKeyValue
'~~> Inform user that the key has been created
MsgBox "Drive Icon has been changed successfully. Your system may need to be re-started to apply the changes.", vbInformation, "Drive Icon Changer"
End Sub
Last edited by rajbdilip; Jun 18th, 2010 at 10:24 AM.
huffff.... finally got it... many thanks to you..... now if I want to delete the same key??
Add this to the Declaration...
Code:
'~~> The RegDeleteKey function deletes the specified key
Private Declare Function RegDeleteKey Lib "advapi32.dll" Alias "RegDeleteKeyA" _
(ByVal hKey As Long, ByVal lpSubKey As String) As Long
Use this code to Delete it...
Code:
Private Sub Command2_Click()
'~~> Check if the specified key exists under HKEY_LOCAL_MACHINE
RegOpenKeyEx HKEY_LOCAL_MACHINE, strKey, 0, KEY_ALL_ACCESS, Result
'~~> If the key doesn't exist, inform user
If Result = 0 Then
MsgBox "Key Doesn't Exist"
Else
'~~> Delete the key
RegDeleteKey Result, ""
'~~> close the handle
RegCloseKey Result
MsgBox "Deleted"
End If
End Sub
Edit:
Since I have already worked on it, I will also upload a small sample based on your requirements
Last edited by Siddharth Rout; Jun 18th, 2010 at 11:11 AM.
A good exercise for the Heart is to bend down and help another up...
Please Mark your Thread "Resolved", if the query is solved
MyGear:
★ CPU ★ Ryzen 5 5800X
★ GPU ★ NVIDIA GeForce RTX 3080 TI Founder Edition
★ RAM ★ G. Skill Trident Z RGB 32GB 3600MHz
★ MB ★ ASUS TUF GAMING X570 (WI-FI) ATX Gaming
★ Storage ★ SSD SB-ROCKET-1TB + SEAGATE 2TB Barracuda IHD
★ Cooling ★ NOCTUA NH-D15 CHROMAX BLACK 140mm + 10 of Noctua NF-F12 PWM
★ PSU ★ ANTEC HCG-1000-EXTREME 1000 Watt 80 Plus Gold Fully Modular PSU
★ Case ★ LIAN LI PC-O11 DYNAMIC XL ROG (BLACK) (G99.O11DXL-X)
★ Monitor ★ LG Ultragear 27" 240Hz Gaming Monitor
★ Keyboard ★ TVS Electronics Gold Keyboard
★ Mouse ★ Logitech G502 Hero