Strona: [  << <   1   > >>  ]  z  1     
Autor Temat: Problem z przy odczytywaniu wartosci z rejestru
lop



Typ: neutral
Postów: 9
Zarejestrowany: Sep 2004
Problem z przy odczytywaniu wartosci z rejestru

Witam! Tak więc mam taki problem, ktory najlepiej pokaze na przykladzie (zmieniony kod pochodzi z vb4all z działu API -> edycja rejsestru)

' Kod w module
Public hKey As Long
Declare Function RegCreateKeyEx Lib "advapi32.dll" Alias "RegCreateKeyExA" (ByVal hKey As Long, ByVal lpSubKey As String, ByVal Reserved As Long, ByVal lpClass As String, ByVal dwOptions As Long, ByVal samDesired As Long, ByVal lpSecurityAttributes As Long, phkResult As Long, lpdwDisposition 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 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 RegDeleteKey Lib "advapi32.dll" Alias "RegDeleteKeyA" (ByVal hKey As Long, ByVal lpSubKey As String) As Long
Declare Function RegCloseKey Lib "advapi32.dll" (ByVal hKey As Long) As Long
Declare Function RegFlushKey Lib "advapi32.dll" (ByVal hKey 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 RegDeleteValue Lib "advapi32.dll" Alias "RegDeleteValueA" (ByVal hKey As Long, ByVal lpValueName As String) As Long
Public Const SYNCHRONIZE = &H100000
Public Const ERROR_SUCCESS = 0&
Public Const STANDARD_RIGHTS_ALL = &H1F0000
Public Const READ_CONTROL = &H20000
Public Const STANDARD_RIGHTS_READ = (READ_CONTROL)
Public Const STANDARD_RIGHTS_WRITE = (READ_CONTROL)
Public Const KEY_CREATE_LINK = &H20
Public Const KEY_CREATE_SUB_KEY = &H4
Public Const KEY_ENUMERATE_SUB_KEYS = &H8
Public Const KEY_QUERY_VALUE = &H1
Public Const KEY_NOTIFY = &H10
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_EXECUTE = (KEY_READ)
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))
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 REG_CREATED_NEW_KEY = &H1
Public Const REG_OPENED_EXISTING_KEY = &H2
Public Const REG_OPTION_VOLATILE = 1
Public Const REG_OPTION_NON_VOLATILE = 0
Public Enum MainRoot
    HKEY_CLASSES_ROOT = &H80000000
    HKEY_CURRENT_CONFIG = &H80000005
    HKEY_CURRENT_USER = &H80000001
    HKEY_DYN_DATA = &H80000006
    HKEY_LOCAL_MACHINE = &H80000002
    HKEY_USERS = &H80000003
End Enum
Public Const REG_BINARY = 3
Public Const REG_DWORD = 4
Public Const REG_DWORD_LITTLE_ENDIAN = 4
Public Const REG_DWORD_BIG_ENDIAN = 5
Public Const REG_EXPAND_SZ = 2
Public Const REG_LINK = 6
Public Const REG_MULTI_SZ = 7
Public Const REG_NONE = 0
Public Const REG_RESOURCE_LIST = 8
Public Const REG_SZ = 1

Public Function OpenKey(Root As MainRoot, KeyPath As String, Optional KeyAccess As Long = KEY_ALL_ACCESS) As Boolean 'Procedura odczytująca wartość danego klucza z rejestru
    If RegOpenKeyEx(Root, KeyPath, 0, KeyAccess, hKey) = ERROR_SUCCESS Then OpenKey = True
End Function

Public Function QueryValue(Root As MainRoot, KeyPath As String, ValueName As String) As String
    Dim ret As String * 255
    OpenKey Root, KeyPath, KEY_QUERY_VALUE
    If RegQueryValueEx(hKey, ValueName, 0, REG_SZ, ByVal ret, 255) = ERROR_SUCCESS Then QueryValue = ret
End Function

' Kod formy
Private Sub Form_Load()
If QueryValue(HKEY_LOCAL_MACHINE, "software\Microsoft\Windows\CurrentVersion\Run", "jakis_program" = "jakas_sciezka_dostepu" Then MsgBox "Wartosc w rejestrze istnieje!"
End Sub

Kod ten sprawdza czy w rejestrze pod ścieżką "HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Run" istanieje wartosc o nazwie "jakis_program", ktora zawiera dane "jakas_sciezka_dostepu". Jesli istnieje wyswietli sie komunikat "Wartosc w rejestrze istnieje!". Problem tkwi w tym ze po wielu usiłowaniach, pomimo ze identyczna taka wartosc istnieje, nie pojawia mi sie ten Msgbox. Czy wie ktos jak ten problem usunąc? Prosze o pomoc i z gory thx

01-04-2005 09:27
Pokaż profil lop  Wyślij email do lop        3710088
HejHo




Typ: neutral
Postów: 66
Zarejestrowany: Apr 2003

Przerobiłem funkcję QueryValue tak aby (przynajmniej u mnie działała) i oto ona:
Public Function QueryValue(Root As MainRoot, KeyPath As String, ValueName As String) As String
    Dim ret As String * 255
    OpenKey Root, KeyPath, KEY_QUERY_VALUE
    If RegQueryValueEx(hKey, ValueName, 0, REG_SZ, ByVal ret, 255) = ERROR_SUCCESS Then
    x = InStr(1, ret, Chr(0))
    QueryValue = Left(ret, x - 1)
    End If
End Function


01-04-2005 10:52
Pokaż profil HejHo  Wyślij email do HejHo        6384729
lop



Typ: neutral
Postów: 9
Zarejestrowany: Sep 2004

o to mi chodziło! Teraz u mnie wszystko działa! Pozdro i thx za odp!

02-04-2005 13:46
Pokaż profil lop  Wyślij email do lop        3710088
Wszystkich odpowiedzi: 2 :: Maxymalnie na stronę: 20
Strona: [  << <   1   > >>  ]  z  1