Visual Basic Românesc  

 

Lista VB Ro
Carti
Surse
Articole
Programe
Legaturi
Pareri

 

 

 

Lucrul cu Registry-ul

       Pornesc de la ideea ca toata lumea stie ce este registry-ul si la ce foloseste el. Intrebarea este daca se poate citi sau scrie in registry din Visual Basic si daca da cum ? Evident raspunsul la prima intrebare este afirmativ si o sa incerc sa dau un exemplu concret. Din pacate Visual Basic nu are instructiuni directe pentru toate operatiile cu registry-ul pentru asta trebuind sa apelam la Windows API. Visual Basic-ul are instructiunile GetSetting, SaveSetting, DeleteSetting si GetAllSettings dar care lucreaza doar cu cheia HKEY_CURRENT_USER\Software\VB and VBA Program Settings in care se pot adauga/sterge/citi chei si valori. Pentru lucrul cu orice cheie din registry cititi mai jos.
       Creem un nou proiect in care intr-un modul scriem declaratiile constantelor si functiilor folosite

Public Const HKEY_CLASSES_ROOT = &H80000000
Public Const HKEY_CURRENT_USER = &H80000001
Public Const HKEY_LOCAL_MACHINE = &H80000002
Public Const HKEY_USERS = &H80000003
Public Const HKEY_PERFORMANCE_DATA = &H80000004

Public Const ERROR_SUCCESS = 0&
Public Const ERROR_NO_MORE_ITEMS = 259&
Public Const ERROR_INVALID_PARAMETER = 87&
Public Const ERROR_MORE_DATA = 234&

Declare Function RegCloseKey Lib "advapi32.dll" (ByVal hKey As Long) As Long
Declare Function RegCreateKey Lib "advapi32.dll" Alias "RegCreateKeyA" _
  (ByVal hKey As Long, ByVal lpSubKey As String, phkResult As Long) As Long
Declare Function RegDeleteKey Lib "advapi32.dll" Alias "RegDeleteKeyA" _
  (ByVal hKey As Long, ByVal lpSubKey As String) As Long
Declare Function RegDeleteValue Lib "advapi32.dll" Alias "RegDeleteValueA" _
  (ByVal hKey As Long, ByVal lpValueName As String) As Long
Declare Function RegOpenKey Lib "advapi32.dll" Alias "RegOpenKeyA" _
  (ByVal hKey As Long, ByVal lpSubKey As String, phkResult As Long) 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 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
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 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 RegEnumKey Lib "advapi32.dll" Alias "RegEnumKeyA" _
  (ByVal hKey As Long, ByVal dwIndex As Long, ByVal lpName As String, _
  ByVal cbName As Long) As Long
Declare Function RegEnumKeyEx Lib "advapi32.dll" Alias "RegEnumKeyExA" _
  (ByVal hKey As Long, ByVal dwIndex As Long, ByVal lpName As String, lpcbName As Long, _
  lpReserved As Long, ByVal lpClass As String, lpcbClass As Long, _
  lpftLastWriteTime As FILETIME) As Long
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

Public Const REG_SZ = 1                         ' Unicode nul terminated string
Public Const REG_EXPAND_SZ = 2                  ' Unicode nul terminated string
Public Const REG_BINARY = 3                     ' Free form binary
Public Const REG_DWORD = 4                      ' 32-bit number
Public Const REG_DWORD_BIG_ENDIAN = 5           ' 32-bit number
Public Const REG_DWORD_LITTLE_ENDIAN = 4        ' 32-bit number (same as REG_DWORD)
Public Const REG_LINK = 6                       ' Symbolic Link (unicode)
Public Const REG_MULTI_SZ = 7                   ' Multiple Unicode strings

Public Type FILETIME 		'Folosit la unele functii = data ultimei operatii cu cheia
        dwLowDateTime As Long
        dwHighDateTime As Long
End Type

       In continuare vom analiza pe rind cautarea unei chei/valori, crearea unei chei/valori, stergerea unei chei/valori si informatii despre o cheie/valoare (pt. fiecare din aceste operatii pun cite un buton de comanda pe forma si realizez comanda pe evenimentul click)

  1. Primul lucru pentru orice operatie cu registry-ul este deschiderea unei chei. Aceasta se face cu functia RegOpenKey sau cu RegOpenKeyEx (pt. a doua functie furnizam in plus o masca de securitate a cheii deschise) ca mai jos:
    
    Dim r as Long
    Dim keyhand as Long
    
    r = RegOpenKey(HKEY_CURRENT_USER, _
      "Software\Microsoft\Windows\CurrentVersion\Explorer", keyhand)
    
    sau pt. a doua varianta
    
    Public 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))
    Public Const KEY_CREATE_LINK = &H20
    Public Const KEY_CREATE_SUB_KEY = &H4
    Public Const KEY_ENUMERATE_SUB_KEYS = &H8
    Public Const KEY_EVENT = &H1     '  Event contains key event record
    Public Const KEY_EXECUTE = ((KEY_READ) And (Not SYNCHRONIZE))
    Public Const KEY_EXECUTE = (KEY_READ)
    Public Const KEY_NOTIFY = &H10
    Public Const KEY_QUERY_VALUE = &H1
    Public Const KEY_READ = ((STANDARD_RIGHTS_READ Or KEY_QUERY_VALUE Or _
      KEY_ENUMERATE_SUB_KEYS Or KEY_NOTIFY) And (Not SYNCHRONIZE))
    Public Const KEY_SET_VALUE = &H2
    Public Const KEY_WRITE = ((STANDARD_RIGHTS_WRITE Or KEY_SET_VALUE Or _
      KEY_CREATE_SUB_KEY) And (Not SYNCHRONIZE))
    
    Dim r as Long
    Dim keyhand as Long
    
    r = RegOpenKeyEx(HKEY_CURRENT_USER, "Software\Microsoft\Windows\CurrentVersion\Explorer",_
      0&, KEY_ENUMERATE_SUB_KEYS, keyhand)
    
    In continuare lucram cu identificatorul keyhand returnat de aceste functii.

  2. Inchiderea unei chei se face cu functia RegCloseKey ca in exemplu de mai jos:
    
    Dim r as Long
    
    r = RegCloseKey(keyhand) 
    
    unde keyhand este identificatorul cheii returnat de RegOpenKey sau RegOpenKeyEx.
    Aceste functii nu creaza o subcheie daca aceasta nu exista. Pentru creerea unei subchei vezi mai jos.

  3. Cautarea unei chei sau a unei valori - Pentru parcurgerea tuturor subcheilor pentru o cheie data si afisarea lor se face folosind functia API Windows RegEnumKeyEx sau RegEnumKey a doua returnind in afara de numele cheii mai putine informatii (fara informatii despre clasa cheii si data ultimei operatii cu cheia):
    
    Dim t$				'in t$ obtinem la final toate subcheile
    				'declarat global la nivel de forma
    
    Private Sub cmdRegEnumKeyEx_Click()
    Dim keyhand As Long		'identificatorul cheii
    Dim dt As FILETIME		'returnat de functie
    Dim lClassName As Long		'lungimea clasei cheii returnate de functie
    Dim ClassName As String		'numele clasei cheii returnate de functie
    Dim strBuf As String * 256	'bufer pt. numele cheii returnate
    
    'deschidem subcheia - in ex. nostru 
    'HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Explorer
    r = RegOpenKey(HKEY_CURRENT_USER, _
      "Software\Microsoft\Windows\CurrentVersion\Explorer", keyhand)
    i = 0& 	'numarul cheii care ne intereseaza
    t$ = ""
    Do	'parcurgem toate cheile
    'apelul functiei API Windows pt. a gasi numele subcheii a i-a
    r = RegEnumKeyEx(keyhand, i, strBuf, 256, 0&, ClassName, lClassName, dt)
    'unde 	keyhand=identificatorul returnat la deschiderea cheii
    '	i = a cita cheie ne intereseaza
    '	strBuf = buferul in care returneaza numele cheii
    '	256 = lungimea buferului
    '	0& = rezervat (?)
    '	ClassName = numele clasei returnate
    '	lClassName = lungimea clasei returnate
    '	dt = structura de tip FILETIME
    ' pt. RegEnumKey se scrie: r = RegEnumKey(keyhand, i, strBuf, 256)
    'r o sa contina dupa apel un cod de succes sau insucces
    If r = ERROR_INVALID_PARAMETER Then MsgBox "Parametru invalid"
    'daca totul a fost OK (r=ERROR_SUCCES) pun in t$ valoarea gasita 
    'din strBuf (numele subcheii este completat in bufer cu caracterul cu codul 0)
    If r = ERROR_SUCCESS Then t$ = t$ & Left(strBuf, InStr(1, strBuf, Chr(0&)) - 1) & _
      vbCrLf 'iau din bufer pina la caracterul cu codul 0
    i = i + 1	'trec la urmatoarea subcheie
    DoEvents	'citesc alte evenimente
    'daca RegEnumKeyEx a returnat in r codul ERROR_NO_MORE_ITEMS inseamna ca 
    'nu mai sint alte subchei si termina ciclul
    Loop Until r = ERROR_NO_MORE_ITEMS
    MsgBox t$
    r = RegCloseKey(keyhand) 	'inchide subcheia
    End Sub
    
    Pentru parcurgerea tuturor valorilor pentru o cheie data si afisarea numelor lor se face folosind functia API Windows RegEnumValue:
    
    Private Sub cmdRegEnumValue_Click()
    Dim keyhand As Long		'identificatorul cheii
    Dim strBuf As String * 256	'bufer pt. numele cheii returnate
    Dim lData as Long
    Dim lType as Long
    Dim byteData as Byte
    
    'deschidem subcheia - in ex. nostru 
    'HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Explorer
    r = RegOpenKey(HKEY_CURRENT_USER, _
      "Software\Microsoft\Windows\CurrentVersion\Explorer", keyhand)
    i = 0& 	'numarul valorii care ne intereseaza
    t$ = ""
    Do	'parcurgem toate valorile
    'apelul functiei API Windows pt. a gasi numele valotii a i-a
    r = RegEnumValue(keyhand, i, strBuf, 256, 0&, lType, byteData, lData)
    'unde 	keyhand=identificatorul returnat la deschiderea cheii
    '	i = a cita cheie ne intereseaza
    '	strBuf = buferul in care returneaza numele cheii
    '	256 = lungimea buferului
    '	0& = rezervat (?)
    'r o sa contina dupa apel un cod de succes sau insucces
    If r = ERROR_INVALID_PARAMETER Then MsgBox "Parametru invalid"
    'daca mai sint si alte valori mai citeste
    If r = ERROR_MORE_DATA Then r = RegEnumValue(keyhand, i, strBuf, MAX_KEY_NAME, _
      0&, lType, byteData, lData)
    'daca totul a fost OK (r=ERROR_SUCCES) pun in t$ valoarea gasita din strBuf 
    '(numele subcheii este completat in bufer cu caracterul cu codul 0)
    If r = ERROR_SUCCESS Then t$ = t$ & Left(strBuf, InStr(1, strBuf, Chr(0&)) - 1) & _
      vbCrLf 'iau din bufer pina la caracterul cu codul 0
    i = i + 1	'trec la urmatoarea valoare
    DoEvents	'citesc alte evenimente
    'daca RegEnumValue a returnat in r codul ERROR_NO_MORE_ITEMS inseamna ca nu mai sint 
    'alte valori si termina ciclul
    Loop Until r = ERROR_NO_MORE_ITEMS
    MsgBox t$
    r = RegCloseKey(keyhand) 	'inchide subcheia
    End Sub
    
  4. Pt. a citi informatii despre o cheie si valorile ei se foloseste functia RegQueryInfoKey. Informatiile care le putem afla sint: clasa cheii, lungimea celei mai lungi clase, numarul de subchei, lungimea celei mai lungi subchei, numarul de valori, lungimea maxima a numelor valorilor, lungimea maxima pt. datele valorilor, un descriptor de securitate si data unltimei scrieri a cheii.
    
    Private Sub cmdRegQueryInfoKey_Click()
    Dim keyhand As Long
    Dim lClassName As Long, lNrKeys As Long, lMaxLungKey As Long
    Dim lMaxLungClass As Long, lNrValues As Long, lMaxLungValue As Long
    Dim lMaxData As Long, lSecurity As Long
    Dim dt As FILETIME
    Dim ClassName As String
    r = RegOpenKey(HKEY_CURRENT_USER, "Software\Microsoft\Windows\CurrentVersion\Explorer", keyhand)
    r = RegQueryInfoKey(keyhand, ClassName, lClassName, 0&, lNrKeys, lMaxLungKey, _
      lMaxLungClass, lNrValues, lMaxLungValue, lMaxData, lSecurity, dt)
    If r = ERROR_INVALID_PARAMETER Then MsgBox "Parametru invalid"
    If r = ERROR_SUCCESS Then
    MsgBox "Nr. chei: " & lNrKeys & vbCrLf & "Max. cheie : " & lMaxLungKey & vbCrLf & _
            "Nr. valori : " & lNrValues & vbCrLf & "Max. valoare : " & lMaxLungValue & vbCrLf & _
            "Max. val. data : " & lMaxData
    End If
    r = RegCloseKey(keyhand)
    End Sub
    
  5. In continuare o sa prezint citirea datelor corespunzatoare unei valori din registry (de sub o anume cheie). Dupa cum stiti datele pot fi de mai multe tipuri si anume REG_SZ sau REG_EXPAND_SZ (de tip string Unicode), REG_BINARY (tip binar), REG_DWORD, REG_DWORD_BIG_ENDIAN, REG_DWORD_LITTLE_ENDIAN (intreg pe 32 de biti) sau REG_LINK (legatura simbolica).
    Voi crea functiile RegCitesteString, RegCitesteDWord si RegCitesteBinary pentru citirea unor date de tip string, intreg pe 32 biti (dword) sau binar. Parametrii pentru fiecare functie sint cheia sub care se afla subcheia, subcheia sub care se afla valoarea si numele valorii, functia returnind datele de sub acesta valoare.
    Pentru toate aceste functii folosesc functia Windows API RegQueryValueEx care pentru un identificator de cheie deschisa citeste intr-un bufer datele de sub o valoare data, iar in functie de tipul datelor fac anumite prelucrari.
    
    Public Function RegCitesteString(Hkey As Long, strPath As String, strValue As String)
    Dim keyhand As Long
    Dim datatype As Long
    Dim lResult As Long
    Dim strBuf As String
    Dim lDataBufSize As Long
    Dim intZeroPos As Integer
    r = RegOpenKey(Hkey, strPath, keyhand)	'deschide cheia
    'citesc datele pt. a determina tipul datelor
    lResult = RegQueryValueEx(keyhand, strValue, 0&, lValueType, ByVal 0&, lDataBufSize)
    If lValueType = REG_SZ Then 'daca este de tip string
        strBuf = String(lDataBufSize, " ")   'pregatesc buferul de raspuns
        ' citesc efectiv datele pt. valoarea data
        lResult = RegQueryValueEx(keyhand, strValue, 0&, 0&, ByVal strBuf, lDataBufSize)
        If lResult = ERROR_SUCCESS Then 'daca totul a fost ok returnez valoarea din bufer
            intZeroPos = InStr(strBuf, Chr$(0))
            If intZeroPos > 0 Then
                RegCitesteString = Left$(strBuf, intZeroPos - 1)
            Else
                RegCitesteString = strBuf
            End If
        End If
    End If
    r = RegCloseKey(keyhand) 'inchide cheia
    End Function
    
    Function RegCitesteDWord(ByVal Hkey As Long, ByVal strPath As String, _
       ByVal strValueName As String) As Long
    Dim lResult As Long
    Dim lValueType As Long
    Dim lBuf As Long
    Dim lDataBufSize As Long
    Dim r As Long
    Dim keyhand As Long
    r = RegOpenKey(Hkey, strPath, keyhand)  'deschide cheia
    lDataBufSize = 4	'pregatesc buferul pt. dword
    'citesc efectiv datele
    lResult = RegQueryValueEx(keyhand, strValueName, 0&, lValueType, lBuf, lDataBufSize)
    if lResult = ERROR_SUCCESS Then 'daca a fost ok
        If lValueType = REG_DWORD Then    	'...testez daca data este de tip dword
            RegCitesteDWord = lBuf		'returneaza valoarea din bufer
        End If
    End If
    r = RegCloseKey(keyhand) 'inchide cheia
    End Function
    
    Public Function RegCitesteBinary(Hkey As Long, strPath As String, strValue As String)
    Dim keyhand As Long
    Dim datatype As Long
    Dim lResult As Long
    Dim strBuf As String, sBin As String, sTemp As String
    Dim lDataBufSize As Long, iCount As Long
    Dim intZeroPos As Integer
    Dim cTemp As Byte
    Dim Digit As Integer
    r = RegOpenKey(Hkey, strPath, keyhand) 'deschide cheia
    'citesc tipul datelor si lungimea lor
    lResult = RegQueryValueEx(keyhand, strValue, 0&, lValueType, ByVal 0&, lDataBufSize)
    If lValueType = REG_BINARY Then 		'daca datele sint de tip binary
        strBuf = String(lDataBufSize + 2, " ")	'pregatesc buferul
        ' citesc efectiv datele in bufer
        lResult = RegQueryValueEx(keyhand, strValue, 0&, REG_BINARY, ByVal strBuf, lDataBufSize)
        If lResult = ERROR_SUCCESS Then		'prelucrez datele din bufer
          sTemp = ""
          For iCount = lDataBufSize To 1 Step -1
           cTemp = Asc(Mid(strBuf, iCount, 1))
           While cTemp > 0
             Digit = cTemp Mod 2
             If Digit Then
               sBin = "1" & sBin
             Else
               sBin = "0" & sBin
             End If
             cTemp = (cTemp - Digit) / 2
           Wend
           If Len(sBin) < 8 Then
              sBin = "0000000" & sBin
              sBin = Right(sBin, Len(sBin) - (Len(sBin) Mod 8))
           End If
           sTemp = sTemp & sBin
           sBin = ""
          Next iCount
          If Len(sTemp) = 0 Then sTemp = "00000000"
    End If
    End If
    RegCitesteBinary = sTemp 'returneaza valoarea gasita
    r = RegCloseKey(keyhand) 'inchide cheia
    End Function
    
    Apelarea acestor functii se face simplu, ca mai jos:
    
    'citesc tips-ul activ ...
    NumarTips$ = CStr(Dec(RegCitesteBinary(HKEY_CURRENT_USER, _
      "Software\Microsoft\Windows\CurrentVersion\Explorer\Tips", "Next")))
    ' ... si il afisez
    Tips$ = RegCitesteString(HKEY_LOCAL_MACHINE, _
      "SOFTWARE\Microsoft\Windows\CurrentVersion\explorer\Tips", NumarTips$ )
    ' citesc numarul de zile pt. history pt. IE
    NumarZileHistoryIE = RegCitesteDWord(HKEY_CURRENT_USER, _
      "Software\Microsoft\Windows\CurrentVersion\Internet Settings\Url History", "DaysToKeep")
    
    unde am folosit o functie Dec() pt. conversie din string binar in numar in baza 10.
    
    Public Function Dec(s As String) As Long
    Dim i As Integer
    Dim temp As Long
    Dim p As Long
    temp = 0
    p = 1
    For i = Len(s) To 1 Step -1
    temp = temp + Val(Mid(s, i, 1)) * p
    p = p * 2
    Next
    Dec = temp
    End Function
    
    Problema este ca dvs. ar trebui sa stiti inainte ce fel de tip are valoarea care vreti sa o cititi din registry si sa o cititi cu functia corespunzatoare.

  6. Pentru scrierea unei valori in registry voi prezenta 3 proceduri (RegScrieString, RegScrieDWord si RegScrieBinary) dupa acelasi principiu ca si la citirea valorilor.
    
    Public Sub RegScrieString(Hkey As Long, strPath As String, strValue As String, strdata As String)
    Dim keyhand As Long
    Dim r As Long
    r = RegCreateKey(Hkey, strPath, keyhand) 'daca nu exista cheia o creaza
    'scrie efectiv in registry (daca nu exista o creaza)
    r = RegSetValueEx(keyhand, strValue, 0, REG_SZ, ByVal strdata, Len(strdata))
    r = RegCloseKey(keyhand)	'inchide cheia
    End Sub
    
    Public Sub RegScrieDword(ByVal Hkey As Long, ByVal strPath As String, _
      ByVal strValueName As String, ByVal lData As Long)
        Dim lResult As Long
        Dim keyhand As Long
        Dim r As Long
        r = RegCreateKey(Hkey, strPath, keyhand) 'deschide cheia
        'scrie valoarea (daca nu exista o creaza)
        lResult = RegSetValueEx(keyhand, strValueName, 0&, REG_DWORD, lData, 4)
        r = RegCloseKey(keyhand)  ' inchide cheia
    End Sub
    
    Public Sub RegScrieBinary(ByVal Hkey As Long, ByVal strPath As String, _
      ByVal strValueName As String, strData As String)
        Dim lResult As Long
        Dim keyhand As Long
        Dim r As Long
        r = RegCreateKey(Hkey, strPath, keyhand) 'deschide cheia
        'scrie in registry ca si valoare binara (daca nu exista cheia o creaza)
        r = RegSetValueEx(keyhand, strValueName, 0, REG_BINARY, ByVal strData, Len(strData))
        r = RegCloseKey(keyhand)  'inchide cheia
    End Sub
    
    Un exemplu de utilizare ar fi urmatorul:
    
    'creez tipsul cu numarul 100...
    Call RegScrieString(HKEY_LOCAL_MACHINE, "SOFTWARE\Microsoft\Windows\CurrentVersion\explorer\Tips", _
      "100", "Tips definit de mine - un text pt. teste") 'tips-ul cu numarul 100
    '... si il fac activ
    Call RegScrieBinary(HKEY_CURRENT_USER, _
      "Software\Microsoft\Windows\CurrentVersion\Explorer\Tips", "Next", chr$(100))
    'Modific nr. de zile pt. history la IE la 10 zile
    Call RegScrieDWord(HKEY_CURRENT_USER, _
      "Software\Microsoft\Windows\CurrentVersion\Internet Settings\Url History", "DaysToKeep", 10)
    
  7. Pentru a creea subchei pt. o cheie data se foloseste functia RegCreateKey (cu varianta RegCreateKeyEx pt. specificarea in plus si a altor date). Iata un exemplu:
    
    Dim r as Long
    Dim keyhand as Long
    r = RegCreateKey(HKEY_CURRENT_USER, _
      "Software\Microsoft\Windows\CurrentVersion\Explorer\Tips2", keyhand)
    r = RegCloseKey(keyhand)
    
    Aceasta functie functioneaza si ca RegOpenKey doar ca in plus daca cheia solicitata nu exista o creaza mai intii dupa care se poate folosi identificatorul keyhand returnat.

  8. Urmatorul lucru este stergerea unei chei sau a unei valori din registry. Pentru asta folosim functiile RegDeleteKey si RegDeleteValue. De asemenea creez 2 proceduri care realizeaza acest lucru.
    
    Public Sub RegStergeCheie(ByVal Hkey As Long, ByVal strKey As String)
    Dim r As Long
    r = RegDeleteKey(Hkey, strKey)		' strKey = cheia de sters
    End Sub
    
    Public Sub RegStergeValoare(ByVal Hkey As Long, ByVal strPath As String, ByVal strValue As String)
    Dim keyhand As Long
    Dim r As Long
    r = RegOpenKey(Hkey, strPath, keyhand)
    r = RegDeleteValue(keyhand, strValue)	' strValue = valoarea de sters
    r = RegCloseKey(keyhand)
    End Sub
    
    si se utilizeaza astfel:
    
    Call RegStergeValoare(HKEY_LOCAL_MACHINE, _
      "SOFTWARE\Microsoft\Windows\CurrentVersion\explorer\Tips", "100") ' sterg tips-ul cu numarul 100
    Call RegStergeCheie(HKEY_CURRENT_USER, "Software\Microsoft\Windows\CurrentVersion\Explorer\Tips2")
    
    Asta ar fi in mare operatiunile de baza cu registry-ul desi mai exista si altele cum ar fi: salvarea si restaurarea registry-ului, conectarea la alt registry, notificari ale schimbarii atributelor, etc. In plus, aici nu am atins problema atributelor de securitate si nici a altor optiuni dar cred ca este suficient pt. a utiliza in programe majoritatea operatiunilor cu registry-ul.

    Inapoi la articole

Caut cu FreeFind

Doar in VB_Ro
Intreg Web-ul

 

 

 

 


1