Avatar billede angelenglen Nybegynder
22. maj 2004 - 21:23 Der er 6 kommentarer og
1 løsning

Hjælp med Registry Funktion

Jeg bruger et modul der giver mig adgang til registry.

Jeg bruger så den funktion deri der hedder QueryValue:

  ' Example:
  '
  '  QueryValue HKEY_CURRENT_USER, "TestKey\\SubKey1", "StringValue"
  '
  '  - displays a message box with the current setting of the "StringValue"
  '    value, and assumes that "StringValue" exists in the "TestKey\\SubKey1" key.
  '    If the Value that you query does not exist, then QueryValue returns an
  '    error code of 2 - 'ERROR_BADKEY'.

Der står at hvis value ikke eksisterer, så retunerer den en error code of 2 - 'ERROR_BADKEY'

-hvordan checker jeg for den errorcode?

Nogen der kan skrive et lille eksempel der får en lille msgbox komme, hvis den forespurgte key ikke eksisterer i registry?

jeg har prøvet følgende uden held:


        RegValue = QueryValue(HKEY_LOCAL_MACHINE, "SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run", "NonExistingKey")
            If RegValue = 2 Then MsgBox "key does not exist" Else MsgBox "The key exists and is: " & RegValue







-her er hele modulet uredigeret:


'-----------------------Registry_stuff.bas-----------------------
Option Explicit

Global Const REG_SZ As Long = 1
Global Const REG_DWORD As Long = 4

Global Const HKEY_CLASSES_ROOT = &H80000000
Global Const HKEY_CURRENT_CONFIG = &H80000005
Global Const HKEY_CURRENT_USER = &H80000001
Global Const HKEY_DYN_DATA = &H80000006
Global Const HKEY_LOCAL_MACHINE = &H80000002
Global Const HKEY_PERFORMANCE_DATA = &H80000004
Global Const HKEY_USERS = &H80000003

Public Type SECURITY_ATTRIBUTES
        nLength As Long
        lpSecurityDescriptor As Long
        bInheritHandle As Long
End Type
Private Type FILETIME
        dwLowDateTime As Long
        dwHighDateTime As Long
End Type
Public Type ACL
        AclRevision As Byte
        Sbz1 As Byte
        AclSize As Integer
        AceCount As Integer
        Sbz2 As Integer
End Type

Public Type SECURITY_DESCRIPTOR
        Revision As Byte
        Sbz1 As Byte
        Control As Long
        Owner As Long
        Group As Long
        Sacl As ACL
        Dacl As ACL
End Type

Global Const ERROR_NONE = 0
Global Const ERROR_BADDB = 1
Global Const ERROR_BADKEY = 2
Global Const ERROR_CANTOPEN = 3
Global Const ERROR_CANTREAD = 4
Global Const ERROR_CANTWRITE = 5
Global Const ERROR_OUTOFMEMORY = 6
Global Const ERROR_INVALID_PARAMETER = 7
Global Const ERROR_ACCESS_DENIED = 8
Global Const ERROR_INVALID_PARAMETERS = 87
Global Const ERROR_NO_MORE_ITEMS = 259

Global Const KEY_ALL_ACCESS = &H3F

Global Const REG_OPTION_NON_VOLATILE = 0




Public Declare Function RegOpenKey Lib "advapi32.dll" Alias "RegOpenKeyA" (ByVal hKey As Long, ByVal lpSubKey As String, phkResult As Long) As Long
Public Declare Function RegConnectRegistry Lib "advapi32.dll" Alias "RegConnectRegistryA" (ByVal lpMachineName As String, ByVal hKey As Long, phkResult As Long) As Long
Public Declare Function RegDeleteKey Lib "advapi32.dll" Alias "RegDeleteKeyA" (ByVal hKey As Long, ByVal lpSubKey As String) As Long
Public Declare Function RegDeleteValue Lib "advapi32.dll" Alias "RegDeleteValueA" (ByVal hKey As Long, ByVal lpValueName As String) As Long
Public Declare Function RegCloseKey Lib "advapi32.dll" (ByVal hKey As Long) As Long
Public 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
Public 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
Public Declare Function RegQueryValueExString Lib "advapi32.dll" Alias "RegQueryValueExA" (ByVal hKey As Long, ByVal lpValueName As String, ByVal lpReserved As Long, lpType As Long, ByVal lpData As String, lpcbData As Long) As Long
Public Declare Function RegQueryValueExLong Lib "advapi32.dll" Alias "RegQueryValueExA" (ByVal hKey As Long, ByVal lpValueName As String, ByVal lpReserved As Long, lpType As Long, lpData As Long, lpcbData As Long) As Long
Public Declare Function RegQueryValueExNULL Lib "advapi32.dll" Alias "RegQueryValueExA" (ByVal hKey As Long, ByVal lpValueName As String, ByVal lpReserved As Long, lpType As Long, ByVal lpData As Long, lpcbData As Long) As Long
Public Declare Function RegSetValueExString Lib "advapi32.dll" Alias "RegSetValueExA" (ByVal hKey As Long, ByVal lpValueName As String, ByVal Reserved As Long, ByVal dwType As Long, ByVal lpValue As String, ByVal cbData As Long) As Long
Public Declare Function RegSetValueExLong Lib "advapi32.dll" Alias "RegSetValueExA" (ByVal hKey As Long, ByVal lpValueName As String, ByVal Reserved As Long, ByVal dwType As Long, lpValue As Long, ByVal cbData As Long) As Long
Public Declare Function RegEnumKey Lib "advapi32.dll" Alias "RegEnumKeyA" (ByVal hKey As Long, ByVal dwIndex As Long, ByVal lpName As String, cbName As Long) As Long
Public 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, ByRef lpftLastWriteTime As Long) As Long
Public 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 Declare Function RegFlushKey Lib "advapi32.dll" (ByVal hKey As Long) As Long
Public Declare Function RegGetKeySecurity Lib "advapi32.dll" (ByVal hKey As Long, ByVal SecurityInformation As Long, pSecurityDescriptor As SECURITY_DESCRIPTOR, lpcbSecurityDescriptor As Long) As Long


Public Sub DeleteKey(ByVal hKey As Long, ByVal strPath As String)
'Syntax:
'DeleteKey HKEY_LOCAL_MACHINE, "Subkey\\Subkey2"

Dim lRegResult As Long

lRegResult = RegDeleteKey(hKey, strPath)

End Sub
Public Sub DeleteValue(ByVal hKey As Long, ByVal strPath As String, ByVal strValue As String)
'Syntax
'DeleteValue HKEY_LOCAL_MACHINE, "Subkey\\Subkey2", "Value"
Dim hCurKey As Long
Dim lRegResult As Long

lRegResult = RegOpenKey(hKey, strPath, hCurKey)

lRegResult = RegDeleteValue(hCurKey, strValue)

lRegResult = RegCloseKey(hCurKey)

End Sub

Public Sub CreateNewKey(sNewKeyName As String, lPredefinedKey As Long)
  '
  ' Creating A New Registry Key
  '
  ' CreateNewKey takes the name of the key to create, and the constant
  ' representing the predefined key to create the key under. The call
  ' to RegCreateKeyEx doesn't take advantage of the security mechanisms.
  '
  ' Examples:
  '
  '  CreateNewKey "TestKey", HKEY_CURRENT_USER
  '
  '  - will create a key called TestKey immediately under HKEY_CURRENT_USER.
  '
  '  CreateNewKey "TestKey\\SubKey1\\SubKey2", HKEY_LOCAL_MACHINE
  '
  '  - will create three-nested keys beginning with TestKey immediately under
  '    HKEY_LOCAL_MACHINE, SubKey1 subordinate to TestKey, and SubKey3 under SubKey2.
  '
  Dim hNewKey As Long        'handle to the new key
  Dim lRetVal As Long        'result of the RegCreateKeyEx function
  lRetVal = RegCreateKeyEx(lPredefinedKey, sNewKeyName, 0&, vbNullString, REG_OPTION_NON_VOLATILE, KEY_ALL_ACCESS, 0&, hNewKey, lRetVal)
  RegCloseKey (hNewKey)
End Sub

Public Sub SetKeyValue(lPredefinedKey As Long, sKeyName As String, sValueName As String, vValueSetting As Variant, lValueType As Long)
  '
  ' Setting / Modifying a Registry Value
  '
  ' SetKeyValue takes the key that the value will be associated with, the
  ' name of the value, the setting of the value, and the type of the value
  ' (the SetValueEx function only supports REG_SZ and REG_DWORD,  but this
  ' can be modified if necessary). Specifying a new value for an existing
  ' sValueName modifies the current setting of that value.
  '
  ' Example:
  '
  '  SetKeyValue HKEY_CURRENT_USER, "TestKey\\SubKey1", "StringValue", "Hello", REG_SZ
  '
  '  - creates a value of type REG_SZ called "SubKey1" with the setting of "Hello".
  '    This value is associated with the key SubKey1 of "TestKey."  In this case,
  '    "TestKey" is a subkey of HKEY_CURRENT_USER. This call fails if "TestKey\\SubKey1"
  '    does not exist. To avoid this problem, use a call to RegCreateKeyEx instead
  '    of a call to RegOpenKeyEx. RegCreateKeyEx opens a specified key if it already
  '    exists.
  '
  Dim lRetVal As Long        'result of the SetValueEx function
  Dim hKey As Long            'handle of open key
  ' open the specified key
  lRetVal = RegOpenKeyEx(lPredefinedKey, sKeyName, 0, KEY_ALL_ACCESS, hKey)
  lRetVal = SetValueEx(hKey, sValueName, lValueType, vValueSetting)
  RegCloseKey (hKey)
End Sub
 
Public Function QueryValue(lPredefinedKey As Long, sKeyName As String, sValueName As String) As Variant
  '
  ' Querying a Registry Value
  '
  ' The next procedure can be used to ascertain the setting of an existing value.
  ' QueryValue takes the name of the key and the name of a value associated with
  ' that key and displays a message box with the corresponding value. It uses a
  ' call to the QueryValueEx wrapper function defined below, which supports only '
  ' REG_SZ and REG_DWORD types:
  '
  ' Example:
  '
  '  QueryValue HKEY_CURRENT_USER, "TestKey\\SubKey1", "StringValue"
  '
  '  - displays a message box with the current setting of the "StringValue"
  '    value, and assumes that "StringValue" exists in the "TestKey\\SubKey1" key.
  '    If the Value that you query does not exist, then QueryValue returns an
  '    error code of 2 - 'ERROR_BADKEY'.
  '
  Dim lRetVal As Long        'result of the API functions
  Dim hKey As Long            'handle of opened key
  Dim vValue As Variant      'setting of queried value
  lRetVal = RegOpenKeyEx(lPredefinedKey, sKeyName, 0, KEY_ALL_ACCESS, hKey)
  lRetVal = QueryValueEx(hKey, sValueName, vValue)
  Rem QueryValue = UCase(vValue)
  QueryValue = vValue
  RegCloseKey (hKey)
End Function
 
Public Function SetValueEx(ByVal hKey As Long, sValueName As String, lType As Long, vValue As Variant) As Long
  Dim lValue As Long
  Dim sValue As String
  Select Case lType
        Case REG_SZ
              sValue = vValue & Chr$(0)
              SetValueEx = RegSetValueExString(hKey, sValueName, 0&, lType, sValue, Len(sValue))
        Case REG_DWORD
              lValue = vValue
              SetValueEx = RegSetValueExLong(hKey, sValueName, 0&, lType, lValue, 4)
 
  End Select
End Function
 
Function QueryValueEx(ByVal lhKey As Long, ByVal szValueName As String, vValue As Variant) As Long
  Dim cch As Long
  Dim lrc As Long
  Dim lType As Long
  Dim lValue As Long
  Dim sValue As String

  On Error GoTo QueryValueExError

  ' Determine the size and type of data to be read
  lrc = RegQueryValueExNULL(lhKey, szValueName, 0&, lType, 0&, cch)
 
  Rem If lrc <> ERROR_NONE Then Error 5
 
  Select Case lType
        ' For strings
        Case REG_SZ:
              sValue = String(cch, 0)
              lrc = RegQueryValueExString(lhKey, szValueName, 0&, lType, sValue, cch)
              If lrc = ERROR_NONE Then
                vValue = Left$(sValue, cch - 1)
              Else
                vValue = Empty
              End If
          ' For DWORDS
          Case REG_DWORD:
              lrc = RegQueryValueExLong(lhKey, szValueName, 0&, lType, lValue, cch)
              If lrc = ERROR_NONE Then vValue = lValue
          Case Else
              'all other data types not supported
              lrc = -1
  End Select

QueryValueExExit:
  QueryValueEx = lrc
  Exit Function

QueryValueExError:
  Resume QueryValueExExit
 
End Function
'-----------------------Registry_stuff.bas-----------------------
Avatar billede vbcoder Nybegynder
23. maj 2004 - 06:47 #1
Public Function QueryValue(lPredefinedKey As Long, sKeyName As String, sValueName As String) As Variant
  '
  ' Querying a Registry Value
  '
  ' The next procedure can be used to ascertain the setting of an existing value.
  ' QueryValue takes the name of the key and the name of a value associated with
  ' that key and displays a message box with the corresponding value. It uses a
  ' call to the QueryValueEx wrapper function defined below, which supports only '
  ' REG_SZ and REG_DWORD types:
  '
  ' Example:
  '
  '  QueryValue HKEY_CURRENT_USER, "TestKey\\SubKey1", "StringValue"
  '
  '  - displays a message box with the current setting of the "StringValue"
  '    value, and assumes that "StringValue" exists in the "TestKey\\SubKey1" key.
  '    If the Value that you query does not exist, then QueryValue returns an
  '    error code of 2 - 'ERROR_BADKEY'.
  '
  Dim lRetVal As Long        'result of the API functions
  Dim hKey As Long            'handle of opened key
  Dim vValue As Variant      'setting of queried value
  lRetVal = RegOpenKeyEx(lPredefinedKey, sKeyName, 0, KEY_ALL_ACCESS, hKey)

'*************************************************
'dette kald returnerer værdien -1
  lRetVal = QueryValueEx(hKey, sValueName, vValue)
'*************************************************

  Rem QueryValue = UCase(vValue)
  QueryValue = vValue
  RegCloseKey (hKey)
End Function
Avatar billede vbcoder Nybegynder
23. maj 2004 - 06:55 #2
det gør det fordi

Function QueryValueEx(ByVal lhKey As Long, ByVal szValueName As String, vValue As Variant) As Long
  Dim cch As Long
  Dim lrc As Long
  Dim lType As Long
  Dim lValue As Long
  Dim sValue As String

  On Error GoTo QueryValueExError

'**************************************************************************
  ' Determine the size and type of data to be read
  lrc = RegQueryValueExNULL(lhKey, szValueName, 0&, lType, 0&, cch)

'lType som returneres i ovenstående ikke har værdien 1 eller 4 hvorfor
'returværdien lrc i nedenstående select sættes til -1
'spørg mig ikke hvorfor
'************************************************************************** 
  Rem If lrc <> ERROR_NONE Then Error 5
 
  Select Case lType
        ' For strings
        Case REG_SZ:
              sValue = String(cch, 0)
              lrc = RegQueryValueExString(lhKey, szValueName, 0&, lType, sValue, cch)
              If lrc = ERROR_NONE Then
                vValue = Left$(sValue, cch - 1)
              Else
                vValue = Empty
              End If
          ' For DWORDS
          Case REG_DWORD:
              lrc = RegQueryValueExLong(lhKey, szValueName, 0&, lType, lValue, cch)
              If lrc = ERROR_NONE Then vValue = lValue
          Case Else

'***********************************************************************
'man kunne 'remme' linjen der sætter lrc til -1
'og det vil betyde at lrc har værdien 2
'som netop i declarationen er ERROR_BADKEY

              'all other data types not supported
              lrc = -1

'***********************************************************************
  End Select

QueryValueExExit:
  QueryValueEx = lrc
  Exit Function

QueryValueExError:
  Resume QueryValueExExit
 
End Function
 
'dette kald ret
  ' Determine the size and type of data to be read
  lrc = RegQueryValueExNULL(lhKey, szValueName, 0&, lType, 0&, cch)
 
  Rem If lrc <> ERROR_NONE Then Error 5
 
  Select Case lType
        ' For strings
        Case REG_SZ:
              sValue = String(cch, 0)
              lrc = RegQueryValueExString(lhKey, szValueName, 0&, lType, sValue, cch)
              If lrc = ERROR_NONE Then
                vValue = Left$(sValue, cch - 1)
              Else
                vValue = Empty
              End If
          ' For DWORDS
          Case REG_DWORD:
              lrc = RegQueryValueExLong(lhKey, szValueName, 0&, lType, lValue, cch)
              If lrc = ERROR_NONE Then vValue = lValue
          Case Else
              'all other data types not supported
              lrc = -1
  End Select

QueryValueExExit:
  QueryValueEx = lrc
  Exit Function

QueryValueExError:
  Resume QueryValueExExit
 
End Function
Avatar billede vbcoder Nybegynder
23. maj 2004 - 06:58 #3
Desuden

Public Function QueryValue(lPredefinedKey As Long, sKeyName As String, sValueName As String) As Variant
  '
  ' Querying a Registry Value
  '
  ' The next procedure can be used to ascertain the setting of an existing value.
  ' QueryValue takes the name of the key and the name of a value associated with
  ' that key and displays a message box with the corresponding value. It uses a
  ' call to the QueryValueEx wrapper function defined below, which supports only '
  ' REG_SZ and REG_DWORD types:
  '
  ' Example:
  '
  '  QueryValue HKEY_CURRENT_USER, "TestKey\\SubKey1", "StringValue"
  '
  '  - displays a message box with the current setting of the "StringValue"
  '    value, and assumes that "StringValue" exists in the "TestKey\\SubKey1" key.
  '    If the Value that you query does not exist, then QueryValue returns an
  '    error code of 2 - 'ERROR_BADKEY'.
  '
  Dim lRetVal As Long        'result of the API functions
  Dim hKey As Long            'handle of opened key

'************************************************************
  Dim vValue As Variant      'setting of queried value
'************************************************************

  lRetVal = RegOpenKeyEx(lPredefinedKey, sKeyName, 0, KEY_ALL_ACCESS, hKey)
  Rem QueryValue = UCase(vValue)

'*********************************************************************
'vValue eksisterer kun somen tom variabel
'hvorfor QueryValue nødvendigvis må være tom ;-)
'jeg kan ikke lige se hvorfra værdien skulle komme

  QueryValue = vValue
'**********************************************************************

  RegCloseKey (hKey)

End Function
Avatar billede angelenglen Nybegynder
23. maj 2004 - 10:02 #4
sorry men jeg forstår ikke alt det du skriver der.

men du ser ud til at forstå det ret godt, så nogen chance for at du kan skrive et lille kode eksempel på hvordan jeg kan få den til at skrive indholdet af key'en hvis den findes, ellers skrive at key'en ikke eksisterer?

lidt ligesom det her, men bare noget der virker, for det gør dette her ikke:
        RegValue = QueryValue(HKEY_LOCAL_MACHINE, "SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run", "NonExistingKey")
            If RegValue = 2 Then MsgBox "key does not exist" Else MsgBox "The key exists and is: " & RegValue
Avatar billede _cyberdude_ Nybegynder
23. maj 2004 - 20:08 #5
Som jeg har forstået det bliver RegValue ikke til 2, men der bliver "raised" en error, som du kan gøre sådan her:

RegValue = QueryValue(HKEY_LOCAL_MACHINE, "SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run", "NonExistingKey")

If Err.Number = 2 Then MsgBox "key does not exist" Else MsgBox "The key exists and is: " & RegValue


Det er sådan jeg forstår teksten... prøv det.. :D
Avatar billede _cyberdude_ Nybegynder
23. maj 2004 - 20:08 #6
og så skal du jo lige huske at RegValue skal være en string... :D:D...
Avatar billede angelenglen Nybegynder
23. maj 2004 - 22:33 #7
cool, har aldrig brugt err.number før, nice lille ting.
Avatar billede Ny bruger Nybegynder

Din løsning...

Tilladte BB-code-tags: [b]fed[/b] [i]kursiv[/i] [u]understreget[/u] Web- og emailadresser omdannes automatisk til links. Der sættes "nofollow" på alle links.

Loading billede Opret Preview
Kategori
Kurser inden for grundlæggende programmering

Log ind eller opret profil

Hov!

For at kunne deltage på Computerworld Eksperten skal du være logget ind.

Det er heldigvis nemt at oprette en bruger: Det tager to minutter og du kan vælge at bruge enten e-mail, Facebook eller Google som login.

Du kan også logge ind via nedenstående tjenester