Page 1 of 2 12 LastLast
Results 1 to 40 of 45

Thread: [RESOLVED] Creating, Updating and Deleting Registry Key

  1. #1

    Thread Starter
    Hyperactive Member rajbdilip's Avatar
    Join Date
    Feb 2010
    Location
    Kathmandu, Nepal
    Posts
    263

    Resolved [RESOLVED] Creating, Updating and Deleting Registry Key

    Hello.
    I want to create registry key inside HKEY_LOCAL_MACHINE\Software\Microsoft\WindowsNT\CurrentVersion\Explorer.
    Last edited by rajbdilip; Jun 18th, 2010 at 11:37 AM.
    A Young Self-Taught Programmer || VB6 | VB.NET (Visual Studio 2010) | Java |CSS | JavaScript | PHP | MySQL

  2. #2
    Discovering Life Siddharth Rout's Avatar
    Join Date
    Feb 2005
    Location
    Mumbai, India
    Posts
    12,001

    Re: Create Registry Key...

    Check the link in my signature
    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

  3. #3

    Thread Starter
    Hyperactive Member rajbdilip's Avatar
    Join Date
    Feb 2010
    Location
    Kathmandu, Nepal
    Posts
    263

    Re: Create Registry Key...

    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.
    A Young Self-Taught Programmer || VB6 | VB.NET (Visual Studio 2010) | Java |CSS | JavaScript | PHP | MySQL

  4. #4
    Discovering Life Siddharth Rout's Avatar
    Join Date
    Feb 2005
    Location
    Mumbai, India
    Posts
    12,001

    Re: Create Registry Key...

    Did you see post number 8? in that link.
    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

  5. #5

    Thread Starter
    Hyperactive Member rajbdilip's Avatar
    Join Date
    Feb 2010
    Location
    Kathmandu, Nepal
    Posts
    263

    Re: Create Registry Key...

    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\.
    A Young Self-Taught Programmer || VB6 | VB.NET (Visual Studio 2010) | Java |CSS | JavaScript | PHP | MySQL

  6. #6
    Discovering Life Siddharth Rout's Avatar
    Join Date
    Feb 2005
    Location
    Mumbai, India
    Posts
    12,001

    Re: Create Registry Key...

    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
    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

  7. #7

    Thread Starter
    Hyperactive Member rajbdilip's Avatar
    Join Date
    Feb 2010
    Location
    Kathmandu, Nepal
    Posts
    263

    Re: Create Registry Key...

    Alright. Creation of Sub Keys it done. Now, I want to set the value of the DEFAULT (REG_SEZ). How can I do this??
    Last edited by rajbdilip; Jun 17th, 2010 at 06:37 AM.
    A Young Self-Taught Programmer || VB6 | VB.NET (Visual Studio 2010) | Java |CSS | JavaScript | PHP | MySQL

  8. #8
    Discovering Life Siddharth Rout's Avatar
    Join Date
    Feb 2005
    Location
    Mumbai, India
    Posts
    12,001

    Re: Create Registry Key...

    Quote Originally Posted by rajbdilip View Post
    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

  9. #9

    Thread Starter
    Hyperactive Member rajbdilip's Avatar
    Join Date
    Feb 2010
    Location
    Kathmandu, Nepal
    Posts
    263

    Re: Create Registry Key...

    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
    Does this set the value?
    A Young Self-Taught Programmer || VB6 | VB.NET (Visual Studio 2010) | Java |CSS | JavaScript | PHP | MySQL

  10. #10
    Discovering Life Siddharth Rout's Avatar
    Join Date
    Feb 2005
    Location
    Mumbai, India
    Posts
    12,001

    Re: Create Registry Key...

    Yes it does
    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

  11. #11

    Thread Starter
    Hyperactive Member rajbdilip's Avatar
    Join Date
    Feb 2010
    Location
    Kathmandu, Nepal
    Posts
    263

    Re: Create Registry Key...

    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.
    A Young Self-Taught Programmer || VB6 | VB.NET (Visual Studio 2010) | Java |CSS | JavaScript | PHP | MySQL

  12. #12
    Discovering Life Siddharth Rout's Avatar
    Join Date
    Feb 2005
    Location
    Mumbai, India
    Posts
    12,001

    Re: Create Registry Key...

    Show me the code that you have tried...
    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

  13. #13

    Thread Starter
    Hyperactive Member rajbdilip's Avatar
    Join Date
    Feb 2010
    Location
    Kathmandu, Nepal
    Posts
    263

    Re: Create Registry Key...

    vb Code:
    1. Option Explicit
    2. '~~> The RegOpenKeyEx function opens the specified key.
    3. Private Declare Function RegOpenKeyEx Lib "advapi32.dll" Alias "RegOpenKeyExA" _
    4. (ByVal hKey As Long, ByVal lpSubKey As String, ByVal Reserved As Long, ByVal _
    5. samDesired As Long, phkResult As Long) As Long
    6.  
    7. '~~> The RegCreateKeyEx function creates the specified key. If the key already exists
    8. '~~> in the registry, the function opens it.
    9. Private Declare Function RegCreateKeyEx Lib "advapi32.dll" Alias "RegCreateKeyExA" _
    10. (ByVal hKey As Long, ByVal lpSubKey As String, ByVal Reserved As Long, ByVal _
    11. lpClass As String, ByVal dwOptions As Long, ByVal samDesired As Long, _
    12. lpSecurityAttributes As Any, phkResult As Long, lpdwDisposition As Long) As Long
    13.  
    14. Private Declare Function RegSetValueEx Lib "advapi32.dll" Alias "RegSetValueExA" _
    15. (ByVal lngRootKey As Long, ByVal lpValueName As String, ByVal Reserved As Long, _
    16. ByVal dwType As Long, lpData As Any, ByVal cbData As Long) As Long
    17.  
    18.  
    19. Private Declare Function RegCreateKey Lib "advapi32.dll" Alias "RegCreateKeyA" _
    20. (ByVal lngRootKey As Long, ByVal lpSubKey As String, phkResult As Long) As Long
    21.  
    22. Private Declare Function RegCloseKey Lib "advapi32.dll" (ByVal lngRootKey As Long) As Long
    23.  
    24. Dim strKeyBase As String, strKey As String, Result As Long
    25.  
    26. Const HKEY_LOCAL_MACHINE = &H80000002
    27. Const REG_OPTION_NON_VOLATILE = 0
    28. Const STANDARD_RIGHTS_ALL = &H1F0000
    29. Const KEY_QUERY_VALUE = &H1
    30. Const KEY_SET_VALUE = &H2
    31. Const KEY_CREATE_SUB_KEY = &H4
    32. Const KEY_ENUMERATE_SUB_KEYS = &H8
    33. Const KEY_NOTIFY = &H10
    34. Const SYNCHRONIZE = &H100000
    35. Const READ_CONTROL = &H20000
    36. Const STANDARD_RIGHTS_READ = (READ_CONTROL)
    37. Const STANDARD_RIGHTS_WRITE = (READ_CONTROL)
    38. Const KEY_CREATE_LINK = &H20
    39. Const KEY_ALL_ACCESS = ((STANDARD_RIGHTS_ALL Or KEY_QUERY_VALUE Or _
    40. KEY_SET_VALUE Or KEY_CREATE_SUB_KEY Or KEY_ENUMERATE_SUB_KEYS Or _
    41. KEY_NOTIFY Or KEY_CREATE_LINK) And (Not SYNCHRONIZE))
    42.  
    43. Public Sub regUpdateValue(ByVal lngRootKey As Long, ByVal strRegKeyPath As String, _
    44. ByVal strRegSubKey As String, varRegData As Variant)
    45.     Dim lngKeyHandle As Long, lngDataType As Long, lngKeyValue As Long, strKeyValue As String
    46.  
    47.     lngDataType = REG_SZ
    48.    
    49.     '~~> Querying the key path
    50.     lngRetVal = RegCreateKey(lngRootKey, strRegKeyPath, lngKeyHandle)
    51.    
    52.     '~~> Making it Null terminated
    53.     strKeyValue = Trim(varRegData) & Chr(0)
    54.    
    55.     '~~> Setting the value
    56.     lngRetVal = RegSetValueEx(lngKeyHandle, strRegSubKey, 0&, lngDataType, _
    57.                   ByVal strKeyValue, Len(strKeyValue))
    58.              
    59.     '~~> Closing the key
    60.     lngRetVal = RegCloseKey(lngKeyHandle)
    61.    
    62.     MsgBox "Key Value Set Succesfully    "
    63. End Sub
    64.  
    65. Private Sub cmdChange_Click()
    66.     Dim ret As Long
    67.    
    68.     strKey = strKeyBase & UCase$(txtDrive.Text) & "\DefaultIcon"
    69.    
    70.     RegCreateKeyEx HKEY_LOCAL_MACHINE, strKey, 0, "REG_DWORD", REG_OPTION_NON_VOLATILE, KEY_ALL_ACCESS, ByVal 0&, Result, ret
    71.        
    72.     Dim strKeyValue As String
    73.    
    74.     '~~> Value for DEFAULT (REG_SEZ)
    75.     strKeyValue = txtIconPath.Text
    76.    
    77.     '~~> Note "(Default)" doesn't have a name so we use ""
    78.     regUpdateValue HKEY_LOCAL_MACHINE, strKey, "", strKeyValue
    79.  
    80.        
    81. End Sub
    A Young Self-Taught Programmer || VB6 | VB.NET (Visual Studio 2010) | Java |CSS | JavaScript | PHP | MySQL

  14. #14
    Discovering Life Siddharth Rout's Avatar
    Join Date
    Feb 2005
    Location
    Mumbai, India
    Posts
    12,001

    Re: Create Registry Key...

    Ok, let me give you a sample...

    Give the values for

    strKeyBase
    txtDrive.Text
    txtIconPath.Text
    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

  15. #15

    Thread Starter
    Hyperactive Member rajbdilip's Avatar
    Join Date
    Feb 2010
    Location
    Kathmandu, Nepal
    Posts
    263

    Re: Create Registry Key...

    strKeyBase = "HEKY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer"
    txtDrive.Text="D"
    txtIconPath.Text="C:\My Icons\D.ico"
    A Young Self-Taught Programmer || VB6 | VB.NET (Visual Studio 2010) | Java |CSS | JavaScript | PHP | MySQL

  16. #16
    Discovering Life Siddharth Rout's Avatar
    Join Date
    Feb 2005
    Location
    Mumbai, India
    Posts
    12,001

    Re: Create Registry Key...

    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

    Code:
    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

  17. #17

    Thread Starter
    Hyperactive Member rajbdilip's Avatar
    Join Date
    Feb 2010
    Location
    Kathmandu, Nepal
    Posts
    263

    Re: Create Registry Key...

    The code I showed in #13 that you provided me, generates an error. Variable not defined for ret and REG_SEZ.
    A Young Self-Taught Programmer || VB6 | VB.NET (Visual Studio 2010) | Java |CSS | JavaScript | PHP | MySQL

  18. #18
    Discovering Life Siddharth Rout's Avatar
    Join Date
    Feb 2005
    Location
    Mumbai, India
    Posts
    12,001

    Re: Create Registry Key...

    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
    Last edited by Siddharth Rout; Jun 17th, 2010 at 12:04 PM.
    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

  19. #19

    Thread Starter
    Hyperactive Member rajbdilip's Avatar
    Join Date
    Feb 2010
    Location
    Kathmandu, Nepal
    Posts
    263

    Re: Create Registry Key...

    It still says variable ret has not been defined. Other seem to be working.
    A Young Self-Taught Programmer || VB6 | VB.NET (Visual Studio 2010) | Java |CSS | JavaScript | PHP | MySQL

  20. #20
    Discovering Life Siddharth Rout's Avatar
    Join Date
    Feb 2005
    Location
    Mumbai, India
    Posts
    12,001

    Re: Create Registry Key...

    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
    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

  21. #21

    Thread Starter
    Hyperactive Member rajbdilip's Avatar
    Join Date
    Feb 2010
    Location
    Kathmandu, Nepal
    Posts
    263

    Re: Create Registry Key...

    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
    On what condition, the highlighted portion's variable RESULT will return value 0?
    A Young Self-Taught Programmer || VB6 | VB.NET (Visual Studio 2010) | Java |CSS | JavaScript | PHP | MySQL

  22. #22
    Discovering Life Siddharth Rout's Avatar
    Join Date
    Feb 2005
    Location
    Mumbai, India
    Posts
    12,001

    Re: Create Registry Key...

    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 "\"
    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

  23. #23

    Thread Starter
    Hyperactive Member rajbdilip's Avatar
    Join Date
    Feb 2010
    Location
    Kathmandu, Nepal
    Posts
    263

    Re: Create Registry Key...

    Whats an error in this code then?

    vb Code:
    1. Private Sub cmdChange_Click()
    2.  
    3.     strKeyBase = "HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\DriveIcons"
    4.     strKey = strKeyBase & txtDrive.Text & "\DefaultIcon"
    5.     strKeyValue = txtIconPath.Text
    6.    
    7.     '~~> Check if the specified key exists under HKEY_LOCAL_MACHINE
    8.     RegOpenKeyEx HKEY_LOCAL_MACHINE, strKey, 0, KEY_ALL_ACCESS, Result
    9.    
    10.     '~~> If the key doesn't exist, we create it
    11.     If Result = 0 Then
    12.         '~~> Create a new key under HKEY_LOCAL_MACHINE
    13.         RegCreateKeyEx HKEY_LOCAL_MACHINE, strKey, 0, "REG_SEZ", _
    14.         REG_OPTION_NON_VOLATILE, KEY_ALL_ACCESS, ByVal 0&, Result, Ret
    15.        
    16.         If Result = 0 Then
    17.             MsgBox "Unknown Error occured while changing drive icon.", vbCritical, "Drive Icon Changer"
    18.             Exit Sub
    19.         End If
    20.        
    21.     End If
    22.    
    23.         '~~> Value for DEFAULT (REG_SEZ)
    24.     strKeyValue = txtIconPath.Text
    25.    
    26.     '~~> Note "(Default)" doesn't have a name so we use ""
    27.     regUpdateValue HKEY_LOCAL_MACHINE, strKey, "", strKeyValue
    28.  
    29.        
    30.         '~~> Inform user that the key has been created
    31.      MsgBox "Drive Icon has been changed successfully. Your system may need to be re-started to apply the changes.", vbInformation, "Drive Icon Changer"
    32.  
    33.  
    34.        
    35. End Sub

    Can I create multiple subkeys at once??
    A Young Self-Taught Programmer || VB6 | VB.NET (Visual Studio 2010) | Java |CSS | JavaScript | PHP | MySQL

  24. #24
    Discovering Life Siddharth Rout's Avatar
    Join Date
    Feb 2005
    Location
    Mumbai, India
    Posts
    12,001

    Re: Create Registry Key...

    The error lies here I guess...

    strKey = strKeyBase & txtDrive.Text & "\DefaultIcon"
    strKeyValue = txtIconPath.Text
    Insert this piece of line

    Msgbox strKey & ", " & strKeyValue

    before

    RegOpenKeyEx HKEY_LOCAL_MACHINE, strKey, 0, KEY_ALL_ACCESS, Result

    in the above code...

    What do you get?
    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

  25. #25

    Thread Starter
    Hyperactive Member rajbdilip's Avatar
    Join Date
    Feb 2010
    Location
    Kathmandu, Nepal
    Posts
    263

    Re: Create Registry Key...

    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??
    A Young Self-Taught Programmer || VB6 | VB.NET (Visual Studio 2010) | Java |CSS | JavaScript | PHP | MySQL

  26. #26
    Discovering Life Siddharth Rout's Avatar
    Join Date
    Feb 2005
    Location
    Mumbai, India
    Posts
    12,001

    Re: Create Registry Key...

    Quote Originally Posted by rajbdilip View Post
    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??
    Did you try what I suggested in my last post?
    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

  27. #27

    Thread Starter
    Hyperactive Member rajbdilip's Avatar
    Join Date
    Feb 2010
    Location
    Kathmandu, Nepal
    Posts
    263

    Re: Create Registry Key...

    Yeah, I tried... it seems to be OK.
    A Young Self-Taught Programmer || VB6 | VB.NET (Visual Studio 2010) | Java |CSS | JavaScript | PHP | MySQL

  28. #28
    Discovering Life Siddharth Rout's Avatar
    Join Date
    Feb 2005
    Location
    Mumbai, India
    Posts
    12,001

    Re: Create Registry Key...

    upload your project and let me test it for you...
    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

  29. #29

    Thread Starter
    Hyperactive Member rajbdilip's Avatar
    Join Date
    Feb 2010
    Location
    Kathmandu, Nepal
    Posts
    263

    Re: Create Registry Key...

    Here is my project file....
    Attached Files Attached Files
    A Young Self-Taught Programmer || VB6 | VB.NET (Visual Studio 2010) | Java |CSS | JavaScript | PHP | MySQL

  30. #30
    Discovering Life Siddharth Rout's Avatar
    Join Date
    Feb 2005
    Location
    Mumbai, India
    Posts
    12,001

    Re: Create Registry Key...

    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
    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

  31. #31

    Thread Starter
    Hyperactive Member rajbdilip's Avatar
    Join Date
    Feb 2010
    Location
    Kathmandu, Nepal
    Posts
    263

    Re: Create Registry Key...

    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.
    A Young Self-Taught Programmer || VB6 | VB.NET (Visual Studio 2010) | Java |CSS | JavaScript | PHP | MySQL

  32. #32
    Discovering Life Siddharth Rout's Avatar
    Join Date
    Feb 2005
    Location
    Mumbai, India
    Posts
    12,001

    Re: Create Registry Key...

    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...

    "HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\DriveIcons\"
    Last edited by Siddharth Rout; Jun 18th, 2010 at 10:14 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

  33. #33

    Thread Starter
    Hyperactive Member rajbdilip's Avatar
    Join Date
    Feb 2010
    Location
    Kathmandu, Nepal
    Posts
    263

    Re: Create Registry Key...

    There are no unwanted character passed but I'm getting the MsgBox you specified.
    A Young Self-Taught Programmer || VB6 | VB.NET (Visual Studio 2010) | Java |CSS | JavaScript | PHP | MySQL

  34. #34
    Discovering Life Siddharth Rout's Avatar
    Join Date
    Feb 2005
    Location
    Mumbai, India
    Posts
    12,001

    Re: Create Registry Key...

    Oh I didn't refresh the page before editing the above post... See point 3 in my last post...
    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

  35. #35

    Thread Starter
    Hyperactive Member rajbdilip's Avatar
    Join Date
    Feb 2010
    Location
    Kathmandu, Nepal
    Posts
    263

    Re: Create Registry Key...

    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.
    A Young Self-Taught Programmer || VB6 | VB.NET (Visual Studio 2010) | Java |CSS | JavaScript | PHP | MySQL

  36. #36
    Discovering Life Siddharth Rout's Avatar
    Join Date
    Feb 2005
    Location
    Mumbai, India
    Posts
    12,001

    Re: Create Registry Key...

    Ok, I am uploading a small Sample in another few minutes... Run it and let me know if it works for you...
    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

  37. #37

    Thread Starter
    Hyperactive Member rajbdilip's Avatar
    Join Date
    Feb 2010
    Location
    Kathmandu, Nepal
    Posts
    263

    Re: Create Registry Key...

    fine..
    A Young Self-Taught Programmer || VB6 | VB.NET (Visual Studio 2010) | Java |CSS | JavaScript | PHP | MySQL

  38. #38
    Discovering Life Siddharth Rout's Avatar
    Join Date
    Feb 2005
    Location
    Mumbai, India
    Posts
    12,001

    Re: Create Registry Key...

    Ha ha ha just realized this....

    You added something which was not there in the code See post 16 once again...

    Change

    strKeyBase = "HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\DriveIcons\"
    to

    Code:
    strKeyBase = "SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\DriveIcons\"
    Now try it...
    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

  39. #39

    Thread Starter
    Hyperactive Member rajbdilip's Avatar
    Join Date
    Feb 2010
    Location
    Kathmandu, Nepal
    Posts
    263

    Re: Create Registry Key...

    huffff.... finally got it... many thanks to you..... now if I want to delete the same key??
    A Young Self-Taught Programmer || VB6 | VB.NET (Visual Studio 2010) | Java |CSS | JavaScript | PHP | MySQL

  40. #40
    Discovering Life Siddharth Rout's Avatar
    Join Date
    Feb 2005
    Location
    Mumbai, India
    Posts
    12,001

    Re: Create Registry Key...

    Quote Originally Posted by rajbdilip View Post
    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
    Attached Files Attached Files
    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

Page 1 of 2 12 LastLast

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width