Oppure

Loading
01/05/08 13:02
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:

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&

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 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
Public Const REG_SZ = 1                         ' Unicode nul terminated string
Public Const REG_DWORD = 4                      ' 32-bit number

Public Sub savekey(hKey As Long, strPath As String)
Dim keyhand&
r = RegCreateKey(hKey, strPath, keyhand&)
r = RegCloseKey(keyhand&)
End Sub

Public Function getstring(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)
lResult = RegQueryValueEx(keyhand, strValue, 0&, lValueType, ByVal 0&, lDataBufSize)
If lValueType = REG_SZ Then
    strBuf = String(lDataBufSize, " ")
    lResult = RegQueryValueEx(keyhand, strValue, 0&, 0&, ByVal strBuf, lDataBufSize)
    If lResult = ERROR_SUCCESS Then
        intZeroPos = InStr(strBuf, Chr$(0))
        If intZeroPos > 0 Then
            getstring = Left$(strBuf, intZeroPos - 1)
        Else
            getstring = strBuf
        End If
    End If
End If
End Function


Public Sub savestring(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)
r = RegSetValueEx(keyhand, strValue, 0, REG_DWORD, ByVal strdata, Len(strdata))
r = RegCloseKey(keyhand)
End Sub


Function getdword(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)


lDataBufSize = 4
    
lResult = RegQueryValueEx(keyhand, strValueName, 0&, lValueType, lBuf, lDataBufSize)

If lResult = ERROR_SUCCESS Then
    If lValueType = REG_DWORD Then
        getdword = lBuf
    End If
'Else
'    Call errlog("GetDWORD-" & strPath, False)
End If

r = RegCloseKey(keyhand)
    
End Function

Function SaveDword(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)
    lResult = RegSetValueEx(keyhand, strValueName, 0&, REG_DWORD, lData, 4)
    'If lResult <> error_success Then Call errlog("SetDWORD", False)
    r = RegCloseKey(keyhand)
End Function

Public Function DeleteKey(ByVal hKey As Long, ByVal strKey As String)
Dim r As Long
r = RegDeleteKey(hKey, strKey)
End Function

Public Function DeleteValue(ByVal hKey As Long, ByVal strPath As String, ByVal strValue As String)
Dim keyhand As Long
r = RegOpenKey(hKey, strPath, keyhand)
r = RegDeleteValue(keyhand, strValue)
r = RegCloseKey(keyhand)
End Function


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

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
01/05/08 15:07
gantonio
(edit, ritiro la risposta ...)
Ultima modifica effettuata da gantonio 01/05/08 15:08
aaa
01/05/08 15:36
chen
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 01/05/08 15:37
aaa
01/05/08 21:41
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-|
01/05/08 21:45
chen
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:
aaa