|
-
Nov 15th, 2006, 12:26 AM
#1
Thread Starter
New Member
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
-
Nov 15th, 2006, 12:29 AM
#2
Re: simple question for a vb6 guy
You could use...
VB Code:
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
result = "installed"
Else
result = "not"
End If
Exit Function
ErrorHandler:
End Function
-
Nov 15th, 2006, 09:40 AM
#3
Thread Starter
New Member
Re: simple question for a vb6 guy
 Originally Posted by DigiRev
You could use...
VB Code:
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
result = "installed"
Else
result = "not"
End If
Exit Function
ErrorHandler:
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!
-
Nov 15th, 2006, 11:26 AM
#4
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:
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
-
Nov 15th, 2006, 11:59 AM
#5
Thread Starter
New Member
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!
-
Nov 15th, 2006, 12:10 PM
#6
Re: simple question for a vb6 guy
 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:
checkinstalled = "installed" '' not result = "installed"
Pradeep
-
Nov 15th, 2006, 01:50 PM
#7
Thread Starter
New Member
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)
-
Nov 15th, 2006, 10:40 PM
#8
Thread Starter
New Member
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
-
Nov 15th, 2006, 10:42 PM
#9
Thread Starter
New Member
Re: simple question for a vb6 guy
WOOPS.. I mean this always returns FALSE
 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
-
Nov 16th, 2006, 11:29 AM
#10
Re: simple question for a vb6 guy
That's because you forgot to put an Exit Sub.
VB Code:
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
[B]Exit Function[/B]
ErrorHandler:
checkinstalled = "not"
End Function
Edit: Corrected
Pradeep
Last edited by Pradeep1210; Nov 16th, 2006 at 01:10 PM.
-
Nov 16th, 2006, 12:38 PM
#11
Thread Starter
New Member
Re: simple question for a vb6 guy
why would the code proceed through the ErrorHandler: code if there was no error?
-
Nov 16th, 2006, 12:47 PM
#12
Re: simple question for a vb6 guy
 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
-
Nov 16th, 2006, 12:48 PM
#13
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________(___)____
__(___)_________) _/_____
___\_ (_________(_/______
____\_)_________________
-
Nov 16th, 2006, 12:51 PM
#14
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
-
Nov 16th, 2006, 01:07 PM
#15
Re: simple question for a vb6 guy
 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:
Function checkinstalled()[B] as String[/B]
Pradeep
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|