PDA

View Full Version : Reading Registry Values (W98)



omuralievemil
07-05-2002, 02:51 AM
I have taken the sample code from this website to read registry values. Here it is:

' Start code
Option Explicit

Dim WSHShell, RegKey, RegKey1, OperatingSystem, Owner, Processor1, ProcessorArch

Set WSHShell = CreateObject("WScript.Shell")

RegKey = "HKLM\Software\Microsoft\Windows\CurrentVersion\"
RegKey1="HKLM\Hardware\Description\System\CentralProcessor\ 0\"

OperatingSystem = WSHShell.RegRead(RegKey & "ProductName")
Owner = WSHShell.RegRead(RegKey & "RegisteredOwner")

Processor1=WSHShell.RegRead(RegKey1 & "VendorIdentifier")
ProcessorArch=WSHShell.RegRead(RegKey1 & "Identifier")

WScript.echo "OperatingSystem: " & OperatingSystem & vbNewline &_
"Registered Owner: " & Owner & vbNewline &_
"Processor: " & Processor1 & vbNewline &_
"Processor Architecture: " & ProcessorArch

' End code

The code works nicely only if the values are present in the registry. If at least one registry value can not be found (the key or the value is not in the registry), it throws an error:
Error: Unable to open registry key
Code: 80070002

I would appreciate for help what to add into my code to avoid this error and just skip this registry value if it is not present in the registry. I suspect it has something to do with conditional If..........Then....., but don't know how to write it.

Thanks,
Emil

Andy-S
07-05-2002, 03:11 AM
Emil,

The easiest way is to put the statement: On Error Resume Next after the Option Explicit.



Cheers
Andy

omuralievemil
07-05-2002, 03:37 AM
Thanks a lot. That is nice. No errors any more. But one more thing. Windows Script Host window that pops up has then the blank entry, say "Operating System: ", which is blank. Is there any way to skip the whole scentence if the registry value can not be found?

Andy-S
07-05-2002, 05:19 PM
This is probably not the best way to do it but as I have no scripting experience it's al I could come up with. Maybe Jama or one of the other experts will have a better way.

' Start code
Option Explicit
On Error Resume Next


Dim WSHShell, RegKey, RegKey1, OperatingSystem, Owner, Processor1, ProcessorArch, NoDisplay

Set WSHShell = CreateObject("WScript.Shell")

RegKey = "HKLM\Software\Microsoft\Windows\CurrentVersion\"
RegKey1="HKLM\Hardware\Description\System\CentralProcessor\ 0\"

OperatingSystem = WSHShell.RegRead(RegKey & "ProductName")
Owner = WSHShell.RegRead(RegKey & "RegisteredOwner")

If OperatingSystem = "" Then
NoDisplay = 1
Else
WScript.echo "OperatingSystem: " & OperatingSystem & vbNewline &_
"Registered Owner: " & Owner & vbNewline
End If

NoDisplay = 0
Processor1=WSHShell.RegRead(RegKey1 & "VendorIdentifier")
ProcessorArch=WSHShell.RegRead(RegKey1 & "Identifier")

If Processor1 = "" Then
NoDisplay = 1
Else

WScript.echo "Processor: " & Processor1 & vbNewline &_
"Processor Architecture: " & ProcessorArch
End If
' End code


Cheers
Andy

omuralievemil
07-06-2002, 02:28 AM
That's great. Thanks, Andy. This code should do it.

Emil

Andy-S
07-06-2002, 03:04 AM
No problem Emil. Glad to help.

Cheers
Andy

adamdelves
07-09-2002, 09:26 AM
It's best not to turn off error handling globally. This can lead to problems as no errors are picked up at all. E.g. a miss spelt variable will not raise an error even if you have Option Explicit at the top of the script. It is best to create a function and turn off error handling inside it. This will only then affect code in your function and not in the script. Secondly - only trap the error you are lokking for, in this case 80070002. Again if there are any errors inside the function these will be picked up. There may also be other reasons why the registry key was not read.

<pre>
Option Explicit

Const ERR_NO_KEY = &h80070002

Dim strKey
Dim strValue
Dim shell

set shell = CreateObject("wscript.shell")
strKey = "HKCU\MyKey\MyValue"

If keyExists(strKey) then
'the key exists - read the value
strValue = shell.RegRead(strKey)
End If

Function keyExists(byVal strHKey)
On Error Resume Next

shell.regRead strHKey

if err.number = ERR_NO_KEY Then
' key / value does not exist or key has no
' default value set
keyExists = False
elseIf err.number &lt;&gt; 0 then
' another error occured - display it as it might
' be important
err.raise err.number
else
' no error occured value was read successfully
keyExists = True
end if
End Function
</pre>