Registre: une cle existe ou non?

Signaler
Messages postés
287
Date d'inscription
vendredi 25 octobre 2002
Statut
Membre
Dernière intervention
18 janvier 2004
-
Messages postés
1471
Date d'inscription
mardi 5 février 2002
Statut
Membre
Dernière intervention
20 octobre 2014
-
Voilà, je suis à la recherche d'une formule SIMPLE ET CONCISE :-p :-p :-p pour vérifier si une clé existe dans le registre...
Voilà, merci d'avance.
PS: une formule simple, qu'un débutant comme moi pourrait comprendre...

@+
Vbsupernul (tout le monde le sait maintenant... c'est malin)

1 réponse

Messages postés
1471
Date d'inscription
mardi 5 février 2002
Statut
Membre
Dernière intervention
20 octobre 2014
12
'Declaration

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

'Utilisation:
RegOpenKeyEx HKEY_CURRENT_USER, "KPD-Team", 0, KEY_ALL_ACCESS, Result

Result=0 si la clé n'existe pas

Le code n'est pas de moi (merci API-Guide) ...

'Je te met l'exemple complet d'API-GUIDE:

Const HKEY_CURRENT_USER = &H80000001
Const REG_OPTION_BACKUP_RESTORE = 4 ' open for backup or restore
Const REG_OPTION_VOLATILE = 1 ' Key is not preserved when system is rebooted
Const REG_OPTION_NON_VOLATILE = 0 ' Key is preserved when system is rebooted
Const STANDARD_RIGHTS_ALL = &H1F0000
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_CREATE_SUB_KEY = &H4
Const KEY_ENUMERATE_SUB_KEYS = &H8
Const KEY_NOTIFY = &H10
Const KEY_QUERY_VALUE = &H1
Const KEY_SET_VALUE = &H2
Const KEY_READ = ((STANDARD_RIGHTS_READ Or KEY_QUERY_VALUE Or KEY_ENUMERATE_SUB_KEYS Or KEY_NOTIFY) And (Not SYNCHRONIZE))
Const KEY_WRITE = ((STANDARD_RIGHTS_WRITE Or KEY_SET_VALUE Or KEY_CREATE_SUB_KEY) And (Not SYNCHRONIZE))
Const KEY_EXECUTE = (KEY_READ)
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 Declare Function RegCloseKey Lib "advapi32.dll" (ByVal hKey As Long) As Long
Private Declare Function RegDeleteKey Lib "advapi32.dll" Alias "RegDeleteKeyA" (ByVal hKey As Long, ByVal lpSubKey As String) As Long
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 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
Private Sub Form_Load()
'KPD-Team 2000
'URL: http://www.allapi.net/
'E-Mail: KPDTeam@Allapi.net
Dim Result As Long
'Check if the specified key exists
RegOpenKeyEx HKEY_CURRENT_USER, "KPD-Team", 0, KEY_ALL_ACCESS, Result
'If the key doesn't exist, we create it
If Result = 0 Then
'Create a new key
RegCreateKeyEx HKEY_CURRENT_USER, "KPD-Team", 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
End If
'Delete the key
RegDeleteKey Result, ""
'close the handle
RegCloseKey Result
End Sub
0