Results 1 to 5 of 5

Thread: reg

  1. #1
    Guest

    Wink

    hey, i cant seem to get all the reg stuff i find on the internet to work, i
    was wondering if you could make an api for me;
    a text box to enter the decimal number, a command button to wright the
    number entered in the text box to a dword value, the place of the dword
    value to edit is (HKEY_CURRENT_USER\Software\Microsoft\Microsoft Games\Age
    of Empires II: The Conquerors Expansion\1.0) the name of the value is (Game
    Speed), so all i have to do is type 20 in the text box and click on the
    button and the decimal value of 20 is enterd to the dwordvalue named Game
    Speed( witch alreday exists)

  2. #2
    Fanatic Member Jerry Grant's Avatar
    Join Date
    Jul 2000
    Location
    Dorset, UK
    Posts
    810
    The following code allows access to all areas of the registry, both for strings and DWords. This looks like overkill, but the registry is a funny thing if you have ever tried to access it in NT!

    Copy the following to a BAS file and call the P_SetKeyValue function to send the data to your key:

    Code:
        Global Const REG_SZ As Long = 1
        Global Const REG_DWORD As Long = 4
        
         '// These are the four main sections of the registy                 //
        Global Const HKEY_CLASSES_ROOT = &H80000000
        Global Const HKEY_CURRENT_USER = &H80000001
        Global Const HKEY_LOCAL_MACHINE = &H80000002
        Global Const HKEY_USERS = &H80000003
        
        Global Const ERROR_NONE = 0
        Global Const ERROR_BADDB = 1
        Global Const ERROR_BADKEY = 2
        Global Const ERROR_CANTOPEN = 3
        Global Const ERROR_CANTREAD = 4
        Global Const ERROR_CANTWRITE = 5
        Global Const ERROR_OUTOFMEMORY = 6
        Global Const ERROR_INVALID_PARAMETER = 7
        Global Const ERROR_ACCESS_DENIED = 8
        Global Const ERROR_INVALID_PARAMETERS = 87
        Global Const ERROR_NO_MORE_ITEMS = 259
        
        Global Const KEY_QUERY_VALUE = &H1
        Global Const KEY_ENUMERATE_SUB_KEYS = &H8
        Global Const KEY_NOTIFY = &H10
        Global Const SYNCHRONIZE = &H100000
        Global Const READ_CONTROL = &H20000
        Global Const STANDARD_RIGHTS_READ = (READ_CONTROL)
            
        Global Const KEY_READ = ((STANDARD_RIGHTS_READ Or KEY_QUERY_VALUE Or KEY_ENUMERATE_SUB_KEYS Or KEY_NOTIFY) And (Not SYNCHRONIZE))
        Global Const KEY_ALL_ACCESS = &H3F
        
        Global Const REG_OPTION_NON_VOLATILE = 0
        
        Declare Function RegCloseKey Lib "advapi32.dll" _
                    (ByVal hKey As Long) As Long
                    
        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, _
                                       ByVal lpSecurityAttributes As Long, _
                                       phkResult As Long, _
                                       lpdwDisposition As Long) As Long
                                       
        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
                                     
        Declare Function RegQueryValueExString Lib "advapi32.dll" Alias _
                    "RegQueryValueExA" (ByVal hKey As Long, _
                                        ByVal lpValueName As String, _
                                        ByVal lpReserved As Long, _
                                        lpType As Long, _
                                        ByVal lpData As String, _
                                        lpcbData As Long) As Long
                                        
        Declare Function RegQueryValueExLong Lib "advapi32.dll" Alias _
                    "RegQueryValueExA" (ByVal hKey As Long, _
                                        ByVal lpValueName As String, _
                                        ByVal lpReserved As Long, _
                                        lpType As Long, lpData As Long, _
                                        lpcbData As Long) As Long
                                        
        Declare Function RegQueryValueExNULL Lib "advapi32.dll" Alias _
                    "RegQueryValueExA" (ByVal hKey As Long, _
                                        ByVal lpValueName As String, _
                                        ByVal lpReserved As Long, _
                                        lpType As Long, _
                                        ByVal lpData As Long, _
                                        lpcbData As Long) As Long
                                        
        Declare Function RegSetValueExString Lib "advapi32.dll" Alias _
                    "RegSetValueExA" (ByVal hKey As Long, _
                                      ByVal lpValueName As String, _
                                      ByVal Reserved As Long, _
                                      ByVal dwType As Long, _
                                      ByVal lpValue As String, _
                                      ByVal cbData As Long) As Long
                                      
        Declare Function RegSetValueExLong Lib "advapi32.dll" Alias _
                    "RegSetValueExA" (ByVal hKey As Long, _
                                      ByVal lpValueName As String, _
                                      ByVal Reserved As Long, _
                                      ByVal dwType As Long, _
                                      lpValue As Long, _
                                      ByVal cbData As Long) As Long
        
    
    Function F_QueryKeyValue( _
                         ByVal hKey As Long, _
                         ByVal lpszSubKey As String, _
                         ByVal szValueName As String, _
                         ByRef vValue As Variant _
                         ) As Long
                         
         Dim cch As Long
         Dim lrc As Long
         Dim lType As Long
         Dim lValue As Long
         Dim sValue As String
         Dim lhKey As Long         'handle of opened key
     
         On Error GoTo F_QueryKeyValue_Error
     
         lrc = RegOpenKeyEx(hKey, _
                            lpszSubKey, _
                            0, _
                            KEY_READ, _
                            lhKey)
                       
         If lrc <> ERROR_NONE Then Error 5
     
         '// Determine the size and type of data to be read
         lrc = RegQueryValueExNULL(lhKey, _
                                   szValueName, _
                                   0&, _
                                   lType, _
                                   0&, _
                                   cch)
    
         If lrc <> ERROR_NONE Then Error 5
    
         Select Case lType
             '// For strings
             Case REG_SZ:
                 sValue = String(cch, 0)
                 lrc = RegQueryValueExString(lhKey, _
                                             szValueName, _
                                             0&, _
                                             lType, _
                                             sValue, _
                                             cch)
                If lrc = ERROR_NONE Then
                    vValue = Left$(sValue, cch - 1)
                Else
                    vValue = Empty
                End If
             '// For DWORDS
             Case REG_DWORD:
                 lrc = RegQueryValueExLong(lhKey, _
                                           szValueName, _
                                           0&, _
                                           lType, _
                                           lValue, _
                                           cch)
                If lrc = ERROR_NONE Then vValue = lValue
             Case Else
                '// All other data types not supported
                lrc = -1
        End Select
    
    F_QueryKeyValue_Exit:
        F_QueryKeyValue = lrc
        RegCloseKey (hKey)
    
        Exit Function
    F_QueryKeyValue_Error:
        Resume F_QueryKeyValue_Exit
    End Function
    
    Public Sub P_SetKeyValue( _
                            ByVal hKey As Long, _
                            sKeyName As String, _
                            sValueName As String, _
                            vValueSetting As Variant, _
                            lValueType As Long _
                            )
        
        Dim lValue As Long
        Dim sValue As String
        Dim lRetVal As Long         'result of the SetValueEx function
        Dim lhKey As Long         'handle of opened key
    
        lRetVal = RegOpenKeyEx(hKey, _
                               sKeyName, _
                               0, _
                               KEY_ALL_ACCESS, _
                               lhKey)
        Select Case lValueType
            Case REG_SZ
                sValue = vValueSetting & Chr$(0)
                lRetVal = RegSetValueExString(lhKey, _
                                              sValueName, _
                                              0&, _
                                              lValueType, _
                                              sValue, _
                                              Len(sValue))
            Case REG_DWORD
                lValue = vValueSetting
                lRetVal = RegSetValueExLong(lhKey, _
                                            sValueName, _
                                            0&, _
                                            lValueType, _
                                            lValue, _
                                            4)
        End Select
        RegCloseKey (lhKey)
            
    End Sub
    To call use the following


    Code:
    Private Sub Command1_Click()
    
    Call P_SetKeyValue( _
                       HKEY_CURRENT_USER, _
                       "Software\Microsoft\Microsoft Games\Age of Empires II: The Conquerors Expansion\1.0", _
                       "Game Speed", _
                       20, _
                       REG_DWORD _
                       ) 
    
    End Sub
    Hope this helps
    Jerry Grant................tnarG yrreJ
    Website: <JG-Design></.net>
    Email: [email protected]
    Working towards a bug free world......
    (Not a Microsoft employee)

  3. #3
    Guest

    Lightbulb it works.. but

    Call P_SetKeyValue( _
    HKEY_CURRENT_USER, _
    "Software\Microsoft\Microsoft Games\Age of Empires II: The Conquerors Expansion\1.0", _
    "Game Speed", _
    20, _
    REG_DWORD _
    )

    it works great but the number 20 entered is fixed,
    is there any way i can change it in the program after it is compiled?
    like a text box to change the fxixed number 20 to sumthing i type in the text box?

  4. #4
    Fanatic Member Jerry Grant's Avatar
    Join Date
    Jul 2000
    Location
    Dorset, UK
    Posts
    810
    I assumed this was obvious!

    Create a Sub routine of Function, thus:
    Code:
    Public Sub SetMyValue(ByVal lngNum as Long)
    
    Call P_SetKeyValue( _
                       HKEY_CURRENT_USER, _
                       "Software\Microsoft\Microsoft Games\Age of Empires II: The Conquerors Expansion\1.0", _
                       "Game Speed", _
                       lngNum, _
                       REG_DWORD _
                       ) 
    
    End Sub
    Call by

    Code:
    If IsNumeric(Text1.Text) Then
        Call SetMyValue(Text1.Text)
    Else
        MsgBox "Enter a valid number!"
    End if
    I hope this is OK now

    Jerry Grant................tnarG yrreJ
    Website: <JG-Design></.net>
    Email: [email protected]
    Working towards a bug free world......
    (Not a Microsoft employee)

  5. #5
    Guest

    Smile

    THx, jus what i was looking for Works Fine!

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