Results 1 to 37 of 37

Thread: If Registry Key Exist

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Dec 2013
    Posts
    330

    Post If Registry Key Exist

    Hi everyone,
    Code:
    Set oReg = GetObject("winmgmts:{impersonationLevel=impersonate}!\\.\root\default:StdRegProv")
    If oReg.EnumKey(HKEY_LOCAL_MACHINE, "SOFTWARE\Coding\Test", "", "") = 0 Then
    MsgBox "The key exist!"
    Else
    End If
    I been using this code for a while, the purpose of this code is to tell if a registry key exist or not.
    for some reason I cannot get the code to work any more, is there a similar way to tell if a registry key exist?
    Thanks in advance.

  2. #2
    Default Member Bonnie West's Avatar
    Join Date
    Jun 2012
    Location
    InIDE
    Posts
    4,060

    Re: If Registry Key Exist

    Try this:

    Code:
    Option Explicit 'In a standard module
    
    Public Enum PredefinedRegKeys
        HKEY_CLASSES_ROOT = &H80000000
        HKEY_CURRENT_USER = &H80000001
        HKEY_LOCAL_MACHINE = &H80000002
        HKEY_USERS = &H80000003
        HKEY_CURRENT_CONFIG = &H80000005
    End Enum
    #If False Then
        Dim HKEY_CLASSES_ROOT, HKEY_CURRENT_USER, _
        HKEY_LOCAL_MACHINE, HKEY_USERS, HKEY_CURRENT_CONFIG
    #End If
    
    Public Enum WOW64_Flags
        KEY_WOW64_64KEY = &H100
        KEY_WOW64_32KEY = &H200
    End Enum
    #If False Then
        Dim KEY_WOW64_64KEY, KEY_WOW64_32KEY
    #End If
    
    Private Declare Function RegCloseKey Lib "advapi32.dll" (ByVal hKey As Long) As Long
    Private Declare Function RegOpenKeyExW Lib "advapi32.dll" (ByVal hKey As Long, ByVal lpSubKey As Long, ByVal ulOptions As Long, ByVal samDesired As Long, ByRef phkResult As Long) As Long
    
    Public Function RegKeyExists(ByVal PredefKey As PredefinedRegKeys, ByRef SubKey As String, Optional ByVal WOW64_Flags As WOW64_Flags) As Boolean
        Const ERROR_SUCCESS = 0&, ERROR_SHARING_VIOLATION = 32&, KEY_READ = &H20019
        Dim hKey As Long, RV As Long
    
        RV = RegOpenKeyExW(PredefKey, StrPtr(SubKey), 0&, KEY_READ Or WOW64_Flags, hKey)
        If hKey Then hKey = RegCloseKey(hKey): Debug.Assert hKey = ERROR_SUCCESS
        RegKeyExists = RV = ERROR_SUCCESS Or RV = ERROR_SHARING_VIOLATION
    End Function
    Code:
    Private Sub Main()
        If RegKeyExists(HKEY_LOCAL_MACHINE, "SOFTWARE\Coding\Test") Then
            MsgBox "The key exists!", vbInformation
        Else
            MsgBox "The key doesn't exist!", vbCritical
        End If
    End Sub
    On Local Error Resume Next: If Not Empty Is Nothing Then Do While Null: ReDim i(True To False) As Currency: Loop: Else Debug.Assert CCur(CLng(CInt(CBool(False Imp True Xor False Eqv True)))): Stop: On Local Error GoTo 0
    Declare Sub CrashVB Lib "msvbvm60" (Optional DontPassMe As Any)

  3. #3

    Thread Starter
    Hyperactive Member
    Join Date
    Dec 2013
    Posts
    330

    Re: If Registry Key Exist

    Thank you so much Bonnie West, but I just found out one thing, my code actually work,
    But only from: HKEY_CURRENT_USER but not if it is from: HKEY_LOCAL_MACHINE ...
    I tested your example, and it does not work unless if it is from: HKEY_CURRENT_USER.
    Code:
    If RegKeyExists(HKEY_CURRENT_USER, "Software\Yahoo") Then
    MsgBox "The key exists!", vbInformation
    Else
    MsgBox "The key doesn't exist!", vbCritical
    End If
    
    Set oReg = GetObject("winmgmts:{impersonationLevel=impersonate}!\\.\root\default:StdRegProv")
    If oReg.EnumKey(HKEY_CURRENT_USER, "Software\Yahoo", "", "") = 0 Then
    MsgBox "The key exists!", vbInformation
    Else
    MsgBox "The key doesn't exist!", vbCritical
    End If

  4. #4
    PowerPoster
    Join Date
    Jan 2008
    Posts
    11,074

    Re: If Registry Key Exist

    It works for both keys

    EDIT:

    You're right, it doesn't work
    Last edited by jmsrickland; Jun 19th, 2015 at 10:34 PM.


    Anything I post is an example only and is not intended to be the only solution, the total solution nor the final solution to your request nor do I claim that it is. If you find it useful then it is entirely up to you to make whatever changes necessary you feel are adequate for your purposes.

  5. #5

    Thread Starter
    Hyperactive Member
    Join Date
    Dec 2013
    Posts
    330

    Re: If Registry Key Exist

    Yeah, it should, but believe me, on my test is not triggering that the key exist

  6. #6

    Thread Starter
    Hyperactive Member
    Join Date
    Dec 2013
    Posts
    330

    Re: If Registry Key Exist

    Take a look:
    Name:  Example.jpg
Views: 730
Size:  13.3 KB

  7. #7
    Hyperactive Member
    Join Date
    Oct 2013
    Posts
    389

    Re: If Registry Key Exist

    Win 7 ? Do you have administrator rights ? is it located on a network drive or local ?
    compile to .exe, place on your desktop, right click it and run as administrator - does it work now ?

    Problem with HKLM, is that unlike HKCU, it is mostly used by installers to read and write stuff, therefor privileges and permissions are asked from the user prior to execution of the installer.
    if it's a normal exe however, you need to manually 'grant' it permissions to read\edit Local Machine values, using a local administrator account.
    Last edited by stum; Jun 20th, 2015 at 11:29 AM.

  8. #8
    PowerPoster
    Join Date
    Jan 2008
    Posts
    11,074

    Re: If Registry Key Exist

    On third thought it does work.

    I tried it several time changing the key and making sure that it either existed or didn't exist and the code gave correct results.


    Anything I post is an example only and is not intended to be the only solution, the total solution nor the final solution to your request nor do I claim that it is. If you find it useful then it is entirely up to you to make whatever changes necessary you feel are adequate for your purposes.

  9. #9
    Default Member Bonnie West's Avatar
    Join Date
    Jun 2012
    Location
    InIDE
    Posts
    4,060

    Re: If Registry Key Exist

    Quote Originally Posted by stum View Post
    if it's a normal exe however, you need to manually 'grant' it permissions to read\edit Local Machine values, using a local administrator account.
    Elevating the process shouldn't be necessary because the function above opens the specified registry key only for "reading" (KEY_READ), not "writing".



    @ Coding

    Try adding a MsgBox "RV = " & RV, vbInformation just before the End Function line. What return value do you get when running that code compiled?

    If you have access to another PC or have a dual-boot OS, see whether that code still fails in that other system. If it doesn't, then there's probably something else wrong with your development machine.
    On Local Error Resume Next: If Not Empty Is Nothing Then Do While Null: ReDim i(True To False) As Currency: Loop: Else Debug.Assert CCur(CLng(CInt(CBool(False Imp True Xor False Eqv True)))): Stop: On Local Error GoTo 0
    Declare Sub CrashVB Lib "msvbvm60" (Optional DontPassMe As Any)

  10. #10

    Thread Starter
    Hyperactive Member
    Join Date
    Dec 2013
    Posts
    330

    Re: If Registry Key Exist

    stum, I am running Windows 7 Ultimate 64-bit, with UAC disabled,
    and it is not on a network drive. then as a compiling the file and run it, still the same.
    then as granting permission, I already have permission, etc.

    jmsrickland, I don't know what to say but did not worked for me.


    Bonnie West: "What return value do you get when running that code compiled?"

    I get the same thing, nothing.

  11. #11
    Default Member Bonnie West's Avatar
    Join Date
    Jun 2012
    Location
    InIDE
    Posts
    4,060

    Re: If Registry Key Exist

    Quote Originally Posted by Coding View Post
    Bonnie West: "What return value do you get when running that code compiled?"

    I get the same thing, nothing.
    Did you remember to insert the MsgBox statement I told you in post #9 before compiling that code and running it? What was the message?

    Have you also already tried my previous suggestion about running that code in a system other than your development machine? If both my code and your code doesn't work in your dev PC, then there is probably something in your dev PC that's causing the problem.
    On Local Error Resume Next: If Not Empty Is Nothing Then Do While Null: ReDim i(True To False) As Currency: Loop: Else Debug.Assert CCur(CLng(CInt(CBool(False Imp True Xor False Eqv True)))): Stop: On Local Error GoTo 0
    Declare Sub CrashVB Lib "msvbvm60" (Optional DontPassMe As Any)

  12. #12

    Thread Starter
    Hyperactive Member
    Join Date
    Dec 2013
    Posts
    330

    Re: If Registry Key Exist

    the problem seems to happen only when I try to read: HKEY_LOCAL_MACHINE
    Take a look at the following code:
    Code:
    If RegKeyExists(HKEY_LOCAL_MACHINE, "OFTWARE\WinRAR") Then
    MsgBox "The key exists!", vbInformation
    Else
    MsgBox "The key doesn't exist!", vbCritical
    End If
    
    Set oReg = GetObject("winmgmts:{impersonationLevel=impersonate}!\\.\root\default:StdRegProv")
    If oReg.EnumKey(HKEY_LOCAL_MACHINE, "OFTWARE\WinRAR", "", "") = 0 Then
    MsgBox "The key exists!", vbInformation
    Else
    MsgBox "The key doesn't exist!", vbCritical
    End If
    
    MsgBox "RV = " & RV, vbInformation
    As the result I get this:

    Project1
    ---------------------------
    The key doesn't exist!
    ---------------------------
    OK
    ---------------------------

    ---------------------------
    Project1
    ---------------------------
    The key doesn't exist!
    ---------------------------
    OK
    ---------------------------

    ---------------------------
    Project1
    ---------------------------
    RV =
    ---------------------------
    OK
    ---------------------------

    In this test I tried both, my code and your example,
    I tried with WinRAR key, either compile and not, I get the same thing.

  13. #13
    Default Member Bonnie West's Avatar
    Join Date
    Jun 2012
    Location
    InIDE
    Posts
    4,060

    Re: If Registry Key Exist

    Quote Originally Posted by Coding View Post
    Take a look at the following code:
    Code:
    If RegKeyExists(HKEY_LOCAL_MACHINE, "OFTWARE\WinRAR") Then
    MsgBox "The key exists!", vbInformation
    Else
    MsgBox "The key doesn't exist!", vbCritical
    End If
    
    Set oReg = GetObject("winmgmts:{impersonationLevel=impersonate}!\\.\root\default:StdRegProv")
    If oReg.EnumKey(HKEY_LOCAL_MACHINE, "OFTWARE\WinRAR", "", "") = 0 Then
    MsgBox "The key exists!", vbInformation
    Else
    MsgBox "The key doesn't exist!", vbCritical
    End If
    
    MsgBox "RV = " & RV, vbInformation
    It seems you have a typo in the registry subkey you're passing to both functions.



    As I've stated in post #9, the MsgBox "RV = " & RV, vbInformation line is supposed to be inserted here:

    Code:
    Public Function RegKeyExists(ByVal PredefKey As PredefinedRegKeys, ByRef SubKey As String, Optional ByVal WOW64_Flags As WOW64_Flags) As Boolean
        Const ERROR_SUCCESS = 0&, ERROR_SHARING_VIOLATION = 32&, KEY_READ = &H20019
        Dim hKey As Long, RV As Long
    
        RV = RegOpenKeyExW(PredefKey, StrPtr(SubKey), 0&, KEY_READ Or WOW64_Flags, hKey)
        If hKey Then hKey = RegCloseKey(hKey): Debug.Assert hKey = ERROR_SUCCESS
        RegKeyExists = RV = ERROR_SUCCESS Or RV = ERROR_SHARING_VIOLATION
        MsgBox "RV = " & RV, vbInformation
    End Function
    The reason I'm asking you to retrieve the return value of the RegOpenKeyExW API function is so that we can find out why it is failing in your system. Additionally, I would also like for you to test my code in another PC (if you have access to one) so that we can further figure out exactly why it doesn't work in your development machine.
    On Local Error Resume Next: If Not Empty Is Nothing Then Do While Null: ReDim i(True To False) As Currency: Loop: Else Debug.Assert CCur(CLng(CInt(CBool(False Imp True Xor False Eqv True)))): Stop: On Local Error GoTo 0
    Declare Sub CrashVB Lib "msvbvm60" (Optional DontPassMe As Any)

  14. #14
    PowerPoster
    Join Date
    Jan 2008
    Posts
    11,074

    Re: If Registry Key Exist

    I made a slight twist to Bonnie's code so you can enter the sub key and see if it exist in one or more HKEYs
    Attached Files Attached Files
    Last edited by jmsrickland; Jun 24th, 2015 at 01:14 AM.


    Anything I post is an example only and is not intended to be the only solution, the total solution nor the final solution to your request nor do I claim that it is. If you find it useful then it is entirely up to you to make whatever changes necessary you feel are adequate for your purposes.

  15. #15

    Thread Starter
    Hyperactive Member
    Join Date
    Dec 2013
    Posts
    330

    Re: If Registry Key Exist

    Good two points of view Bonnie, but you know what, maybe I was exhausted on that night when I made that test,
    But before that I had it right and made few test and I was getting the same return.
    Thank you for your example jms rickland, I am testing it now, please take a look at the results:
    Name:  Example.jpg
Views: 854
Size:  43.4 KB
    For some reason I do not get any result with HKEY_LOCAL_MACHINE.

  16. #16
    PowerPoster
    Join Date
    Jan 2008
    Posts
    11,074

    Re: If Registry Key Exist

    You don't put HKEY_LOCAL_MACHINE in the Textbox because that is only for the subkey so all you type in is SOFTWARE\WinRAR although you still may get the same results
    Last edited by jmsrickland; Jun 23rd, 2015 at 12:59 AM.


    Anything I post is an example only and is not intended to be the only solution, the total solution nor the final solution to your request nor do I claim that it is. If you find it useful then it is entirely up to you to make whatever changes necessary you feel are adequate for your purposes.

  17. #17

    Thread Starter
    Hyperactive Member
    Join Date
    Dec 2013
    Posts
    330

    Re: If Registry Key Exist

    Sorry I was having issues loging back to the forum...
    My bad jmsrickland, here is another example, and you know what,
    it looks like is working, but only with HKEY_CURRENT_USER
    Name:  Example.jpg
Views: 650
Size:  33.2 KB

  18. #18
    Default Member Bonnie West's Avatar
    Join Date
    Jun 2012
    Location
    InIDE
    Posts
    4,060

    Re: If Registry Key Exist

    Coding, you mentioned that you are "running Windows 7 Ultimate 64-bit". Your screenshots of your's and jmsrickland's test projects, however, seems to suggest otherwise. Are you absolutely sure that you're running the test projects under Windows 7 Ultimate 64-bit and not inside a virtual machine or emulator?


    One more thing. Please add to jmsrickland's test project the diagnostic MsgBox I've been telling you in post #13. Please insert it in the location I've highlighted there. Compile the modified code and then run it under Win 7. Please post here the message that you got for the HKLM subkey.
    On Local Error Resume Next: If Not Empty Is Nothing Then Do While Null: ReDim i(True To False) As Currency: Loop: Else Debug.Assert CCur(CLng(CInt(CBool(False Imp True Xor False Eqv True)))): Stop: On Local Error GoTo 0
    Declare Sub CrashVB Lib "msvbvm60" (Optional DontPassMe As Any)

  19. #19

    Thread Starter
    Hyperactive Member
    Join Date
    Dec 2013
    Posts
    330

    Re: If Registry Key Exist

    Bonnie, I am really running Windows 7 64-bit, let me show you a full screenshot of the five clicks I performed
    after adding the error-trap you wanted me to add in the module of the project, please take a look at these examples:
    Name:  Example 1.jpg
Views: 635
Size:  32.7 KB
    Name:  Example 2.jpg
Views: 629
Size:  32.6 KB
    Name:  Example 3.jpg
Views: 599
Size:  32.7 KB
    Name:  Example 4.jpg
Views: 609
Size:  32.7 KB
    Name:  Example 5.jpg
Views: 600
Size:  32.7 KB

  20. #20
    Default Member Bonnie West's Avatar
    Join Date
    Jun 2012
    Location
    InIDE
    Posts
    4,060

    Re: If Registry Key Exist

    A return value of 2 means that "The system cannot find the file specified."

    Hmm... Is your version of WinRAR 64-bit? If so, try specifying KEY_WOW64_64KEY for the WOW64_Flags parameter of the RegKeyExists function.
    On Local Error Resume Next: If Not Empty Is Nothing Then Do While Null: ReDim i(True To False) As Currency: Loop: Else Debug.Assert CCur(CLng(CInt(CBool(False Imp True Xor False Eqv True)))): Stop: On Local Error GoTo 0
    Declare Sub CrashVB Lib "msvbvm60" (Optional DontPassMe As Any)

  21. #21
    PowerPoster
    Join Date
    Jan 2008
    Posts
    11,074

    Re: If Registry Key Exist

    I notice that there is a difference in the results depending on whether you use \ in the subkey

    I checked my Registry (XP PRO/SP2) and it has the following keys:

    HKEY_CLASSES_ROOT\SOFTWARE\Microsoft
    HKEY_CURRENT_USER\SOFTWARE\Microsoft
    HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft
    HKEY_CURRENT_CONFIG\SOFTWARE\Microsoft

    I get different results with and without the \
    Attached Images Attached Images   


    Anything I post is an example only and is not intended to be the only solution, the total solution nor the final solution to your request nor do I claim that it is. If you find it useful then it is entirely up to you to make whatever changes necessary you feel are adequate for your purposes.

  22. #22
    PowerPoster
    Join Date
    Jan 2008
    Posts
    11,074

    Re: If Registry Key Exist

    To get around that issue you can make the following change to the code in the module

    Code:
    Public Function RegKeyExists(ByVal HKEYvalue As Long, ByRef SubKey As String, Optional ByVal WOW64_Flags As WOW64_Flags) As Boolean
     Const ERROR_SUCCESS = 0&, ERROR_SHARING_VIOLATION = 32&, KEY_READ = &H20019
     Dim HKey As Long, RV As Long
    
     
     SubKey = Replace(SubKey, "\", "", 1, 1)
    
     RV = RegOpenKeyExW(HKEYvalue, StrPtr(SubKey), 0&, KEY_READ Or WOW64_Flags, HKey)
        
     If HKey Then HKey = RegCloseKey(HKey): Debug.Assert HKey = ERROR_SUCCESS
        
     RegKeyExists = RV = ERROR_SUCCESS Or RV = ERROR_SHARING_VIOLATION
    End Function
    EDIT: Correction. The line above is incorrect. It should be this:

    Code:
    If Left$(SubKey, 1) = "\" Then SubKey = Mid$(SubKey, 2)
    See post 23 by MikiSoft who caught the error
    Last edited by jmsrickland; Jun 24th, 2015 at 01:16 AM.


    Anything I post is an example only and is not intended to be the only solution, the total solution nor the final solution to your request nor do I claim that it is. If you find it useful then it is entirely up to you to make whatever changes necessary you feel are adequate for your purposes.

  23. #23
    Hyperactive Member
    Join Date
    Jun 2011
    Posts
    461

    Re: If Registry Key Exist

    Quote Originally Posted by jmsrickland View Post
    ...
    Code:
    ...
    
     SubKey = Replace(SubKey, "\", "", 1, 1)
    
    ...
    That is wrong, it will always replace the first occurrence no matter if it's in the beginning or not. I would solve it like this:
    Code:
    If Left$(SubKey, 1) = "\" Then SubKey = Mid$(SubKey, 2)

  24. #24

    Thread Starter
    Hyperactive Member
    Join Date
    Dec 2013
    Posts
    330

    Re: If Registry Key Exist

    jmsrickland...
    I was so happy when I started reading, I thought that this time it was going to work
    So I went ahead and modify and add the extra line of code, and even without it,
    I tried adding the backlash manually, and both methods failed!

  25. #25

    Thread Starter
    Hyperactive Member
    Join Date
    Dec 2013
    Posts
    330

    Re: If Registry Key Exist

    jmsrickland, Bonnie, guys, I compile the project and took it to another computer which is also running Windows 7 64-bit
    and here I bring few examples, I hope you guys can see all these examples, or I could upload one by one, is just that their many,
    But here they are, please take a look:
    Name:  Few-Examples.jpg
Views: 634
Size:  65.2 KB
    I don't get it, it does get a reading of some subkeys but it does not on some other subkeys.

  26. #26
    PowerPoster
    Join Date
    Jan 2008
    Posts
    11,074

    Re: If Registry Key Exist

    Quote Originally Posted by MikiSoft View Post
    That is wrong, it will always replace the first occurrence no matter if it's in the beginning or not. I would solve it like this:
    Code:
    If Left$(SubKey, 1) = "\" Then SubKey = Mid$(SubKey, 2)
    You're right. I don't know why I posted that line. It needs to be as you stated.

    I corrected the code in the zip file upload for post #14

    Thanks for catching that
    Last edited by jmsrickland; Jun 24th, 2015 at 01:13 AM.


    Anything I post is an example only and is not intended to be the only solution, the total solution nor the final solution to your request nor do I claim that it is. If you find it useful then it is entirely up to you to make whatever changes necessary you feel are adequate for your purposes.

  27. #27

    Thread Starter
    Hyperactive Member
    Join Date
    Dec 2013
    Posts
    330

    Re: If Registry Key Exist

    jmsrickland I downloaded your updated project, this time I compiled the project to get the best results as possible.
    Also this computer and the other one that I ran the test yesterday, both have UAC disabled. and not that I have something
    with WinRAR, but not just WinRAR subkeys were not being detected, if you take a closer look at my post #25, there
    I showed multiple examples in which not only the WinRAR subkeys were not detected.

  28. #28
    Default Member Bonnie West's Avatar
    Join Date
    Jun 2012
    Location
    InIDE
    Posts
    4,060

    Re: If Registry Key Exist

    Try this modification of jmsrickland's test project.


    Name:  RegKeyExists Demo.png
Views: 562
Size:  6.1 KB
    Attached Files Attached Files
    On Local Error Resume Next: If Not Empty Is Nothing Then Do While Null: ReDim i(True To False) As Currency: Loop: Else Debug.Assert CCur(CLng(CInt(CBool(False Imp True Xor False Eqv True)))): Stop: On Local Error GoTo 0
    Declare Sub CrashVB Lib "msvbvm60" (Optional DontPassMe As Any)

  29. #29
    PowerPoster
    Join Date
    Jan 2008
    Posts
    11,074

    Re: If Registry Key Exist

    Just a question. Are the values the same for the predefined keys across different platforms


    Anything I post is an example only and is not intended to be the only solution, the total solution nor the final solution to your request nor do I claim that it is. If you find it useful then it is entirely up to you to make whatever changes necessary you feel are adequate for your purposes.

  30. #30

    Thread Starter
    Hyperactive Member
    Join Date
    Dec 2013
    Posts
    330

    Re: If Registry Key Exist

    Hello guys, thank you for the new example Bonnie, please take a look:
    Name:  Example.png
Views: 682
Size:  10.8 KB
    I've been thinking this... the example code I posted on post #1 thats the code I that I've been using for few months now,
    and this whole time I thought that the code was reading and checking all these subkeys, when actually there was only a
    reading on the HKEY_CURRENT_USER, and honestly I don't see any special with the WinRAR subkeys I mean, I don't see
    why I am not able to read it with my code and after all the hard work from you guys, is just that is not only the WinRAR
    subkeys, but also so many other subkeys on the registry, and for what I see, the hardest one is: HKEY_LOCAL_MACHINE.
    Last edited by Coding; Jun 25th, 2015 at 11:42 AM.

  31. #31
    PowerPoster
    Join Date
    Jan 2008
    Posts
    11,074

    Re: If Registry Key Exist

    I'm at a complete lost, Coding, I just don't know what else to do. Wait till Bonnie returns perhaps we will know more.


    Anything I post is an example only and is not intended to be the only solution, the total solution nor the final solution to your request nor do I claim that it is. If you find it useful then it is entirely up to you to make whatever changes necessary you feel are adequate for your purposes.

  32. #32

    Thread Starter
    Hyperactive Member
    Join Date
    Dec 2013
    Posts
    330

    Re: If Registry Key Exist

    Thank you for all your help jmsrickland, I'll wait for Bonnie and see, I'll be testing something...

  33. #33
    Default Member Bonnie West's Avatar
    Join Date
    Jun 2012
    Location
    InIDE
    Posts
    4,060

    Re: If Registry Key Exist

    Quote Originally Posted by Coding View Post
    ... and honestly I don't see any special with the WinRAR subkeys I mean, I don't see
    why I am not able to read it with my code ...
    According to your latest screenshot, the "HKEY_LOCAL_MACHINE\SOFTWARE\WinRAR" subkey exists only in the 64-bit view of the registry. In my post #20, I began to suspect that this was probably the case. After looking back at one of your screenshots showing the WinRAR registry key,


    The value name "exe64" hints that the installed WinRAR version is 64-bit.


    I became even more convinced that your issue stems from trying to access the non-existent 32-bit view of the WinRAR registry key.


    So, in order to check the existence of your target registry keys when running under a 64-bit OS, you not only need to test the 32-bit keys but you also have to check the 64-bit keys as well. As I've already shown in post #20, this is done by specifying the KEY_WOW64_64KEY flag.
    Last edited by Bonnie West; Jun 26th, 2015 at 02:22 AM.
    On Local Error Resume Next: If Not Empty Is Nothing Then Do While Null: ReDim i(True To False) As Currency: Loop: Else Debug.Assert CCur(CLng(CInt(CBool(False Imp True Xor False Eqv True)))): Stop: On Local Error GoTo 0
    Declare Sub CrashVB Lib "msvbvm60" (Optional DontPassMe As Any)

  34. #34
    Default Member Bonnie West's Avatar
    Join Date
    Jun 2012
    Location
    InIDE
    Posts
    4,060

    Re: If Registry Key Exist

    Quote Originally Posted by jmsrickland View Post
    Just a question. Are the values the same for the predefined keys across different platforms
    Yes. The predefined keys' values are defined in WinReg.h:

    Code:
    //
    // Reserved Key Handles.
    //
    
    #define HKEY_CLASSES_ROOT                   (( HKEY ) (ULONG_PTR)((LONG)0x80000000) )
    #define HKEY_CURRENT_USER                   (( HKEY ) (ULONG_PTR)((LONG)0x80000001) )
    #define HKEY_LOCAL_MACHINE                  (( HKEY ) (ULONG_PTR)((LONG)0x80000002) )
    #define HKEY_USERS                          (( HKEY ) (ULONG_PTR)((LONG)0x80000003) )
    #define HKEY_PERFORMANCE_DATA               (( HKEY ) (ULONG_PTR)((LONG)0x80000004) )
    #define HKEY_PERFORMANCE_TEXT               (( HKEY ) (ULONG_PTR)((LONG)0x80000050) )
    #define HKEY_PERFORMANCE_NLSTEXT            (( HKEY ) (ULONG_PTR)((LONG)0x80000060) )
    #if(WINVER >= 0x0400)
    #define HKEY_CURRENT_CONFIG                 (( HKEY ) (ULONG_PTR)((LONG)0x80000005) )
    #define HKEY_DYN_DATA                       (( HKEY ) (ULONG_PTR)((LONG)0x80000006) )
    #define HKEY_CURRENT_USER_LOCAL_SETTINGS    (( HKEY ) (ULONG_PTR)((LONG)0x80000007) )
    On Local Error Resume Next: If Not Empty Is Nothing Then Do While Null: ReDim i(True To False) As Currency: Loop: Else Debug.Assert CCur(CLng(CInt(CBool(False Imp True Xor False Eqv True)))): Stop: On Local Error GoTo 0
    Declare Sub CrashVB Lib "msvbvm60" (Optional DontPassMe As Any)

  35. #35

    Thread Starter
    Hyperactive Member
    Join Date
    Dec 2013
    Posts
    330

    Re: If Registry Key Exist

    I got your point Bonnie, I used that WinRAR subkey as an example since I my code could not detect it.
    the use of my code was with the purpose of deleting unwanted subkes, and so far was working fine,
    until I decided to take a closer look, then I noticed that those subkeys were not detect it even if they were there
    that's when I thought that my code was failing. so Bonnie, at the end of the road, I mean, there is nothing I can do huh?
    I mean, I cannot use this code to determine if those subkeys are there, I am not talking about the WinRAR subkey, I mean,
    other subkeys which are not detected just like the WinRAR one.
    few days ago I put toghether a code to directly jump to some these same registry subkeys, and also failed

  36. #36
    PowerPoster
    Join Date
    Jan 2008
    Posts
    11,074

    Re: If Registry Key Exist

    How did the WinRAR get in the Registry in the first place? It was put there when you installed WinRAR. So, you should have WinRar on your system. Just for a test uninstall WinRar and see if this removes the Registry entry for it. If it does then you know that it can be removed via code. If you do not have WinRar on your system then go into the Registry and manually rename or delete (which ever you think is best) the WinRAR key from the registry then in a VB6 program see if you can create it. Do this in all the predefined H keys.

    Also, VB6 has it's own set of Registry functions; have you given any thought to using them?
    Last edited by jmsrickland; Jun 26th, 2015 at 10:27 AM.


    Anything I post is an example only and is not intended to be the only solution, the total solution nor the final solution to your request nor do I claim that it is. If you find it useful then it is entirely up to you to make whatever changes necessary you feel are adequate for your purposes.

  37. #37
    Default Member Bonnie West's Avatar
    Join Date
    Jun 2012
    Location
    InIDE
    Posts
    4,060

    Re: If Registry Key Exist

    Quote Originally Posted by Coding View Post
    I got your point Bonnie, ...
    I don't think you fully understood what I wrote in post #33. Did you even bother to visit the links I gave there? Those links points to a couple of MSDN topics which explains well the issue you're facing here. I strongly recommend that you read the documentation first (click that link!) before giving up. My code works; you just haven't realized it yet.
    On Local Error Resume Next: If Not Empty Is Nothing Then Do While Null: ReDim i(True To False) As Currency: Loop: Else Debug.Assert CCur(CLng(CInt(CBool(False Imp True Xor False Eqv True)))): Stop: On Local Error GoTo 0
    Declare Sub CrashVB Lib "msvbvm60" (Optional DontPassMe As Any)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width