Hello.
I want to create registry key inside HKEY_LOCAL_MACHINE\Software\Microsoft\WindowsNT\CurrentVersion\Explorer.
Printable View
Hello.
I want to create registry key inside HKEY_LOCAL_MACHINE\Software\Microsoft\WindowsNT\CurrentVersion\Explorer.
Check the link in my signature :)
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.
Did you see post number 8? in that link.
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\.
This works for me...
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, 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
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...
And then use this in the form...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
Hope this helps...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
In the previous post, I found the following API Declaration:
Does this set the value?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
Yes it does :)
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.
Show me the code that you have tried...
vb Code:
Option Explicit '~~> 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 Private 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 Private Declare Function RegCreateKey Lib "advapi32.dll" Alias "RegCreateKeyA" _ (ByVal lngRootKey As Long, ByVal lpSubKey As String, phkResult As Long) As Long Private Declare Function RegCloseKey Lib "advapi32.dll" (ByVal lngRootKey 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)) 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) MsgBox "Key Value Set Succesfully " End Sub Private Sub cmdChange_Click() Dim ret As Long strKey = strKeyBase & UCase$(txtDrive.Text) & "\DefaultIcon" RegCreateKeyEx HKEY_LOCAL_MACHINE, strKey, 0, "REG_DWORD", REG_OPTION_NON_VOLATILE, KEY_ALL_ACCESS, ByVal 0&, Result, ret Dim strKeyValue As String '~~> Value for DEFAULT (REG_SEZ) strKeyValue = txtIconPath.Text '~~> Note "(Default)" doesn't have a name so we use "" regUpdateValue HKEY_LOCAL_MACHINE, strKey, "", strKeyValue End Sub
Ok, let me give you a sample...
Give the values for
strKeyBase
txtDrive.Text
txtIconPath.Text
strKeyBase = "HEKY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer"
txtDrive.Text="D"
txtIconPath.Text="C:\My Icons\D.ico"
Fair enough...
I hope you understand that it will create a key
strKey = strKeyBase & "D" & "\DefaultIcon"
Which is
"HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\ExplorerD\DefaultIcon"
If this is correct then paste this in a module
and this in the form code areaCode: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
Hope this helps...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
The code I showed in #13 that you provided me, generates an error. Variable not defined for ret and REG_SEZ.
I have just updated the code in Post 16
If you want you can move the code form 2nd command button to the end of the 1st command button :) I have kept it separately so that you can understand it :)
It still says variable ret has not been defined. Other seem to be working.
Oh I missed that
Replace
Dim strKeyBase As String, strKey As String, strKeyValue As String, Result As Long
with
Dim strKeyBase As String, strKey As String, strKeyValue As String, Result As Long, Ret as long
On what condition, the highlighted portion's variable RESULT will return value 0?Code: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
When you include characters like "\" in the key name
for example
you can have a key with it's text "DefaultIcon" but not "Default\Icon"
Try this
Create a new key by right clicking in the registry and manually typing "\" :)
Whats an error in this code then?
vb 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 MsgBox "Unknown Error occured while changing drive icon.", 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
Can I create multiple subkeys at once??
The error lies here I guess...
Insert this piece of lineQuote:
strKey = strKeyBase & txtDrive.Text & "\DefaultIcon"
strKeyValue = txtIconPath.Text
Msgbox strKey & ", " & strKeyValue
before
RegOpenKeyEx HKEY_LOCAL_MACHINE, strKey, 0, KEY_ALL_ACCESS, Result
in the above code...
What do you get?
I corrected this line:
strKeyBase = "HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\DriveIcons"
to:
strKeyBase = "HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\DriveIcons\"
.
Other seems fine.
Now what's the problem??
Yeah, I tried... it seems to be OK.
upload your project and let me test it for you...
Here is my project file....
It is asking me to install the ocx...
Ok do this for me...
In your cmdChange click event add these 4 lines in red. Then run your program. It will create a sample.txt in c:\
Upload that...
Code:Private Sub cmdChange_Click()
strKeyBase = "HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\DriveIcons\"
strKey = strKeyBase & txtDrive.Text & "\DefaultIcon"
strKeyValue = txtIconPath.Text
Open "c:\sample.txt" For Output As #1
Print #1, strKey
Print #1, strKeyValue
Close #1
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...
Quote:
"HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\DriveIcons\"
There are no unwanted character passed but I'm getting the MsgBox you specified.
Oh I didn't refresh the page before editing the above post... See point 3 in my last post...
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
Ok, I am uploading a small Sample in another few minutes... Run it and let me know if it works for you...
fine..
Ha ha ha just realized this....
You added something which was not there in the code ;) See post 16 once again...
Change
toQuote:
strKeyBase = "HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\DriveIcons\"
Now try it...Code:strKeyBase = "SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\DriveIcons\"
huffff.... finally got it... many thanks to you..... now if I want to delete the same key??
Add this to the Declaration...
Use this code to Delete it...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
Edit: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
Since I have already worked on it, I will also upload a small sample based on your requirements :)