Guru | Un bout de code VB permettant de manipuler la base de registre récupéré sur le MSDN il y a un moment...
A partir de la ce devrait être assez simple.
Code :
- Option Explicit
- ' Reg Key Security Options...
- Global Const KEY_ALL_ACCESS = &H2003F
- Global Const REG_OPTION_NON_VOLATILE = 0
- ' Reg Key ROOT Types...
- Global Const HKEY_CURRENT_USER = &H80000001
- Global Const HKEY_LOCAL_MACHINE = &H80000002
- Global Const ERROR_SUCCESS = 0
- Global Const REG_SZ = 1 ' Unicode nul terminated string
- Global Const REG_DWORD = 4 ' 32-bit number
- Declare Function RegCreateKeyEx Lib "advapi32" 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" Alias "RegOpenKeyExA" (ByVal hKey As Long, ByVal lpSubKey As String, ByVal ulOptions As Long, ByVal samDesired As Long, ByRef phkResult As Long) As Long
- Declare Function RegQueryValueEx Lib "advapi32" Alias "RegQueryValueExA" (ByVal hKey As Long, ByVal lpValueName As String, ByVal lpReserved As Long, ByRef lpType As Long, ByVal lpData As String, ByRef lpcbData As Long) As Long
- Declare Function RegCloseKey Lib "advapi32" (ByVal hKey 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
- Public Function GetKeyValue( _
- KeyRoot As Long, _
- KeyName As String, _
- SubKeyRef As String, _
- ByRef KeyVal As String _
- ) As Boolean
- Dim i As Long ' Loop Counter
- Dim rc As Long ' Return Code
- Dim hKey As Long ' Handle To An Open Registry Key
- Dim hDepth As Long '
- Dim KeyValType As Long ' Data Type Of A Registry Key
- Dim tmpVal As String ' Tempory Storage For A Registry Key Value
- Dim KeyValSize As Long ' Size Of Registry Key Variable
- '------------------------------------------------------------
- ' Open RegKey Under KeyRoot {HKEY_LOCAL_MACHINE...}
- '------------------------------------------------------------
- rc = RegOpenKeyEx(KeyRoot, KeyName, 0, KEY_ALL_ACCESS, hKey) ' Open Registry Key
-
- If (rc <> ERROR_SUCCESS) Then GoTo GetKeyError ' Handle Error...
-
- tmpVal = String$(1024, 0) ' Allocate Variable Space
- KeyValSize = 1024 ' Mark Variable Size
-
- '------------------------------------------------------------
- ' Retrieve Registry Key Value...
- '------------------------------------------------------------
- rc = RegQueryValueEx(hKey, SubKeyRef, 0, KeyValType, tmpVal, KeyValSize) ' Get/Create Key Value
-
- If (rc <> ERROR_SUCCESS) Then GoTo GetKeyError ' Handle Errors
-
- If (Asc(Mid(tmpVal, KeyValSize, 1)) = 0) Then ' Win95 Adds Null Terminated String...
- tmpVal = Left(tmpVal, KeyValSize - 1) ' Null Found, Extract From String
- Else ' WinNT Does NOT Null Terminate String...
- tmpVal = Left(tmpVal, KeyValSize) ' Null Not Found, Extract String Only
- End If
- '------------------------------------------------------------
- ' Determine Key Value Type For Conversion...
- '------------------------------------------------------------
- Select Case KeyValType ' Search Data Types...
- Case REG_SZ ' String Registry Key Data Type
- KeyVal = tmpVal ' Copy String Value
- Case REG_DWORD ' Double Word Registry Key Data Type
- For i = Len(tmpVal) To 1 Step -1 ' Convert Each Bit
- KeyVal = KeyVal + Hex(Asc(Mid(tmpVal, i, 1))) ' Build Value Char. By Char.
- Next
- KeyVal = Format$("&h" + KeyVal) ' Convert Double Word To String
- End Select
-
- GetKeyValue = True ' Return Success
- rc = RegCloseKey(hKey) ' Close Registry Key
- Exit Function ' Exit
-
- GetKeyError: ' Cleanup After An Error Has Occured...
- KeyVal = "" ' Set Return Val To Empty String
- GetKeyValue = False ' Return Failure
- rc = RegCloseKey(hKey) ' Close Registry Key
- End Function
- Public Function CreateNewKey( _
- sNewKeyName As String, _
- lPredefinedKey As Long _
- ) As Boolean
- Dim hNewKey As Long
- Dim lRetVal As Long
-
- lRetVal = RegCreateKeyEx(lPredefinedKey, sNewKeyName, 0&, _
- vbNullString, REG_OPTION_NON_VOLATILE, KEY_ALL_ACCESS, _
- 0&, hNewKey, lRetVal)
- If (lRetVal <> ERROR_SUCCESS) Then GoTo CreateKeyError
- RegCloseKey (hNewKey)
- CreateNewKey = True
- Exit Function
- CreateKeyError:
- CreateNewKey = False
- Exit Function
- End Function
- Public Function SetKeyValue( _
- sKeyName As String, _
- lPredefinedKey As Long, _
- sValueName As String, _
- vValueSetting As Variant, _
- lValueType As Long _
- ) As Boolean
- Dim lRetVal As Long
- Dim hKey As Long
-
- lRetVal = RegOpenKeyEx(lPredefinedKey, sKeyName, 0, KEY_ALL_ACCESS, hKey)
- If (lRetVal <> ERROR_SUCCESS) Then GoTo SetKeyError
- lRetVal = SetValueEx(hKey, sValueName, lValueType, vValueSetting)
- If (lRetVal <> ERROR_SUCCESS) Then GoTo SetKeyError
- RegCloseKey (hKey)
- SetKeyValue = True
- Exit Function
-
- SetKeyError:
- SetKeyValue = False
- Exit Function
- End Function
- Private Function SetValueEx( _
- ByVal hKey As Long, _
- sValueName As String, _
- lType As Long, _
- vValue As Variant _
- ) As Long
- Dim lValue As Long
- Dim sValue As String
-
- Select Case lType
- Case REG_SZ
- sValue = vValue & Chr$(0)
- SetValueEx = RegSetValueExString(hKey, sValueName, 0&, lType, sValue, Len(sValue))
- Case REG_DWORD
- lValue = vValue
- SetValueEx = RegSetValueExLong(hKey, sValueName, 0&, lType, lValue, 4)
- End Select
- End Function
|
[jfdsdjhfuetppo]--Message édité par Guru--[/jfdsdjhfuetppo] |