Solved

Registry keys in vb6.0

Posted on 2004-03-25
5
2,493 Views
Last Modified: 2013-11-13
I just want to ask how can i check the existance of any registry key either it exists or not via win32 API in vb6.0 .kindly help me in this regard as soon as possible.
0
Comment
Question by:talhaamin
  • 3
5 Comments
 
LVL 4

Expert Comment

by:xassets
ID: 10684829
Here's a full blown do it all registry class

do
dim reg as new registry
sSetting = reg.GetSettingEx(HKEY_LOCAL_MACHINE, "Key", "Value", sDefault)

paste the below into a new class, and thats it.


Option Explicit
Option Compare Text
'
Public Enum PredefinedKeyEnum
    HKEY_CLASSES_ROOT = &H80000000
    HKEY_CURRENT_USER = &H80000001
    HKEY_LOCAL_MACHINE = &H80000002
    HKEY_USERS = &H80000003
End Enum
'
Private Const REG_SZ As Long = 1
Private Const REG_DWORD As Long = 4

Private Const ERROR_NONE = 0
Private Const ERROR_SUCCESS = 0

Private Const READ_CONTROL = &H20000
Private Const STANDARD_RIGHTS_ALL = &H1F0000
Private Const STANDARD_RIGHTS_REQUIRED = &HF0000
Private Const STANDARD_RIGHTS_READ = (READ_CONTROL)
Private Const STANDARD_RIGHTS_WRITE = (READ_CONTROL)
Private Const SYNCHRONIZE = &H100000

'Private Const KEY_ALL_ACCESS = &H3F
Private Const KEY_CREATE_LINK = &H20
Private Const KEY_CREATE_SUB_KEY = &H4
Private Const KEY_ENUMERATE_SUB_KEYS = &H8
Private Const KEY_EVENT = &H1     '  Event contains key event record
Private Const KEY_NOTIFY = &H10
Private Const KEY_QUERY_VALUE = &H1
Private Const KEY_SET_VALUE = &H2
Private Const KEY_READ = ((STANDARD_RIGHTS_READ Or KEY_QUERY_VALUE Or KEY_ENUMERATE_SUB_KEYS Or KEY_NOTIFY) And (Not SYNCHRONIZE))
Private Const KEY_EXECUTE = ((KEY_READ) And (Not SYNCHRONIZE))
Private Const KEY_WRITE = ((STANDARD_RIGHTS_WRITE Or KEY_SET_VALUE Or KEY_CREATE_SUB_KEY) And (Not SYNCHRONIZE))
Private 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))

Private Const REG_OPTION_NON_VOLATILE = 0

Private Declare Function RegCloseKey Lib "advapi32.dll" (ByVal hKey As Long) As Long
Private 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
Private 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
Private 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
Private 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
Private 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
Private 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
Private 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
Private 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
Private Declare Function RegDeleteKey Lib "advapi32.dll" Alias "RegDeleteKeyA" (ByVal hKey As Long, ByVal lpSubKey As String) As Long
'

'--------------------------------------------------------------------------------------
'   Public functions
'--------------------------------------------------------------------------------------

Public Sub CreateKey(sKey As String)
   
   

End Sub

Public Sub DeleteKey(PredefinedKey As PredefinedKeyEnum, sKey As String)
   
    On Error Resume Next
    RegDeleteKey PredefinedKey, sKey

End Sub

Public Function GetKeyList(PredefinedKey As PredefinedKeyEnum, _
                            Section As String, Separator As String) As String
   
    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
    Dim lIndex  As Long
    Dim lLen    As Long
    Dim sResult As String
    Dim sBuffer As String
   
    lRetVal = RegOpenKeyEx(PredefinedKey, Section, 0, _
                            KEY_ENUMERATE_SUB_KEYS, hKey)
    lIndex = 0
    Do
        sBuffer = String(255, 0)
        lRetVal = RegEnumKey(hKey, lIndex, sBuffer, 254)
        sResult = sResult & Left$(sBuffer, InStr(sBuffer, Chr$(0)) - 1) & Separator
        lIndex = lIndex + 1
    Loop Until lRetVal <> ERROR_NONE
    RegCloseKey (hKey)
    GetKeyList = sResult
   
End Function

Public Function GetSettingEx(PredefinedKey As PredefinedKeyEnum, _
                            Section As String, _
                            Key As String, _
                            Default As Variant) As Variant
   
    Dim varResult As Variant
   
    varResult = QueryValue(PredefinedKey, Section, Key)
    If varResult = "" Then
        varResult = Default
    End If
    GetSettingEx = varResult
   
End Function

Public Sub SaveSettingEx(PredefinedKey As PredefinedKeyEnum, _
                            Section As String, _
                            Key As String, _
                            Setting As Variant)
                               
    SetKeyValue PredefinedKey, Section, Key, Setting, REG_SZ
   
End Sub

Public Function KeyExists(sKey As String) As Boolean
   
    Dim sMainKey        As String
    Dim lPredefinedKey  As PredefinedKeyEnum
   
    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
    Dim lLen    As Long
    Dim sResult As String
    Dim sBuffer As String
   
    lPredefinedKey = GetPredefinedKey(sKey, sMainKey)
   
    lRetVal = RegOpenKeyEx(lPredefinedKey, sMainKey, 0, KEY_READ, hKey)
    If lRetVal = ERROR_NONE Then
        KeyExists = True
    Else
        KeyExists = False
    End If
    RegCloseKey (hKey)
   
End Function

Public Function ListKeys(sKey As String, sSeparator As String) As String
   
    Dim sMainKey        As String
    Dim lPredefinedKey  As PredefinedKeyEnum
   
    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
    Dim lIndex  As Long
    Dim lLen    As Long
    Dim sResult As String
    Dim sBuffer As String
   
    lPredefinedKey = GetPredefinedKey(sKey, sMainKey)
   
    lRetVal = RegOpenKeyEx(lPredefinedKey, sMainKey, 0, _
                            KEY_READ, hKey)
    lIndex = 0
    Do
        sBuffer = String(255, 0)
        lRetVal = RegEnumKey(hKey, lIndex, sBuffer, 254)
        sResult = sResult & Left$(sBuffer, InStr(sBuffer, Chr$(0)) - 1) & sSeparator
        lIndex = lIndex + 1
    Loop Until lRetVal <> ERROR_NONE
    RegCloseKey (hKey)
    ListKeys = sResult
   
End Function


'--------------------------------------------------------------------------------------
'   Private functions
'--------------------------------------------------------------------------------------

Private Function GetPredefinedKey(sFullKey As String, ByRef sRemainder As String) As PredefinedKeyEnum
   
    Dim var As Variant
   
    var = Split(sFullKey, "\", 2)
    Select Case var(0)
        Case "HKEY_CLASSES_ROOT"
            GetPredefinedKey = HKEY_CLASSES_ROOT
        Case "HKEY_CURRENT_USER", "HKCU"
            GetPredefinedKey = HKEY_CURRENT_USER
        Case "HKEY_LOCAL_MACHINE", "HKLM"
            GetPredefinedKey = HKEY_LOCAL_MACHINE
        Case "HKEY_USERS"
            GetPredefinedKey = HKEY_USERS
    End Select
   
    sRemainder = var(1)
   
End Function


Private Sub CreateNewKey(sNewKeyName As String, lPredefinedKey As Long)
   
    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_WRITE Or KEY_CREATE_SUB_KEY, _
              0&, hNewKey, lRetVal)
    RegCloseKey (hNewKey)

End Sub

Private Sub SetKeyValue(lPredefinedKey As Long, _
                        sKeyName As String, sValueName As String, _
                        vValueSetting As Variant, lValueType As Long)
   
    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_WRITE, hKey)
    'if it didn't exist then create it
    If lRetVal = 2 Then
        CreateNewKey sKeyName, lPredefinedKey
        lRetVal = RegOpenKeyEx(lPredefinedKey, sKeyName, 0, _
                               KEY_ALL_ACCESS, hKey)
        If lRetVal <> 0 Then
            Err.Raise vbObjectError + 1, "SetKeyValue", "Unable to create registry key"
        End If
    ElseIf hKey = 0 Then
        Err.Raise vbObjectError + 2, "SetKeyValue", "Unable to open registry key"
    End If
    lRetVal = SetValueEx(hKey, sValueName, lValueType, vValueSetting)
    RegCloseKey (hKey)
   
End Sub

Private Function QueryValue(lPredefinedKey As Long, _
                        sKeyName As String, sValueName As String) As String
   
    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_READ, hKey)
    lRetVal = QueryValueEx(hKey, sValueName, vValue)
    QueryValue = vValue
    RegCloseKey (hKey)

End Function

Private 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

Private 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)
        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







0
 
LVL 1

Expert Comment

by:sendtojp
ID: 10685090
Whoa, what does this do?
0
 
LVL 4

Accepted Solution

by:
xassets earned 125 total points
ID: 10685155
talhaamin: Just call the KeyExists method of this class and it will return true of false.

its good to have the full blown class in your app so you can call on it whenever needed and not worry about the specifics of reading the registry
0
 
LVL 2

Author Comment

by:talhaamin
ID: 10685600
Key exist function is not giving me a correct result it is always giving me the indication of key not exist
How can i correctly use it.Iam searching a key value "Nodrives".Kindly help me
0
 
LVL 4

Expert Comment

by:xassets
ID: 10685683
Are you trying to tell if a key exists or if a value exists ? KeyExists only works on keys (left window in regedit)

To tell if a key value exists, use GetSettingEx and supply a default which would not occur in the value, such as "" or "$$"

E.g. Explorer Search for hidden files would always be 0 or 1, so "" is oK as a default

sString = reg.GetSettingEx(HKEY_LOCAL_MACHINE, "Software\Microsoft\Windows\CurrentVersion\Explorer", "SearchHidden", "")
If sString = "" then
    bExists = false
else
    bExists = true
end if
0

Featured Post

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

Suggested Solutions

RIA (Rich Internet Application) tools are interactive internet applications which have many of the characteristics of desktop applications. The RIA tools typically deliver output either by the way of a site-specific browser or via browser plug-in. T…
If you’re thinking to yourself “That description sounds a lot like two people doing the work that one could accomplish,” you’re not alone.
An introduction to basic programming syntax in Java by creating a simple program. Viewers can follow the tutorial as they create their first class in Java. Definitions and explanations about each element are given to help prepare viewers for future …
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…

746 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now