Results 1 to 15 of 15

Thread: simple question for a vb6 guy

  1. #1

    Thread Starter
    New Member
    Join Date
    Aug 2006
    Posts
    13

    simple question for a vb6 guy

    I have some code that I am having issues with.. The code blows up if the registry value doesnt exist. This is what I am trying to check for but I get an run time error because it doesnt exist.

    I just want to return a value of "not" if the registry value does not exist. Can someone help me out?

    Thanks! Below is the code I have (as a function). This should be pretty simple but vb6 syntax is a challenge for me.

    Function checkinstalled()
    Dim WSH, result, theval
    Set WshShell = CreateObject("WScript.Shell")

    theval = WshShell.RegRead("HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\.NETFramework\InstallRoot")
    If theval <> "" Then
    result = "installed"
    Else
    result = "not"
    End If

    End Function

  2. #2
    "Digital Revolution"
    Join Date
    Mar 2005
    Posts
    4,471

    Re: simple question for a vb6 guy

    You could use...

    VB Code:
    1. Function checkinstalled()
    2.     On Error Goto ErrorHandler
    3.  
    4.         Dim WSH, result, theval
    5.         Set WshShell = CreateObject("WScript.Shell")
    6.      
    7.           theval = WshShell.RegRead("HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\.NETFramework\InstallRoot")
    8.             If theval <> "" Then
    9.                 result = "installed"
    10.             Else
    11.                 result = "not"
    12.             End If
    13.  
    14. Exit Function
    15.  
    16. ErrorHandler:
    17.  
    18.          
    19.     End Function

  3. #3

    Thread Starter
    New Member
    Join Date
    Aug 2006
    Posts
    13

    Re: simple question for a vb6 guy

    Quote Originally Posted by DigiRev
    You could use...

    VB Code:
    1. Function checkinstalled()
    2.     On Error Goto ErrorHandler
    3.  
    4.         Dim WSH, result, theval
    5.         Set WshShell = CreateObject("WScript.Shell")
    6.      
    7.           theval = WshShell.RegRead("HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\.NETFramework\InstallRoot")
    8.             If theval <> "" Then
    9.                 result = "installed"
    10.             Else
    11.                 result = "not"
    12.             End If
    13.  
    14. Exit Function
    15.  
    16. ErrorHandler:
    17.  
    18.          
    19.     End Function
    yes I tried that but it returns a null value. I also tried to do this: but it still returns null value:

    ErrorHandler: result = "not"

    how could I do the above properly? Thanks again!

  4. #4
    Fanatic Member Comintern's Avatar
    Join Date
    Nov 2004
    Location
    Lincoln, NE
    Posts
    826

    Re: simple question for a vb6 guy

    I wouldn't use the WshShell for this -- I'd use API calls instead. Try this:
    VB Code:
    1. Const HKEY_LOCAL_MACHINE = &H80000002
    2. Const STANDARD_RIGHTS_ALL = &H1F0000
    3. Const SYNCHRONIZE = &H100000
    4. Const READ_CONTROL = &H20000
    5. Const STANDARD_RIGHTS_READ = (READ_CONTROL)
    6. Const STANDARD_RIGHTS_WRITE = (READ_CONTROL)
    7. Const KEY_CREATE_LINK = &H20
    8. Const KEY_CREATE_SUB_KEY = &H4
    9. Const KEY_ENUMERATE_SUB_KEYS = &H8
    10. Const KEY_NOTIFY = &H10
    11. Const KEY_QUERY_VALUE = &H1
    12. Const KEY_SET_VALUE = &H2
    13. Const KEY_READ = ((STANDARD_RIGHTS_READ Or KEY_QUERY_VALUE Or KEY_ENUMERATE_SUB_KEYS Or KEY_NOTIFY) And (Not SYNCHRONIZE))
    14. Const KEY_WRITE = ((STANDARD_RIGHTS_WRITE Or KEY_SET_VALUE Or KEY_CREATE_SUB_KEY) And (Not SYNCHRONIZE))
    15. Const KEY_EXECUTE = (KEY_READ)
    16. 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))
    17.  
    18. Private Declare Function RegCloseKey Lib "advapi32.dll" (ByVal hKey As Long) As Long
    19. Private Declare Function RegOpenKeyEx Lib "advapi32.dll" Alias "RegOpenKeyExA" (ByVal hKey As Long, ByVal lpSubKey As String, ByVal Reserved As Long, ByVal samDesired As Long, phklKey As Long) As Long
    20.  
    21. Private Sub Example()
    22.  
    23.     Debug.Print KeyExists("SOFTWARE\Microsoft\.NETFramework\InstallRoot")
    24.  
    25. End Sub
    26.  
    27. Private Function KeyExists(sKey As String) As Boolean
    28.  
    29.     Dim lKey As Long
    30.  
    31.     Call RegOpenKeyEx(HKEY_LOCAL_MACHINE, sKey, 0, KEY_ALL_ACCESS, lKey)
    32.     If lKey Then
    33.         KeyExists = True
    34.         Call RegCloseKey(lKey)
    35.     End If
    36.  
    37. End Function

  5. #5

    Thread Starter
    New Member
    Join Date
    Aug 2006
    Posts
    13

    Re: simple question for a vb6 guy

    nice.. I think this is more along the lines of something I am looking for. I am not near the dev machine right now but will check it out tonight. I appreciate the help guys!

  6. #6
    VB Addict Pradeep1210's Avatar
    Join Date
    Apr 2004
    Location
    Inside the CPU...
    Posts
    6,614

    Re: simple question for a vb6 guy

    Quote Originally Posted by project2501
    yes I tried that but it returns a null value. I also tried to do this: but it still returns null value:

    ErrorHandler: result = "not"

    how could I do the above properly? Thanks again!
    In VB6, you return a value by a variable same as function name.

    So instead of "result" everywhere in the code, you need to use "checkinstalled"

    e.g.
    VB Code:
    1. checkinstalled = "installed"        '' not result = "installed"


    Pradeep
    Pradeep, Microsoft MVP (Visual Basic)
    Please appreciate posts that have helped you by clicking icon on the left of the post.
    "A problem well stated is a problem half solved." — Charles F. Kettering

    Read articles on My Blog101 LINQ SamplesJSON ValidatorXML Schema Validator"How Do I" videos on MSDNVB.NET and C# ComparisonGood Coding PracticesVBForums Reputation SaverString EnumSuper Simple Tetris Game


    (2010-2013)
    NB: I do not answer coding questions via PM. If you want my help, then make a post and PM me it's link. If I can help, trust me I will...

  7. #7

    Thread Starter
    New Member
    Join Date
    Aug 2006
    Posts
    13

    Re: simple question for a vb6 guy

    huh... I was not aware of that. I suppose it makes sense though. I guess I never though about it since .NET you have a:

    return (object)

  8. #8

    Thread Starter
    New Member
    Join Date
    Aug 2006
    Posts
    13

    Re: simple question for a vb6 guy

    I am still having issues. Using this code: Not matter what it always errors out:

    Function checkinstalled()
    On Error GoTo ErrorHandler

    Dim WSH, result, theval
    Set WshShell = CreateObject("WScript.Shell")

    theval = WshShell.RegRead("HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\.NETFramework\InstallRoot")
    If theval <> "" Then
    checkinstalled = "installed"
    Else
    checkinstalled = "not"
    End If


    ErrorHandler:
    checkinstalled = "not"

    End Function

    Using this code It always returns "installed" (True) ... what am I doing wrong?

    Const HKEY_LOCAL_MACHINE = &H80000002
    Const STANDARD_RIGHTS_ALL = &H1F0000
    Const SYNCHRONIZE = &H100000
    Const READ_CONTROL = &H20000
    Const STANDARD_RIGHTS_READ = (READ_CONTROL)
    Const STANDARD_RIGHTS_WRITE = (READ_CONTROL)
    Const KEY_CREATE_LINK = &H20
    Const KEY_CREATE_SUB_KEY = &H4
    Const KEY_ENUMERATE_SUB_KEYS = &H8
    Const KEY_NOTIFY = &H10
    Const KEY_QUERY_VALUE = &H1
    Const KEY_SET_VALUE = &H2
    Const KEY_READ = ((STANDARD_RIGHTS_READ Or KEY_QUERY_VALUE Or KEY_ENUMERATE_SUB_KEYS Or KEY_NOTIFY) And (Not SYNCHRONIZE))
    Const KEY_WRITE = ((STANDARD_RIGHTS_WRITE Or KEY_SET_VALUE Or KEY_CREATE_SUB_KEY) And (Not SYNCHRONIZE))
    Const KEY_EXECUTE = (KEY_READ)
    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 Declare Function RegCloseKey Lib "advapi32.dll" (ByVal hKey As Long) As Long
    Private Declare Function RegOpenKeyEx Lib "advapi32.dll" Alias "RegOpenKeyExA" (ByVal hKey As Long, ByVal lpSubKey As String, ByVal Reserved As Long, ByVal samDesired As Long, phklKey As Long) As Long

    Private Sub Example()

    Debug.Print KeyExists("SOFTWARE\Microsoft\.NETFramework\InstallRoot")

    End Sub

    Private Function KeyExists(sKey As String) As Boolean

    Dim lKey As Long

    Call RegOpenKeyEx(HKEY_LOCAL_MACHINE, sKey, 0, KEY_ALL_ACCESS, lKey)
    If lKey Then
    KeyExists = True
    Call RegCloseKey(lKey)
    End If

    End Function

  9. #9

    Thread Starter
    New Member
    Join Date
    Aug 2006
    Posts
    13

    Re: simple question for a vb6 guy

    WOOPS.. I mean this always returns FALSE

    Quote Originally Posted by project2501
    Using this code It always returns "installed" (True) ... what am I doing wrong?

    Const HKEY_LOCAL_MACHINE = &H80000002
    Const STANDARD_RIGHTS_ALL = &H1F0000
    Const SYNCHRONIZE = &H100000
    Const READ_CONTROL = &H20000
    Const STANDARD_RIGHTS_READ = (READ_CONTROL)
    Const STANDARD_RIGHTS_WRITE = (READ_CONTROL)
    Const KEY_CREATE_LINK = &H20
    Const KEY_CREATE_SUB_KEY = &H4
    Const KEY_ENUMERATE_SUB_KEYS = &H8
    Const KEY_NOTIFY = &H10
    Const KEY_QUERY_VALUE = &H1
    Const KEY_SET_VALUE = &H2
    Const KEY_READ = ((STANDARD_RIGHTS_READ Or KEY_QUERY_VALUE Or KEY_ENUMERATE_SUB_KEYS Or KEY_NOTIFY) And (Not SYNCHRONIZE))
    Const KEY_WRITE = ((STANDARD_RIGHTS_WRITE Or KEY_SET_VALUE Or KEY_CREATE_SUB_KEY) And (Not SYNCHRONIZE))
    Const KEY_EXECUTE = (KEY_READ)
    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 Declare Function RegCloseKey Lib "advapi32.dll" (ByVal hKey As Long) As Long
    Private Declare Function RegOpenKeyEx Lib "advapi32.dll" Alias "RegOpenKeyExA" (ByVal hKey As Long, ByVal lpSubKey As String, ByVal Reserved As Long, ByVal samDesired As Long, phklKey As Long) As Long

    Private Sub Example()

    Debug.Print KeyExists("SOFTWARE\Microsoft\.NETFramework\InstallRoot")

    End Sub

    Private Function KeyExists(sKey As String) As Boolean

    Dim lKey As Long

    Call RegOpenKeyEx(HKEY_LOCAL_MACHINE, sKey, 0, KEY_ALL_ACCESS, lKey)
    If lKey Then
    KeyExists = True
    Call RegCloseKey(lKey)
    End If

    End Function

  10. #10
    VB Addict Pradeep1210's Avatar
    Join Date
    Apr 2004
    Location
    Inside the CPU...
    Posts
    6,614

    Re: simple question for a vb6 guy

    That's because you forgot to put an Exit Sub.

    VB Code:
    1. Function checkinstalled()
    2. On Error GoTo ErrorHandler
    3.  
    4. Dim WSH, result, theval
    5. Set WshShell = CreateObject("WScript.Shell")
    6.  
    7. theval = WshShell.RegRead("HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\.NETFramework\InstallRoot")
    8. If theval <> "" Then
    9. checkinstalled = "installed"
    10. Else
    11. checkinstalled = "not"
    12. End If
    13.  
    14. [B]Exit Function[/B]
    15. ErrorHandler:
    16. checkinstalled = "not"
    17.  
    18. End Function

    Edit: Corrected

    Pradeep
    Last edited by Pradeep1210; Nov 16th, 2006 at 01:10 PM.
    Pradeep, Microsoft MVP (Visual Basic)
    Please appreciate posts that have helped you by clicking icon on the left of the post.
    "A problem well stated is a problem half solved." — Charles F. Kettering

    Read articles on My Blog101 LINQ SamplesJSON ValidatorXML Schema Validator"How Do I" videos on MSDNVB.NET and C# ComparisonGood Coding PracticesVBForums Reputation SaverString EnumSuper Simple Tetris Game


    (2010-2013)
    NB: I do not answer coding questions via PM. If you want my help, then make a post and PM me it's link. If I can help, trust me I will...

  11. #11

    Thread Starter
    New Member
    Join Date
    Aug 2006
    Posts
    13

    Re: simple question for a vb6 guy

    why would the code proceed through the ErrorHandler: code if there was no error?

  12. #12
    VB Addict Pradeep1210's Avatar
    Join Date
    Apr 2004
    Location
    Inside the CPU...
    Posts
    6,614

    Re: simple question for a vb6 guy

    Quote Originally Posted by project2501
    why would the code proceed through the ErrorHandler: code if there was no error?
    b'coz u didn't tell it to stop before that
    Pradeep, Microsoft MVP (Visual Basic)
    Please appreciate posts that have helped you by clicking icon on the left of the post.
    "A problem well stated is a problem half solved." — Charles F. Kettering

    Read articles on My Blog101 LINQ SamplesJSON ValidatorXML Schema Validator"How Do I" videos on MSDNVB.NET and C# ComparisonGood Coding PracticesVBForums Reputation SaverString EnumSuper Simple Tetris Game


    (2010-2013)
    NB: I do not answer coding questions via PM. If you want my help, then make a post and PM me it's link. If I can help, trust me I will...

  13. #13
    Frenzied Member litlewiki's Avatar
    Join Date
    Dec 2005
    Location
    Zeta Reticuli Distro:Ubuntu Fiesty
    Posts
    1,162

    Re: simple question for a vb6 guy

    ErrorHandlers a label that you specified.The code will follow its normal course untill you put an end to it!
    __________________
    ________________0îîî___
    ___îîî0________(___)____
    __(___)_________) _/_____
    ___\_ (_________(_/______
    ____\_)_________________

  14. #14
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,687

    Re: simple question for a vb6 guy

    And since it's a Function is should be Exit Function.... not Exit Sub (shame on you Pradeep1210!)

    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  15. #15
    VB Addict Pradeep1210's Avatar
    Join Date
    Apr 2004
    Location
    Inside the CPU...
    Posts
    6,614

    Re: simple question for a vb6 guy

    Quote Originally Posted by techgnome
    And since it's a Function is should be Exit Function.... not Exit Sub (shame on you Pradeep1210!)

    -tg
    Thanks

    (It was such a small declaration that I took it for a Sub. )
    I always specify the return type of my functions even if they return variants and much used to see that syntax.


    project2501:
    Besides changing "Exit Sub" to Exit Function, You should modify the declaration to include return type.
    VB Code:
    1. Function checkinstalled()[B] as String[/B]


    Pradeep
    Pradeep, Microsoft MVP (Visual Basic)
    Please appreciate posts that have helped you by clicking icon on the left of the post.
    "A problem well stated is a problem half solved." — Charles F. Kettering

    Read articles on My Blog101 LINQ SamplesJSON ValidatorXML Schema Validator"How Do I" videos on MSDNVB.NET and C# ComparisonGood Coding PracticesVBForums Reputation SaverString EnumSuper Simple Tetris Game


    (2010-2013)
    NB: I do not answer coding questions via PM. If you want my help, then make a post and PM me it's link. If I can help, trust me I will...

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