Questo sito utilizza cookies solo per scopi di autenticazione sul sito e nient'altro. Nessuna informazione personale viene tracciata. Leggi l'informativa sui cookies.
Username: Password: oppure
Visual Basic 6 - Problema col Registry
Forum - Visual Basic 6 - Problema col Registry

Avatar
()
Newbie


Messaggi:
Iscritto:

Segnala al moderatore
Postato alle 1:00
Giovedì, 01/01/1970
Ciao a tutti ragazzi ho un problema nel creare chiavi  e valori nel registro di sistema...

Innanzi tutto vi posto il codice che ho usato:

Codice sorgente - presumibilmente VB.NET

  1. Public Const HKEY_CLASSES_ROOT = &H80000000
  2. Public Const HKEY_CURRENT_USER = &H80000001
  3. Public Const HKEY_LOCAL_MACHINE = &H80000002
  4. Public Const HKEY_USERS = &H80000003
  5. Public Const HKEY_PERFORMANCE_DATA = &H80000004
  6. Public Const ERROR_SUCCESS = 0&
  7.  
  8. Declare Function RegCloseKey Lib "advapi32.dll" (ByVal hKey As Long) As Long
  9. Declare Function RegCreateKey Lib "advapi32.dll" Alias "RegCreateKeyA" (ByVal hKey As Long, ByVal lpSubKey As String, phkResult As Long) As Long
  10. Declare Function RegDeleteKey Lib "advapi32.dll" Alias "RegDeleteKeyA" (ByVal hKey As Long, ByVal lpSubKey As String) As Long
  11. Declare Function RegDeleteValue Lib "advapi32.dll" Alias "RegDeleteValueA" (ByVal hKey As Long, ByVal lpValueName As String) As Long
  12. Declare Function RegOpenKey Lib "advapi32.dll" Alias "RegOpenKeyA" (ByVal hKey As Long, ByVal lpSubKey As String, phkResult As Long) As Long
  13. 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
  14. 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
  15. Public Const REG_SZ = 1                         ' Unicode nul terminated string
  16. Public Const REG_DWORD = 4                      ' 32-bit number
  17.  
  18. Public Sub savekey(hKey As Long, strPath As String)
  19. Dim keyhand&
  20. r = RegCreateKey(hKey, strPath, keyhand&)
  21. r = RegCloseKey(keyhand&)
  22. End Sub
  23.  
  24. Public Function getstring(hKey As Long, strPath As String, strValue As String)
  25.  
  26. Dim keyhand As Long
  27. Dim datatype As Long
  28. Dim lResult As Long
  29. Dim strBuf As String
  30. Dim lDataBufSize As Long
  31. Dim intZeroPos As Integer
  32. r = RegOpenKey(hKey, strPath, keyhand)
  33. lResult = RegQueryValueEx(keyhand, strValue, 0&, lValueType, ByVal 0&, lDataBufSize)
  34. If lValueType = REG_SZ Then
  35.     strBuf = String(lDataBufSize, " ")
  36.     lResult = RegQueryValueEx(keyhand, strValue, 0&, 0&, ByVal strBuf, lDataBufSize)
  37.     If lResult = ERROR_SUCCESS Then
  38.         intZeroPos = InStr(strBuf, Chr$(0))
  39.         If intZeroPos > 0 Then
  40.             getstring = Left$(strBuf, intZeroPos - 1)
  41.         Else
  42.             getstring = strBuf
  43.         End If
  44.     End If
  45. End If
  46. End Function
  47.  
  48.  
  49. Public Sub savestring(hKey As Long, strPath As String, strValue As String, strdata As String)
  50. Dim keyhand As Long
  51. Dim r As Long
  52. r = RegCreateKey(hKey, strPath, keyhand)
  53. r = RegSetValueEx(keyhand, strValue, 0, REG_DWORD, ByVal strdata, Len(strdata))
  54. r = RegCloseKey(keyhand)
  55. End Sub
  56.  
  57.  
  58. Function getdword(ByVal hKey As Long, ByVal strPath As String, ByVal strValueName As String) As Long
  59. Dim lResult As Long
  60. Dim lValueType As Long
  61. Dim lBuf As Long
  62. Dim lDataBufSize As Long
  63. Dim r As Long
  64. Dim keyhand As Long
  65.  
  66. r = RegOpenKey(hKey, strPath, keyhand)
  67.  
  68.  
  69. lDataBufSize = 4
  70.    
  71. lResult = RegQueryValueEx(keyhand, strValueName, 0&, lValueType, lBuf, lDataBufSize)
  72.  
  73. If lResult = ERROR_SUCCESS Then
  74.     If lValueType = REG_DWORD Then
  75.         getdword = lBuf
  76.     End If
  77. 'Else
  78. '    Call errlog("GetDWORD-" & strPath, False)
  79. End If
  80.  
  81. r = RegCloseKey(keyhand)
  82.    
  83. End Function
  84.  
  85. Function SaveDword(ByVal hKey As Long, ByVal strPath As String, ByVal strValueName As String, ByVal lData As Long)
  86.     Dim lResult As Long
  87.     Dim keyhand As Long
  88.     Dim r As Long
  89.     r = RegCreateKey(hKey, strPath, keyhand)
  90.     lResult = RegSetValueEx(keyhand, strValueName, 0&, REG_DWORD, lData, 4)
  91.     'If lResult <> error_success Then Call errlog("SetDWORD", False)
  92.     r = RegCloseKey(keyhand)
  93. End Function
  94.  
  95. Public Function DeleteKey(ByVal hKey As Long, ByVal strKey As String)
  96. Dim r As Long
  97. r = RegDeleteKey(hKey, strKey)
  98. End Function
  99.  
  100. Public Function DeleteValue(ByVal hKey As Long, ByVal strPath As String, ByVal strValue As String)
  101. Dim keyhand As Long
  102. r = RegOpenKey(hKey, strPath, keyhand)
  103. r = RegDeleteValue(keyhand, strValue)
  104. r = RegCloseKey(keyhand)
  105. End Function



per scrivere una chiave di valore DWORD ho usato questo comando:

Codice sorgente - presumibilmente Visual Basic 6

  1. Call SaveDword(HKEY_LOCAL_MACHINE, "Software\myapp", "miao", "fffff9d")




la chiave non viene creata... mi da un errore dicendo "Tipo non corrispondente" ho modificato il codice cambiando la variabile da long a string (vedere public sub "savedword" nel codice è long ma è per farvi capire) e le cose si sono aggiustate... la chiave e il valore vengono messi... solo che i dati del valore aggiunto (in questo caso "ffffff9d") non sono uguali.. e ne ho la dimostrazione creando manualmente la chiave con lo stesso valore e stessi dati...

Dov'è che sbaglio? Potete darmi una mano voi? grazie mille :D

PM Quote
Avatar
gantonio (Normal User)
Guru^2


Messaggi: 1532
Iscritto: 09/09/2007

Segnala al moderatore
Postato alle 17:07
Giovedì, 01/05/2008
(edit, ritiro la risposta ...)

Ultima modifica effettuata da gantonio il 01/05/2008 alle 17:08
PM Quote
Avatar
chen (Normal User)
Rookie


Messaggi: 54
Iscritto: 27/04/2008

Segnala al moderatore
Postato alle 17:36
Giovedì, 01/05/2008
ma esiste gia il percorso nel regedit HKEY_LOCAL_MACHINE\Software\myapp ? o ti aspetti che lo crei con il codice che hai provato?..se gia ce nn so che dirti altrmenti prova prima a creare manualmente il percorso e poi a vedere se il prgg ti aggiunge solo la chiave...:k:

Ultima modifica effettuata da chen il 01/05/2008 alle 17:37
PM Quote
Avatar
()
Newbie


Messaggi:
Iscritto:

Segnala al moderatore
Postato alle 23:41
Giovedì, 01/05/2008
si la cartella la crea da solo :D

solo che nn so come rendere i dati uguali..

potrei provare usando file batch.. ma diciamo che mi sta un po sullo zigomo 8-|8-|8-|

PM Quote
Avatar
chen (Normal User)
Rookie


Messaggi: 54
Iscritto: 27/04/2008

Segnala al moderatore
Postato alle 23:45
Giovedì, 01/05/2008
scusa se nn sono stato utile...cmq anche ha me ha sepre dato fastidio usare file batch esterni..ibfatti se lo devo fare uso sempre il comando shell per eseguire il comando in modalita nascosta..o al  massimo creo in quel momento il file batch lo avvio e poi lo elimino...per nn creare spazzatura sempre con shell..:)ti auguro di trovare al piu presto la soluzione:k::k:

PM Quote