Results 1 to 3 of 3

Thread: registry problem

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    May 2001
    Posts
    237

    registry problem

    If I use the savesetting and getsetting command, VB saves the registry in HKEY_CURRENT_USER --> Software --> VB and VBA program setting.

    What should I do if I want to put the registry entries NOT in that directory?

    thx...

  2. #2
    Frenzied Member zuperman's Avatar
    Join Date
    Dec 2000
    Location
    Portugal
    Posts
    1,033

    this is a good code to create and delete keys and values in the registry...

    Code:
    Option Explicit
    
    Private Declare Function RegOpenKeyEx Lib "advapi32.dll" Alias "RegOpenKeyExA" (ByVal hKey As Long, ByVal lpSubKey As String, ByVal ulOptions As Long, ByVal samDesired As Long, phkResult As Long) As Long
    Private Declare Function RegCloseKey Lib "advapi32.dll" (ByVal hKey As Long) As Long
    Private Const ERROR_SUCCESS = 0&
    '****************************************************
    'Indented because it belongs to the last constant of this batch - it must come before the last one
      Private Const SYNCHRONIZE = &H100000
      Private Const KEY_CREATE_SUB_KEY = &H4
      Private Const KEY_SET_VALUE = &H2
        Private Const READ_CONTROL = &H20000
      Private Const STANDARD_RIGHTS_WRITE = (READ_CONTROL)
    Private Const KEY_WRITE = ((STANDARD_RIGHTS_WRITE Or KEY_SET_VALUE Or KEY_CREATE_SUB_KEY) And (Not SYNCHRONIZE))
      
    'Indented because it belongs to the last constant of this batch - it must come before the last one
      'Private Const SYNCHRONIZE = &H100000
      Private Const KEY_CREATE_LINK = &H20
      Private Const KEY_NOTIFY = &H10
      Private Const KEY_ENUMERATE_SUB_KEYS = &H8
      'Private Const KEY_CREATE_SUB_KEY = &H4
      'Private Const KEY_SET_VALUE = &H2
      Private Const KEY_QUERY_VALUE = &H1
      Private Const STANDARD_RIGHTS_ALL = &H1F0000
    Private 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 Const HKEY_CLASSES_ROOT = &H80000000
    Private Const HKEY_CURRENT_CONFIG = &H80000005
    Private Const HKEY_CURRENT_USER = &H80000001
    Private Const HKEY_DYN_DATA = &H80000006
    Private Const HKEY_LOCAL_MACHINE = &H80000002
    Private Const HKEY_PERFORMANCE_DATA = &H80000004
    Private Const HKEY_USERS = &H80000003
    '****************************************************
    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 SECURITY_ATTRIBUTES, phkResult As Long, lpdwDisposition As Long) As Long
    Private Const REG_OPTION_NON_VOLATILE = 0
    Private Type SECURITY_ATTRIBUTES
        nLength As Long
        lpSecurityDescriptor As Long
        bInheritHandle As Long
    End Type
    
    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 Const REG_SZ = 1
    
    Private Declare Function RegQueryValueEx Lib "advapi32.dll" Alias "RegQueryValueExA" (ByVal hKey As Long, ByVal lpValueName As String, ByVal lpReserved As Long, lpType As Long, lpData As Any, lpcbData 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 RegDeleteKey Lib "advapi32.dll" Alias "RegDeleteKeyA" (ByVal hKey As Long, ByVal lpSubKey As String) As Long
    '****************************************************
    
    
    Dim str_KeyFolder As String
    Dim str_Value_ProgramName As String
    Dim str_Data_ProgramPath As String
    Dim lng_Handle_To_KeyFolder As Long  ' handle to the open or new registry key
    Dim lng_Return As Long
    
    Sub Main()
      'CONCEPTS:  KEY   - Folders in Window Explorers
      '           VALUE - Files
      '           DATA  - Path
    
      'PITFALL: Make sure there is no "\" at the end
      
      str_KeyFolder = "SOFTWARE\Microsoft\Windows\CurrentVersion\Testing"
      'str_KeyFolder = "SOFTWARE\Microsoft\Windows\CurrentVersion\Run"          'NOTICE: Uncomment this line will make program start everytime log on
      str_Value_ProgramName = "Project1"
      str_Data_ProgramPath = "C:\Test.exe"
    
        
      'Open RegEdit.Exe while step through this code - Refresh(F5) Regedit Screen after each procedure
      
      'OVERALL: Create a Key - Folder Column in RegEdit.Exe
      Call p_Create_Key
      
      'OVERALL: Create a Value for a registry Key - Name Column in RegEdit.Exe
      Call p_Create_Value(str_Value_ProgramName, str_Data_ProgramPath)
      
      'OVERALL: Read Data or File Path from a Value - Data Column in RegEdit.Exe
      Call p_Read_Data_From_Value(str_Value_ProgramName)
    
      'OVERALL: Delete a Value from a registry Key
      Call p_Delete_Value(str_Value_ProgramName)
      
      'OVERALL: Delete a Key and all of its Value
      Call p_Delete_Key
    End Sub
    
    Public Sub p_Create_Key()
        'PURPOSE: Set the name of the new key and the default security settings
        Dim sec_Attri As SECURITY_ATTRIBUTES  ' security settings of the key
        sec_Attri.nLength = Len(sec_Attri)    ' size of the structure
        sec_Attri.lpSecurityDescriptor = 0    ' default security level
        sec_Attri.bInheritHandle = True       ' the default value for this setting
        
        Dim lng_New_Or_Exist As Long          ' "1" if new key - "2" if existing key was opened
        
        'PURPOSE: Create or open the registry key and assign the handle to lng_Handle_To_KeyFolder
        lng_Return = RegCreateKeyEx(HKEY_LOCAL_MACHINE, str_KeyFolder, 0, vbNullString, _
                                    REG_OPTION_NON_VOLATILE, KEY_WRITE, sec_Attri, _
                                    lng_Handle_To_KeyFolder, lng_New_Or_Exist)
        
        If lng_Return <> ERROR_SUCCESS Then
          MsgBox "Error in p_Create_Key procedure!"
          End
        End If
        
        'PURPOSE: Close the registry key if it was successfully opened
        Call RegCloseKey(lng_Handle_To_KeyFolder)
    End Sub
    
    Public Sub p_Create_Value(str_Value_ProgramName As String, str_Data_ProgramPath As String)
        'PURPOSE: Open the registry key (str_KeyFolder) and _
                  assign the handle to lng_Handle_To_KeyFolder
        lng_Return = RegOpenKeyEx(HKEY_LOCAL_MACHINE, str_KeyFolder, 0, _
                                  KEY_ALL_ACCESS, lng_Handle_To_KeyFolder)
        
        If lng_Return = ERROR_SUCCESS Then
          'PURPOSE: Add the desired value to the key.
          Call RegSetValueEx(lng_Handle_To_KeyFolder, str_Value_ProgramName, _
                             0, REG_SZ, ByVal str_Data_ProgramPath, Len(str_Data_ProgramPath))
          
          'PURPOSE: Close the registry key if it was successfully opened
          Call RegCloseKey(lng_Handle_To_KeyFolder)
        End If
    End Sub
    
    Public Sub p_Read_Data_From_Value(str_Value_ProgramName As String)
        'PURPOSE: Open the registry key (str_KeyFolder) and _
                  assign the handle to lng_Handle_To_KeyFolder
        lng_Return = RegOpenKeyEx(HKEY_LOCAL_MACHINE, str_KeyFolder, 0, _
                                  KEY_ALL_ACCESS, lng_Handle_To_KeyFolder)
        
        If lng_Return = ERROR_SUCCESS Then
          'PURPOSE: Read data
          Dim str_Data As String
          str_Data = Space(50)
          Call RegQueryValueEx(lng_Handle_To_KeyFolder, str_Value_ProgramName, _
                               0, REG_SZ, ByVal str_Data, Len(str_Data))
    
          str_Data = Left(str_Data, InStr(1, str_Data, Chr(0)) - 1)
          MsgBox str_Data
    
          'PURPOSE: Close the registry key if it was successfully opened
          Call RegCloseKey(lng_Handle_To_KeyFolder)
        End If
    End Sub
    
    Public Sub p_Delete_Value(str_Value_ProgramName As String)
        'PURPOSE: Open the registry key (str_KeyFolder) and _
                  assign the handle to lng_Handle_To_KeyFolder
        lng_Return = RegOpenKeyEx(HKEY_LOCAL_MACHINE, str_KeyFolder, 0, _
                                  KEY_ALL_ACCESS, lng_Handle_To_KeyFolder)
        
        If lng_Return = ERROR_SUCCESS Then
          'PURPOSE: Delete the desired value from the key.
          Call RegDeleteValue(lng_Handle_To_KeyFolder, str_Value_ProgramName)
          
          'PURPOSE: Close the registry key if it was successfully opened
          Call RegCloseKey(lng_Handle_To_KeyFolder)
        End If
    End Sub
    
    Public Sub p_Delete_Key()
        lng_Return = RegDeleteKey(HKEY_LOCAL_MACHINE, str_KeyFolder)
        
        If lng_Return <> ERROR_SUCCESS Then
          MsgBox "Error in p_Delete_Key procedure!"
        End If
    End Sub
    hope this helps...

    Regards
    Help keep this forum clean: Remember to mark your thread as resolved · Search before you post · Remember to rate posts that help

    VS2010: Visual Studio 2010 Keybinding Posters
    · Service Pack 1
    Tools: GhostDoc - automatically generates XML documentation comments
    · NuGet package Manager · PowerCommands IDE extensions
    Source Control: ankhsvn - integration for SVN
    · Windows Shell Extension for Subversion

    Development Laptop: Intel Core i5 430M 2.26 GHz @ 2.53 GHz
    · 4096 MB, DDR3 PC3-8500F (533 MHz), Kingston · ATI Mobility Radeon HD 5470 · 15.6 @ 16:9, 1366x768 pixel, HD LED LCD

    I follow:
    JoelOnSoftware - A weblog by Joel Spolsky, a programmer working in New York City, about software and software companies
    ScottGu's Blog - Scott Guthrie works for Microsoft as the Product Manager of the .NET Framework
    Portugal-a-Programar - Portuguese Developers Community
    .NET Rocks! - is a weekly Internet audio talk show for .NET Developers.

    Programming Languages:
    C#
    · VB.NET · JAVA · PHP · Javascript
    Other:
    XML
    · HTML · CSS · JQuery · SQL



    *** Proudly Portuguese ***

  3. #3
    Frenzied Member zuperman's Avatar
    Join Date
    Dec 2000
    Location
    Portugal
    Posts
    1,033
    i recommend to focus on the Main Sub

    Code:
    Sub Main()
      'CONCEPTS:  KEY   - Folders in Window Explorers
      '           VALUE - Files
      '           DATA  - Path
    
      'PITFALL: Make sure there is no "\" at the end
      
      str_KeyFolder = "SOFTWARE\Microsoft\Windows\CurrentVersion\Testing"
      'str_KeyFolder = "SOFTWARE\Microsoft\Windows\CurrentVersion\Run"          'NOTICE: Uncomment this line will make program start everytime log on
      str_Value_ProgramName = "Project1"
      str_Data_ProgramPath = "C:\Test.exe"
    
        
      'Open RegEdit.Exe while step through this code - Refresh(F5) Regedit Screen after each procedure
      
      'OVERALL: Create a Key - Folder Column in RegEdit.Exe
      Call p_Create_Key
      
      'OVERALL: Create a Value for a registry Key - Name Column in RegEdit.Exe
      Call p_Create_Value(str_Value_ProgramName, str_Data_ProgramPath)
      
      'OVERALL: Read Data or File Path from a Value - Data Column in RegEdit.Exe
      Call p_Read_Data_From_Value(str_Value_ProgramName)
    
      'OVERALL: Delete a Value from a registry Key
      Call p_Delete_Value(str_Value_ProgramName)
      
      'OVERALL: Delete a Key and all of its Value
      Call p_Delete_Key
    End Sub
    Help keep this forum clean: Remember to mark your thread as resolved · Search before you post · Remember to rate posts that help

    VS2010: Visual Studio 2010 Keybinding Posters
    · Service Pack 1
    Tools: GhostDoc - automatically generates XML documentation comments
    · NuGet package Manager · PowerCommands IDE extensions
    Source Control: ankhsvn - integration for SVN
    · Windows Shell Extension for Subversion

    Development Laptop: Intel Core i5 430M 2.26 GHz @ 2.53 GHz
    · 4096 MB, DDR3 PC3-8500F (533 MHz), Kingston · ATI Mobility Radeon HD 5470 · 15.6 @ 16:9, 1366x768 pixel, HD LED LCD

    I follow:
    JoelOnSoftware - A weblog by Joel Spolsky, a programmer working in New York City, about software and software companies
    ScottGu's Blog - Scott Guthrie works for Microsoft as the Product Manager of the .NET Framework
    Portugal-a-Programar - Portuguese Developers Community
    .NET Rocks! - is a weekly Internet audio talk show for .NET Developers.

    Programming Languages:
    C#
    · VB.NET · JAVA · PHP · Javascript
    Other:
    XML
    · HTML · CSS · JQuery · SQL



    *** Proudly Portuguese ***

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