croftman
Messages postés110Date d'inscriptiondimanche 23 janvier 2005StatutMembreDernière intervention 2 avril 2010
-
10 juil. 2006 à 19:29
croftman
Messages postés110Date d'inscriptiondimanche 23 janvier 2005StatutMembreDernière intervention 2 avril 2010
-
14 juil. 2006 à 12:32
Bonjour
J'utilise habituellement, car c'est le plus simple, la méthode :
Dim WshShell As Object
Set WshShell = CreateObject("Wscript.Shell")
pour écrire, lire et effacer dans la base de registre, mais pour lire des valeurs bianires, cette méthode ne fonctionne pas, ou je ne sais pas comment :/
J'ai donc recuperé les API, mais comment lire une valeur binaire avec les API? RegReadKey?
Je sais qu'il faut ouvrir la cle, la lire puis la fermer, mais pour la lire??
PCPT
Messages postés13272Date d'inscriptionlundi 13 décembre 2004StatutMembreDernière intervention 3 février 201847 10 juil. 2006 à 21:40
salut,
on ouvre la clé, on lit le handle retourné par l'ouverture.
regarde dans les sources existantes, y'a masse
sinon :
'This program needs 3 buttons
Const REG_SZ = 1 ' Unicode nul terminated string
Const REG_BINARY = 3 ' Free form binary
Const HKEY_CURRENT_USER = &H80000001
Private Declare Function RegCloseKey Lib "advapi32.dll" (ByVal hKey As Long) As Long
Private Declare Function RegCreateKey Lib "advapi32.dll" Alias "RegCreateKeyA" (ByVal hKey As Long, ByVal lpSubKey As String, phkResult 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 RegOpenKey Lib "advapi32.dll" Alias "RegOpenKeyA" (ByVal hKey As Long, ByVal lpSubKey As String, phkResult As Long) As Long
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 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
Function RegQueryStringValue(ByVal hKey As Long, ByVal strValueName As String) As String
Dim lResult As Long, lValueType As Long, strBuf As String, lDataBufSize As Long
'retrieve nformation about the key
lResult = RegQueryValueEx(hKey, strValueName, 0, lValueType, ByVal 0, lDataBufSize)
If lResult = 0 Then
If lValueType = REG_SZ Then
'Create a buffer
strBuf = String (lDataBufSize, Chr$(0))
'retrieve the key's content
lResult = RegQueryValueEx(hKey, strValueName, 0, 0, ByVal strBuf, lDataBufSize)
If lResult = 0 Then
'Remove the unnecessary chr$(0)'s
RegQueryStringValue = Left$(strBuf, InStr (1, strBuf, Chr$(0)) - 1)
End If
ElseIf lValueType = REG_BINARY Then
Dim strData As Integer
'retrieve the key's value
lResult = RegQueryValueEx(hKey, strValueName, 0, 0, strData, lDataBufSize)
If lResult = 0 Then
RegQueryStringValue = strData
End If
End If
End If
End Function
Function GetString(hKey As Long, strPath As String, strValue As String)
Dim Ret
'Open the key
RegOpenKey hKey, strPath, Ret
'Get the key's content
GetString = RegQueryStringValue(Ret, strValue)
'Close the key
RegCloseKey Ret
End Function
Sub SaveString(hKey As Long, strPath As String, strValue As String, strData As String)
Dim Ret
'Create a new key
RegCreateKey hKey, strPath, Ret
'Save a string to the key
RegSetValueEx Ret, strValue, 0, REG_SZ, ByVal strData, Len (strData)
'close the key
RegCloseKey Ret
End Sub
Sub SaveStringLong(hKey As Long, strPath As String, strValue As String, strData As String)
Dim Ret
'Create a new key
RegCreateKey hKey, strPath, Ret
'Set the key's value
RegSetValueEx Ret, strValue, 0, REG_BINARY, CByte(strData), 4
'close the key
RegCloseKey Ret
End Sub
Sub DelSetting(hKey As Long, strPath As String, strValue As String)
Dim Ret
'Create a new key
RegCreateKey hKey, strPath, Ret
'Delete the key's value
RegDeleteValue Ret, strValue
'close the key
RegCloseKey Ret
End Sub
Private Sub Command1_Click()
Dim strString As String
'Ask for a value
strString = InputBox("Please enter a value between 0 and 255 to be saved as a binary value in the registry.", App.Title)
If strString = "" Or Val (strString) > 255 Or Val(strString) < 0 Then
MsgBox "Invalid value entered ...", vbExclamation + vbOKOnly, App.Title
Exit Sub
End If
'Save the value to the registry
SaveStringLong HKEY_CURRENT_USER, "KPD-Team", "BinaryValue", CByte(strString)
End Sub
Private Sub Command2_Click()
'Get a string from the registry
Ret = GetString(HKEY_CURRENT_USER, "KPD-Team", "BinaryValue")
If Ret = "" Then MsgBox "No value found !", vbExclamation + vbOKOnly, App.Title: Exit Sub
MsgBox "The value is " + Ret, vbOKOnly + vbInformation, App.Title
End Sub
Private Sub Command3_Click()
'Delete the setting from the registry
DelSetting HKEY_CURRENT_USER, "KPD-Team", "BinaryValue"
MsgBox "The value was deleted ...", vbInformation + vbOKOnly, App.Title
End Sub
Private Sub Form_Load()
'KPD-Team 1998
'URL: http://www.allapi.net/ 'E-Mail: KPDTeam@Allapi.net
Command1.Caption = "Set Value"
Command2.Caption = "Get Value"
Command3.Caption = "Delete Value"
End Sub
croftman
Messages postés110Date d'inscriptiondimanche 23 janvier 2005StatutMembreDernière intervention 2 avril 2010 11 juil. 2006 à 12:41
Problème résolu, un grand merci, mais si je veux verifier si une cle existe (pas une valeur), comment je dois m'y prendre, j'ai essayé quelque manip, mais en vain, je pensai que si la clé n'existait pas, regopen me
donnerait autre chose que 0, mais non :/
PCPT
Messages postés13272Date d'inscriptionlundi 13 décembre 2004StatutMembreDernière intervention 3 février 201847 12 juil. 2006 à 13:38
hein?
beuh... non, c'est la différence entre RegOpenKey et RegQueryValueEx
mais sinon, voilà le détail des 3 :
<hr size= "2" width="100%" />Declare Function RegOpenKey Lib "advapi32.dll" Alias "RegOpenKeyA" _
(ByVal hKey As Long, ByVal lpSubKey As String, phkResult As Long) As Long
· hKey
Identifies a currently open key or any of the following predefined reserved handle values:
HKEY_CLASSES_ROOT
HKEY_CURRENT_USER
HKEY_LOCAL_MACHINE
HKEY_USERS
The key opened by the RegOpenKey function is a subkey of the key identified by hKey.
· lpSubKey
Points to a null-terminated string containing the name of the key to
open. This key must be a subkey of the key identified by the hKey
parameter.
If this parameter is NULL or a pointer to an empty string, the function returns the same handle that was passed in.
· phkResult
Points to a variable that receives the handle of the opened key.
If the function succeeds, the return value is ERROR_SUCCESS.
If the function fails, the return value is a nonzero error code defined
in WINERROR.H. You can use the FormatMessage function with the
FORMAT_MESSAGE_FROM_SYSTEM flag to get a generic description of the
error.
<hr size ="2" width= "100%" />
<hr size="2" width="100%" />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
· hKey
Identifies a currently open key or any of the following predefined reserved handle values:
HKEY_CLASSES_ROOT
HKEY_CURRENT_USER
HKEY_LOCAL_MACHINE
HKEY_USERS
· lpSubKey
Points to a null-terminated string containing the name of the subkey to open.
If this parameter is NULL or a pointer to an empty string, the function will open a new handle of the key
identified by the hKey parameter. In this case, the function will not close the handles previously opened.
· ulOptions
Reserved; must be zero.
· samDesired
Specifies an access mask that describes the desired security access for
the new key. This parameter can be a combination of the following
values:
KEY_ALL_ACCESS
Combination of KEY_QUERY_VALUE, KEY_ENUMERATE_SUB_KEYS, KEY_NOTIFY, KEY_CREATE_SUB_KEY,
KEY_CREATE_LINK, and KEY_SET_VALUE access.
KEY_CREATE_LINK
Permission to create a symbolic link.
KEY_CREATE_SUB_KEY
Permission to create subkeys.
KEY_ENUMERATE_SUB_KEYS
Permission to enumerate subkeys.
KEY_EXECUTE
Permission for read access.
KEY_NOTIFY
Permission for change notification.
KEY_QUERY_VALUE
Permission to query subkey data.
KEY_READ
Combination of KEY_QUERY_VALUE, KEY_ENUMERATE_SUB_KEYS, and KEY_NOTIFY access.
KEY_SET_VALUE
Permission to set subkey data.
KEY_WRITE
Combination of KEY_SET_VALUE and KEY_CREATE_SUB_KEY access.
· phkResult
Points to a variable that receives the handle of the opened key.
If the function succeeds, the return value is ERROR_SUCCESS.
If the function fails, the return value is a nonzero error code defined
in WINERROR.H. You can use the FormatMessage function with the
FORMAT_MESSAGE_FROM_SYSTEM flag to get a generic description of the
error.
<hr size ="2" width= "100%" />
<hr size="2" width="100%" />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
' Note that if you declare the lpData parameter as String, you must pass it By Value.
· hKey
Identifies a currently open key or any of the following predefined reserved handle values:
HKEY_CLASSES_ROOT
HKEY_CURRENT_USER
HKEY_LOCAL_MACHINE
HKEY_USERS
· lpValueName
Points to a null-terminated string containing the name of the value to be queried.
· lpReserved
Reserved; must be NULL.
· lpType
Points to a variable that receives the key’s value type. The value returned through this parameter will be one of the following:
REG_BINARY
Binary data in any form.
REG_DWORD
A 32-bit number.
REG_DWORD_LITTLE_ENDIAN
A 32-bit number in little-endian format (same as REG_DWORD). In
little-endian format, the most significant byte of a word is the
high-order byte.
This is the most common format for computers running Windows NT and Windows 95.
REG_DWORD_BIG_ENDIAN
A 32-bit number in big-endian format. In big-endian format, the most significant byte of a word is the low-order byte.
REG_EXPAND_SZ
A null-terminated string that contains unexpanded references to environment variables (for example, “%PATH%”).
It will be a Unicode or ANSI string depending on whether you use the Unicode or ANSI functions.
REG_LINK
A Unicode symbolic link.
REG_MULTI_SZ
An array of null-terminated strings, terminated by two null characters.
REG_NONE
No defined value type.
REG_RESOURCE_LIST
A device-driver resource list.
REG_SZ
A null-terminated string. It will be a Unicode or ANSI string depending on whether you use the Unicode or ANSI functions.
The lpType parameter can be NULL if the type is not required.
· lpData
Points to a buffer that receives the value’s data. This parameter can be NULL if the data is not required.
· lpcbData
Points to a variable that specifies the size, in bytes, of the buffer pointed to by the lpData parameter.
When the function returns, this variable contains the size of the data copied to lpData.
If the buffer specified by lpData parameter is not large enough to hold the data, the function returns the
value ERROR_MORE_DATA, and stores the required buffer size, in bytes, into the variable pointed to by lpcbData.
If lpData is NULL, and lpcbData is non-NULL, the function returns ERROR_SUCCESS, and stores the size of the data,
in bytes, in the variable pointed to by lpcbData. This lets an
application determine the best way to allocate a buffer for the value
key’s data.
If the data has the REG_SZ, REG_MULTI_SZ or REG_EXPAND_SZ type, then
lpData will also include the size of the terminating null character.
The lpcbData parameter can be NULL only if lpData is NULL.
If the function succeeds, the return value is ERROR_SUCCESS.
If the function fails, the return value is a nonzero error code defined
in WINERROR.H. You can use the FormatMessage function with the
FORMAT_MESSAGE_FROM_SYSTEM flag to get a generic description of the
error.
<hr size ="2" width="100%" />
++
PCPT [AFCK]
<hr size="2" width="100%" />Prenez un instant pour répondre à ce sondage svp