REG_SZ API

browser64 Messages postés 112 Date d'inscription dimanche 24 avril 2005 Statut Membre Dernière intervention 5 décembre 2010 - 24 juin 2006 à 08:15
cs_Willi Messages postés 2375 Date d'inscription jeudi 12 juillet 2001 Statut Modérateur Dernière intervention 15 décembre 2018 - 24 juin 2006 à 10:50
Salut, je ne parvient pas a ecrire une valeur REG_SZ voir code ci-dessous ca ne marche pas.
Je ne trouve pas l'erreur dans le code ou est-elle ? ou est le probleme ?

                       **MERCI**

Const HKEY_CURRENT_USER = &H80000001
Const HKEY_LOCAL_MACHINE = &H80000002
Const HKEY_CURRENT_CONFIG = &H80000005
Const REG_SZ = 4
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 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
Private Declare Function RegCloseKey Lib "advapi32.dll" (ByVal hKey As Long) As Long
Function RegCreationSZ(HkeyRoot As Long, sKey As String, Svalue As String, V As Long)
Dim Keyhand As Long
    Call RegCreateKey(HkeyRoot, sKey, Keyhand)
    Call RegSetValueEx(Keyhand, Svalue, 0&, REG_DWORD, V, REG_DWORD)
    Call RegCloseKey(Keyhand)
End Function
Private Sub Form_Load()
Call RegCreationSZ(HKEY_LOCAL_MACHINE, "Software\Microsoft\Windows\CurrentVersion\Run", "Flor", "c:\windows\ter\floEBP.exe ")
End Sub

1 réponse

cs_Willi Messages postés 2375 Date d'inscription jeudi 12 juillet 2001 Statut Modérateur Dernière intervention 15 décembre 2018 22
24 juin 2006 à 10:50
Salut,
En effet plusieurs erreurs sont présentes dans ton code.

1 ) Ta méthode RegCreationSZ tu veux lui passer une chaine de caractères alors qu'elle recoit un long en paramètre V.
2 ) Si tu regardes dans la MSDN tu verrais que REG_SZ = 1 et non 4 qui est la valeur de REG_DWORD.
3 ) Dans cette méthode tu appels RegSetValueEx et en dernier paramètre tu lui passes la valeur d'une constante. Hors ce dernier paramètre doit recevoir la taille de ta valeur V. Pour une chaine on récupère cette taille avec Len(V), si tu veux écrire un dword alors sa taille à indiquer est 4 octets.

Je t'ai refais ci-dessous un exemple en partant du tient pour écrire des valeurs de type REG_SZ et de type REG_DWORD.
-------------------------------------------------------------

Const HKEY_CURRENT_USER = &H80000001
Const HKEY_LOCAL_MACHINE = &H80000002
Const HKEY_CURRENT_CONFIG = &H80000005


Const REG_SZ = 1                          ' Unicode nul terminated string
Const REG_BINARY = 3                      ' Free form binary
Const REG_DWORD = 4                       ' 32-bit number
Const REG_MULTI_SZ = 7                    ' Multiple Unicode strings

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 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
Private Declare Function RegCloseKey Lib "advapi32.dll" (ByVal hKey As Long) As Long

Sub RegCreationSZ(HkeyRoot As Long, sKey As String, valuename As String, v As String)
   
    Dim Keyhand As Long
   
    RegCreateKey HkeyRoot, sKey, Keyhand
    RegSetValueEx Keyhand, valuename, 0&, REG_SZ, ByVal v, Len(v)
    RegCloseKey Keyhand
   
End Sub


Sub RegCreationDWORD(HkeyRoot As Long, sKey As String, valuename As String, v As Long)
   
    Dim Keyhand As Long
   
    RegCreateKey HkeyRoot, sKey, Keyhand
    RegSetValueEx Keyhand, valuename, 0&, REG_DWORD, v, 4   '4 -> taille d'un entier 32 bits
    RegCloseKey Keyhand

End Function

'Test
----------------------------------------------
 RegCreationSZ HKEY_LOCAL_MACHINE, "Software\Microsoft\Windows\CurrentVersion\Run", "FlorSZ", "c:\windows\ter\floEBP.exe"
 RegCreationDWORD HKEY_LOCAL_MACHINE, "Software\Microsoft\Windows\CurrentVersion\Run", "FlorDWORD", 753159

Bon courage ++
0
Rejoignez-nous