OVH Cloud OVH Cloud

cle dans Base de registre

15 réponses
Avatar
yannick
Bonsoir à tous
J'utilise la commande SaveSetting appname pur enregistrer une cle dans le
registre
exemple:
SaveSetting appname:="MonAppli", Section:="application", Key:="serial",
Setting:="028825aa"
Je voudrais savoir comment peut mettre 028825aa par ******** non visible par
l'utilisateur dans la base de registre


Merci pour toute aide.

Bonne soirée

Yannick

5 réponses

1 2
Avatar
Anor
Salut les copains

Voici un exemple de code complet (lecture+écriture dans la base de registres) que j'avais dans
mes tablettes :

'**********************************CODE BEGIN**********************
Option Compare Database
Option Explicit

Type SECURITY_ATTRIBUTES
nLength As Long
lpSecurityDescriptor As Long
bInheritHandle As Boolean
End Type

Type FILETIME
dwLowDateTime As Long
dwHighDateTime As Long
End Type

' Constants for Registry top-level keys
Public Const HKEY_CURRENT_USER = &H80000001
Public Const HKEY_LOCAL_MACHINE = &H80000002
Public Const HKEY_USERS = &H80000003
Public Const HKEY_DYN_DATA = &H80000006
Public Const HKEY_CURRENT_CONFIG = &H80000005
Public Const HKEY_CLASSES_ROOT = &H80000000

' Return values
Public Const ERROR_SUCCESS = 0&
Public Const ERROR_FILE_NOT_FOUND = 2&
Public Const ERROR_MORE_DATA = 234

' RegCreateKeyEx options
Public Const REG_OPTION_NON_VOLATILE = 0

' RegCreateKeyEx Disposition
Public Const REG_CREATED_NEW_KEY = &H1
Public Const REG_OPENED_EXISTING_KEY = &H2

' Registry data types
Public Const REG_SZ = 1
Public Const REG_BINARY = 3

' Registry security attributes
Public Const KEY_QUERY_VALUE = &H1
Public Const KEY_SET_VALUE = &H2
Public Const KEY_CREATE_SUB_KEY = &H4

Declare Function RegEnumValue Lib "advapi32.dll" _
Alias "RegEnumValueA" (ByVal hKey As Long, _
ByVal dwIndex As Long, ByVal lpValueName As String, _
lpcbValueName As Long, lpReserved As Long, _
lpType As Long, lpData As Byte, lpcbData As Long) As Long

Declare Function RegQueryInfoKey Lib "advapi32.dll" _
Alias "RegQueryInfoKeyA" (ByVal hKey As Long, _
ByVal lpClass As String, lpcbClass As Long, _
lpReserved As Long, lpcSubKeys As Long, _
lpcbMaxSubKeyLen As Long, lpcbMaxClassLen As Long, _
lpcValues As Long, lpcbMaxValueNameLen As Long, _
lpcbMaxValueLen As Long, lpcbSecurityDescriptor As Long, _
lpftLastWriteTime As FILETIME) As Long

Declare Function RegDeleteValue Lib "advapi32.dll" _
Alias "RegDeleteValueA" _
(ByVal hKey As Long, ByVal lpValueName As String) _
As Long

Declare Function RegDeleteKey Lib "advapi32.dll" _
Alias "RegDeleteKeyA" _
(ByVal hKey As Long, ByVal lpSubKey As String) 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 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

Declare Function RegQueryValueEx Lib "advapi32.dll" _
Alias "RegQueryValueExA" _
(ByVal hKey As Long, ByVal lpszValueName As String, _
ByVal lpdwReserved As Long, lpdwType As Long, _
lpData As Any, lpcbData As Long) As Long

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

Declare Function RegCloseKey Lib "advapi32.dll" _
(ByVal hKey As Long) As Long

Declare Function GetPrivateProfileSection Lib "kernel32" _
Alias "GetPrivateProfileSectionA" _
(ByVal lpAppName As String, _
ByVal lpReturnedString As String, _
ByVal nSize As Long, ByVal _
lpFileName As String) As Long

Declare Function GetPrivateProfileString Lib "kernel32" _
Alias "GetPrivateProfileStringA" _
(ByVal lpApplicationName As String, _
ByVal lpKeyName As Any, ByVal lpDefault As String, _
ByVal lpReturnedString As String, ByVal nSize As Long, _
ByVal lpFileName As String) As Long

Declare Function WritePrivateProfileString Lib "kernel32" _
Alias "WritePrivateProfileStringA" _
(ByVal lpApplicationName As String, _
ByVal lpKeyName As Any, ByVal lpString As Any, _
ByVal lpFileName As String) As Long

Declare Function GetPrivateProfileInt Lib "kernel32" _
Alias "GetPrivateProfileIntA" _
(ByVal lpApplicationName As String, _
ByVal lpKeyName As String, ByVal nDefault As Long, _
ByVal lpFileName As String) As Long

Public Function fDeleteKey(ByVal sTopKey As String, _
ByVal sSubKey As String, ByVal sKeyName As String) As Long

Dim lTopKey As Long
Dim lHandle As Long
Dim lResult As Long

On Error GoTo fDeleteKeyError
lResult = 99
lTopKey = fTopKey(sTopKey)
If lTopKey = 0 Then GoTo fDeleteKeyError

lResult = RegOpenKeyEx(lTopKey, sSubKey, 0, KEY_CREATE_SUB_KEY, lHandle)
If lResult = ERROR_SUCCESS Then
lResult = RegDeleteKey(lHandle, sKeyName)
End If

If lResult = ERROR_SUCCESS Or lResult = ERROR_FILE_NOT_FOUND Then
fDeleteKey = ERROR_SUCCESS
Else
fDeleteKey = lResult
End If
Exit Function

fDeleteKeyError:
MsgBox "Unable to delete registry key.", vbExclamation, "fDeleteKey"
fDeleteKey = lResult
End Function

Public Function fDeleteValue(ByVal sTopKeyOrFile As String, _
ByVal sSubKeyOrSection As String, ByVal sValueName As String) As Long

Dim lTopKey As Long
Dim lHandle As Long
Dim lResult As Long

On Error GoTo fDeleteValueError
lResult = 99
lTopKey = fTopKey(sTopKeyOrFile)
If lTopKey = 0 Then GoTo fDeleteValueError

If lTopKey = 1 Then
lResult = WritePrivateProfileString(sSubKeyOrSection, _
sValueName, "", sTopKeyOrFile)
Else
lResult = RegOpenKeyEx(lTopKey, sSubKeyOrSection, 0, _
KEY_SET_VALUE, lHandle)

If lResult = ERROR_SUCCESS Then
lResult = RegDeleteValue(lHandle, sValueName)
End If

If lResult = ERROR_SUCCESS Or lResult = ERROR_FILE_NOT_FOUND Then
fDeleteValue = ERROR_SUCCESS
Else
fDeleteValue = lResult
End If
End If
Exit Function

fDeleteValueError:
MsgBox "Unable to delete registry or .ini file value.", _
vbExclamation, "fDeleteValue"
fDeleteValue = lResult
End Function

Public Function fEnumValue(ByVal sTopKeyOrIniFile As String, _
ByVal sSubKeyOrSection As String, sValues As String) As Long

Dim lTopKey As Long
Dim lHandle As Long
Dim lResult As Long
Dim lMaxLen As Long
Dim lLenData As Long
Dim lActualLen As Long
Dim lValues As Long
Dim lIndex As Long
Dim lValueType As Long
Dim sValueName As String
Dim sValue As String
Dim bValue As Boolean
Dim tFileTime As FILETIME

On Error GoTo fEnumValueError
lResult = 99
lTopKey = fTopKey(sTopKeyOrIniFile)
If lTopKey = 0 Then GoTo fEnumValueError

If lTopKey = 1 Then
'
' Enumerate an .ini file section.
'
sValues = Space$(8192)
lResult = GetPrivateProfileSection(sSubKeyOrSection, _
sValues, Len(sValues), sTopKeyOrIniFile)
Else
'
' Open the registry SubKey.
'
lResult = RegOpenKeyEx(lTopKey, sSubKeyOrSection, 0, _
KEY_QUERY_VALUE, lHandle)

If lResult <> ERROR_SUCCESS Then GoTo fEnumValueError

lResult = RegQueryInfoKey(lHandle, "", 0, 0, 0, 0, 0, _
lValues, lLenData, 0, 0, tFileTime)

If lResult <> ERROR_SUCCESS Then GoTo fEnumValueError
lMaxLen = lLenData + 1

Do While lIndex <= lValues - 1
sValueName = Space$(lMaxLen)
lActualLen = lMaxLen
'
' Query the value's type, size and length.
'
Call RegEnumValue(lHandle, lIndex, sValueName, _
lActualLen, 0, lValueType, ByVal 0, 0)
'
' Get the actual value.
'
If lValueType = REG_SZ Then
'
' String value. The first query gets the string length.
' The second gets the string value.
'
sValueName = Left$(sValueName, lActualLen)
lLenData = 0

lResult = RegQueryValueEx(lHandle, sValueName, 0, _
REG_SZ, "", lLenData)
If lResult = ERROR_MORE_DATA Then
sValue = Space$(lLenData)
lResult = RegQueryValueEx(lHandle, sValueName, 0, _
REG_SZ, ByVal sValue, lLenData)
If lResult = ERROR_SUCCESS Then
sValues = sValues & sValueName & "=" & sValue
Else
GoTo fEnumValueError
End If
Else
GoTo fEnumValueError
End If
Else
'
' Boolean value.
'
lLenData = Len(bValue)
lResult = RegQueryValueEx(lHandle, sValueName, 0, 0, _
bValue, lLenData)
If lResult = ERROR_SUCCESS Then
sValueName = Left$(sValueName, lActualLen)
sValues = sValues & sValueName & "=" _
& bValue & vbNullChar
Else
GoTo fEnumValueError
End If
End If
lIndex = lIndex + 1
Loop
sValues = sValues & vbNullChar
'
' Close the key.
'
lResult = RegCloseKey(lHandle)
fEnumValue = lResult
End If
Exit Function
'
' Error processing.
'
fEnumValueError:
MsgBox "Unable to enumerate registry or .ini file values.", _
vbExclamation, "fEnumValue"
fEnumValue = lResult
End Function


Public Function fReadIniFuzzy(ByVal sIniFile As String, _
sSection As String, ByVal sIniEntry As String, _
ByVal sDefault As String, sValue As String) As Long

Dim sNextChar As String
Dim sLine As String
Dim sEntry As String
Dim sSectionName As String
Dim iLen As Integer
Dim iLocOfEq As Integer
Dim iFnum As Integer
Dim bDone As Boolean
Dim bFound As Boolean
Dim bNewSection As Boolean

On Error GoTo fReadIniFuzzyError
fReadIniFuzzy = 99
bDone = False
sValue = sDefault
sEntry = UCase$(sIniEntry)
sSection = UCase$(sSection)
iLen = Len(sSection)

iFnum = FreeFile
Open sIniFile For Input Access Read As iFnum

Line Input #iFnum, sLine
Do While Not EOF(iFnum) And Not bDone
sLine = UCase$(Trim$(sLine))
bNewSection = False
'
' See if line is a section heading.
'
If Left$(sLine, 1) = "[" Then
'
' See if section heading contains desired value.
'
sSectionName = sLine
Dim iPos As Integer
iPos = InStr(1, sLine, sSection)
If iPos > 0 Then
'
' Be sure the value is not part of a larger value.
'
sNextChar = Mid$(sLine, iPos + iLen, 1)
If sNextChar = " " Or sNextChar = "]" Then
'
' Search this section for the entry.
'
Line Input #iFnum, sLine
bFound = False
bNewSection = False
Do While Not EOF(iFnum) And Not bFound
'
' If we hit a new section, stop.
'
sLine = UCase$(Trim$(sLine))
If Left$(sLine, 1) = "[" Then
bNewSection = True
Exit Do
End If
'
' Entry must start in column 1 to avoid comment lines.
'
If InStr(1, sLine, sEntry) = 1 Then
'
' If entry found and line is not incomplete, get value.
'
iLocOfEq = InStr(1, sLine, "=")
If iLocOfEq <> 0 Then
sValue = Mid$(sLine, iLocOfEq + 1)
sSection = Mid$(sSectionName, 2, _
InStr(1, sSectionName, "]") - 2)
bFound = True
bDone = True
fReadIniFuzzy = 0
End If
End If
If Not bFound Then
Line Input #iFnum, sLine
End If
Loop
If EOF(iFnum) Then bDone = True
sSection = Mid$(sSectionName, 2, _
InStr(1, sSectionName, "]") - 2)
End If
End If
End If
If Not bNewSection And Not bDone Then
Line Input #iFnum, sLine
End If
Loop
Close iFnum
Exit Function

fReadIniFuzzyError:
MsgBox "Unable to read .ini file value.", _
vbExclamation, "fReadIniFuzzy"
fReadIniFuzzy = 99
End Function

Public Function ReadRegistry(ByVal sTopKeyOrFile As String, _
ByVal sSubKeyOrSection As String, ByVal sValueName As String, _
ByVal sValueType As String, ByVal vDefault As Variant, _
vValue As Variant) As Long

Dim lTopKey As Long
Dim lHandle As Long
Dim lLenData As Long
Dim lResult As Long
Dim lDefault As Long
Dim sValue As String
Dim sSubKeyPath As String
Dim sDefaultStr As String
Dim bValue As Boolean

On Error GoTo fReadValueError
lResult = 99
vValue = vDefault
lTopKey = fTopKey(sTopKeyOrFile)
If lTopKey = 0 Then GoTo fReadValueError

If lTopKey = 1 Then
'
' Read the .ini file value.
'
If UCase$(sValueType) = "S" Then
lLenData = 255
sDefaultStr = vDefault
sValue = Space$(lLenData)
lResult = GetPrivateProfileString(sSubKeyOrSection, _
sValueName, sDefaultStr, sValue, lLenData, sTopKeyOrFile)
vValue = Left$(sValue, lResult)
Else
lDefault = 0
lResult = GetPrivateProfileInt(sSubKeyOrSection, _
sValueName, lDefault, sTopKeyOrFile)
End If
Else
'
' Open the registry SubKey.
'
lResult = RegOpenKeyEx(lTopKey, sSubKeyOrSection, 0, _
KEY_QUERY_VALUE, lHandle)
If lResult <> ERROR_SUCCESS Then GoTo fReadValueError
'
' Get the actual value.
'
If UCase$(sValueType) = "S" Then
'
' String value. The first query gets the string length. The second
' gets the string value.
'
lResult = RegQueryValueEx(lHandle, sValueName, 0, _
REG_SZ, "", lLenData)
If lResult = ERROR_MORE_DATA Then
sValue = Space(lLenData)
lResult = RegQueryValueEx(lHandle, sValueName, 0, _
REG_SZ, ByVal sValue, lLenData)
End If
If lResult = ERROR_SUCCESS Then 'Remove null character.
vValue = Left$(sValue, lLenData - 1)
Else
GoTo fReadValueError
End If
Else
'
' Boolean value.
'
lLenData = Len(bValue)
lResult = RegQueryValueEx(lHandle, sValueName, 0, 0, _
bValue, lLenData)
If lResult = ERROR_SUCCESS Then
vValue = bValue
Else
GoTo fReadValueError
End If
End If
'
' Close the key.
'
lResult = RegCloseKey(lHandle)
ReadRegistry = lResult
End If
Exit Function
'
' Error processing.
'
fReadValueError:
vValue = vDefault
End Function

Private Function fTopKey(ByVal sTopKeyOrFile As String) As Long
Dim sDir As String

' This function returns:
' - the numeric value of a top level registry key or
' - 1 if sTopKey is a valid .ini file or
' - 0 otherwise.
'
On Error GoTo fTopKeyError
fTopKey = 0
Select Case UCase$(sTopKeyOrFile)
Case "HKCU"
fTopKey = HKEY_CURRENT_USER
Case "HKLM"
fTopKey = HKEY_LOCAL_MACHINE
Case "HKU"
fTopKey = HKEY_USERS
Case "HKDD"
fTopKey = HKEY_DYN_DATA
Case "HKCC"
fTopKey = HKEY_CURRENT_CONFIG
Case "HKCR"
fTopKey = HKEY_CLASSES_ROOT
Case Else
On Error Resume Next
sDir = Dir$(sTopKeyOrFile)
If err.Number = 0 And sDir <> "" Then fTopKey = 1
End Select
Exit Function

fTopKeyError:
MsgBox "Unable to decode registry key or find .ini file.", _
vbExclamation, "fTopKey"
End Function

Public Function WriteRegistry(ByVal sTopKeyOrFile As String, _
ByVal sSubKeyOrSection As String, ByVal sValueName As String, _
ByVal sValueType As String, ByVal vValue As Variant) As Long

Dim hKey As Long
Dim lTopKey As Long
Dim lOptions As Long
Dim lsamDesired As Long
Dim lHandle As Long
Dim lDisposition As Long
Dim lLenData As Long
Dim lResult As Long
Dim sClass As String
Dim sValue As String
Dim sSubKeyPath As String
Dim bValue As Boolean
Dim tSecurityAttributes As SECURITY_ATTRIBUTES

On Error GoTo fWriteValueError
lResult = 99
lTopKey = fTopKey(sTopKeyOrFile)
If lTopKey = 0 Then GoTo fWriteValueError

If lTopKey = 1 Then
'
' Read the .ini file value.
'
If UCase$(sValueType) = "S" Then
sValue = vValue
lResult = WritePrivateProfileString(sSubKeyOrSection, _
sValueName, sValue, sTopKeyOrFile)
Else
GoTo fWriteValueError
End If
Else
sClass = ""
lOptions = REG_OPTION_NON_VOLATILE
lsamDesired = KEY_CREATE_SUB_KEY Or KEY_SET_VALUE
'
' Create the SubKey or open it if it exists. Return its handle.
' lDisposition will be REG_CREATED_NEW_KEY if the key did not exist.
'
lResult = RegCreateKeyEx(lTopKey, sSubKeyOrSection, 0, sClass, lOptions, _
lsamDesired, tSecurityAttributes, lHandle, lDisposition)
If lResult <> ERROR_SUCCESS Then GoTo fWriteValueError
'
' Set the actual value.
'
If UCase$(sValueType) = "S" Then 'String value.
sValue = vValue
lLenData = Len(sValue) + 1
lResult = RegSetValueEx(lHandle, sValueName, 0, _
REG_SZ, ByVal sValue, lLenData)
Else 'Boolean value.
bValue = vValue
lLenData = Len(bValue)
lResult = RegSetValueEx(lHandle, sValueName, 0, _
REG_BINARY, bValue, lLenData)
End If
'
' Close the key.
'
If lResult = ERROR_SUCCESS Then
lResult = RegCloseKey(lHandle)
WriteRegistry = lResult
Exit Function
End If
End If
Exit Function
'
' Error processing.
'
fWriteValueError:
MsgBox "Unable to write registry or .ini file value.", _
vbExclamation, "fWriteValue"
WriteRegistry = lResult
End Function

'********************CODE END******************************

Si ça peut aider.....:
à+
--
Arnaud
-----------------------------------
http://users.skynet.be/mpfa/
-----------------------------------
(décline toute responsabilité en cas de modification irréversible, hein ?)


"yannick" a écrit dans le message de news:

| Excuses cela fonctionne pour ecrire mais pas pour lire alors que si j'utilise
| http://users.skynet.be/accesshome/registry.htm pour lire cela fonctionne.
| Donc je vais me débrouiller avec cela;
|
| Je te remercie pour toute l'aide que tu m'as apporté.
|
| Merci et bonne soirée
|
| @+
|
| Yannick
|
|
Avatar
Pierre CFI [mvp]
salut mon petit arnaud

ah je comprend pourquoi tu es resté silencieux. Tu préparais dans le plus grand secret 20 Ko de code

Quel homme cet homme :o))
on va pas tarder à revoir emilie

--
Pierre CFI
MVP Microsoft Access



Avatar
Anor
Bonjour Pierre,


Eh oui, 163840 bits, ça se prépare avec amour ;-)

A+
Arnaud



"Pierre CFI [mvp]" a écrit dans le message de news:

| salut mon petit arnaud
|
| ah je comprend pourquoi tu es resté silencieux. Tu préparais dans le plus grand secret 20 Ko
de code
|
| Quel homme cet homme :o))
| on va pas tarder à revoir emilie
|
| --
| Pierre CFI
| MVP Microsoft Access
| >
|
|
Avatar
Raymond [mvp]
Salut Arnaud.

Faut pas venir que pour ça, c'est toujours un plaisir de lire ta prose. Tu
vas la retrouver sur mon site bientôt, dommage que la doc n'y soit pas ! je
vais être obligé de la faire.
Tu verras Pierre, un jour on aura du code en alexandrins.
--
@+
Raymond Access MVP
http://OfficeSystem.Access.free.fr/
http://OfficeSystem.Access.free.fr/runtime/
http://users.skynet.be/mpfa/ pour débuter sur le forum


"Anor" a écrit dans le message de news:
%

Salut les copains


Avatar
Pierre CFI [mvp]
oh, avec anor, çà risque plutot de tourner vers le porno
Mais à nos ages on aime bien, surtout le belge (parait-il) :o)

--
Pierre CFI
MVP Microsoft Access
Mail : http://cerbermail.com/?z0SN8cN53B

Site pour bien commencer
Access http://users.skynet.be/mpfa/
Excel http://www.excelabo.net

"Raymond [mvp]" a écrit dans le message de news:%23$PU0A%
Salut Arnaud.

Faut pas venir que pour ça, c'est toujours un plaisir de lire ta prose. Tu
vas la retrouver sur mon site bientôt, dommage que la doc n'y soit pas ! je
vais être obligé de la faire.
Tu verras Pierre, un jour on aura du code en alexandrins.
--
@+
Raymond Access MVP
http://OfficeSystem.Access.free.fr/
http://OfficeSystem.Access.free.fr/runtime/
http://users.skynet.be/mpfa/ pour débuter sur le forum


"Anor" a écrit dans le message de news:
%

Salut les copains






1 2