How can I read registry values? 
Author Message
 How can I read registry values?

I tried the following (for example):
' VBScript.
Set Sh = CreateObject("WScript.Shell")
key = "HKEY_CURRENT_USER\"
WScript.Echo Sh.RegRead(key & "RemoteAccess\")

But the output is:
impossible to access...
code 80070002



Thu, 26 May 2005 01:57:01 GMT  
 How can I read registry values?

Quote:

> I tried the following (for example):
> ' VBScript.
> Set Sh = CreateObject("WScript.Shell")
> key = "HKEY_CURRENT_USER\"
> WScript.Echo Sh.RegRead(key & "RemoteAccess\")

> But the output is:
> impossible to access...
> code 80070002

Reading a registry key that exists but has no default value throws the same
err.number as a key that doesn't exist at all.  They only differ in the text
of the error description.

Here's how you can tell the difference.  Note that on localized language
versions of WSH, the text of the err.description will probably be
non-English, so adjust accordingly.  This example assumes an English version
of WSH...

Set Sh = CreateObject("WScript.Shell")
key = "HKEY_CURRENT_USER\"

on error resume next
value = Sh.RegRead(key & "RemoteAccess\")
if err then
  clue = split(err.description, " ",2)(0)
  select case lcase(clue)
    case "unable" : value = "Key exists with no default value"
    case "invalid" : value = "Key doesn't exist"
    case else : value = "Error: " & err.description
  end select
end if
on error goto 0

WScript.Echo value

--
Michael Harris
Microsoft.MVP.Scripting
Seattle WA US



Thu, 26 May 2005 04:18:12 GMT  
 How can I read registry values?
Thank you but the output is "Error impossible to open HKCU ..."


Thu, 26 May 2005 05:15:17 GMT  
 How can I read registry values?


: Thank you but the output is "Error impossible to open HKCU ..."

However I would like to read registry values,is it possible?
I'm searching for a function like reg query in DOS.



Thu, 26 May 2005 05:46:34 GMT  
 How can I read registry values?

Quote:

> Thank you but the output is "Error impossible to open HKCU ..."

I assume this is your English translation of an Italian error message.  I'd
work this out myself with SetLocale "it" except that SetLocale has no effect
on these error messages - I know that because I tried <g>!

Create a simple test case that reads in key with no default value and also a
non-existent key.  Look at what the err.description is in each case and
adjust the logic that extracts the clue from the language specific
err.description.

If you post the err.description here in Italian for both cases, we can work
out the logic to distinguish one case from the other.

--
Michael Harris
Microsoft.MVP.Scripting
Seattle WA US



Thu, 26 May 2005 06:28:22 GMT  
 How can I read registry values?
I've found this function to read subkeys but it doesn't work,could you
tell me how to modify it? The problem should be UBound.
Thanks

Set WshShell = WScript.CreateObject("WScript.Shell")
Set oReg=GetObject("winmgmts:!root/default:StdRegProv")

Function EnumKey(Key, SubKey)
     Dim Ret()
     oReg.EnumKey Key,SubKey, sKeys
     ReDim Ret(UBound(sKeys))

     For Count = 0 to UBound(sKeys)
          Ret(Count) = sKeys(Count)
     Next

     EnumKey = Join(Ret,vbCrLf)
End Function



Thu, 26 May 2005 08:41:16 GMT  
 How can I read registry values?

Quote:

> I've found this function to read subkeys but it doesn't work,could you
> tell me how to modify it? The problem should be UBound.

Set oReg=GetObject("winmgmts:!root/default:StdRegProv")
const HKCR = &H80000000 'HKEY_CLASSES_ROOT
const HKCU = &H80000001 'HKEY_CURRENT_USER
const HKLM = &H80000002 'HKEY_LOCAL_MACHINE
const HKU  = &h80000003 'HKEY_USERS
const HKCC = &h80000005 'HKEY_CURRENT_CONFIG
const HKDD = &h80000006 'HKEY_DYN_DATA

sKey = "Software\Microsoft\Windows\CurrentVersion"

values = EnumKey(HKCU, sKey)
if not IsNull(values) Then
  msgbox values
end if

Function EnumKey(Hive, Key)
  Dim rc, arKeys
  EnumKey = null
  rc = oReg.EnumKey(Hive, Key, arSubKeys)
  select case rc    
    case 0 : EnumKey = Join(arSubKeys,vbCrLf)
    case 1 : MsgBox "RC=1 Key exists, but default or named value not found"
    case 2 : MsgBox "RC=2 Key not found"
    case 6 : MsgBox "RC=6 Invalid hive"
    case else : MsgBox "Unexpected RC=" & rc
  End Select
End Function

StdRegProv
http://msdn.microsoft.com/library/en-us/wmisdk/wmi/stdregprov.asp

Script Center
http://www.microsoft.com/technet/scriptcenter/default.asp

Registry
http://www.microsoft.com/technet/scriptcenter/registry/default.asp

Enumerate Subkeys
http://www.microsoft.com/technet/scriptcenter/registry/ScrReg09.asp

--
Michael Harris
Microsoft.MVP.Scripting
Seattle WA US



Thu, 26 May 2005 10:07:05 GMT  
 How can I read registry values?
Thank you,very interesting links.I have two further questions:
1)
When I run the following script an error message appear(Errore: Tipo non corrispondente: 'UBound') because Software\Microsoft has no
default
value.How should I apply your script in this case?
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''

Const REG_SZ=1
Const REG_EXPAND_SZ=2
Const REG_BINARY=3
Const REG_DWORD=4
Const REG_MULTI_SZ=7
Const HKCU=&H80000001 'HKEY_CURRENT_USER

Set WshShell = WScript.CreateObject("WScript.Shell")
Set oReg=GetObject("winmgmts:!root/default:StdRegProv")

Function EnumValues(Key, SubKey)
     Dim Ret()
     oReg.EnumValues Key,SubKey, sKeys, iKeyType 'fill the array
     ReDim Ret(UBound(sKeys))

     For Count = 0 to UBound(sKeys)
          Select Case iKeyType(Count)
               Case REG_SZ
                    oReg.GetStringValue Key,SubKey, sKeys(Count), sValue
                    Ret(Count) = sKeys(Count) & "," & "REG_SZ" & "," & sValue
               Case REG_EXPAND_SZ
                    oReg.GetExpandedStringValue Key,SubKey, sKeys(Count), sValue
                    Ret(Count) = sKeys(Count) & "," & "REG_EXPAND_SZ" & "," & sValue
               Case REG_BINARY
                    oReg.GetBinaryValue Key,SubKey, sKeys(Count), aValue
                    Ret(Count) = sKeys(Count) & "," & "REG_BINARY" & "," & Join(aValue,"")
               Case REG_DWORD
                    oReg.GetDWORDValue Key,SubKey, sKeys(Count), lValue
                    Ret(Count) = sKeys(Count) & "," & "REG_DWORD" & "," & lValue
               Case REG_MULTI_SZ
                    oReg.GetMultiStringValue Key,SubKey, sKeys(Count), sValue
                    Ret(Count) = sKeys(Count) & "," & "REG_MULTI_SZ" & "," & Join(sValue,"")
          End Select
     Next
     EnumValues = Join(Ret,vbCrLf)
End Function

wscript.echo EnumValues(HKCU, "Software\Microsoft")

''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''

2)
I have found the following script and I would like to know
what does "vbCrLf _" and "strMsg = strMsg _" mean.
Thanks a lot.

''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
'''''''''''''

strComputer = "."
Set objWMIService = GetObject("winmgmts:" _
    & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
Set colBIOS = objWMIService.ExecQuery _
    ("Select * from Win32_BIOS")
For each objBIOS in colBIOS
strMsg = strMsg _
  &   "Build Number: " & objBIOS.BuildNumber & vbCrLf _
  &   "Current Language: " & objBIOS.CurrentLanguage & vbCrLf _
Next

MsgBox strMsg
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
''''''''



Fri, 27 May 2005 02:18:52 GMT  
 How can I read registry values?

: 2)
: I have found the following script and I would like to know
: what does "vbCrLf _" and "strMsg = strMsg _" mean.
: Thanks a lot.

Now I understand what it is its meaning



Fri, 27 May 2005 05:46:35 GMT  
 How can I read registry values?
The last question:
I would like to use the function EnumValues with
InputBox to enter the name of a key in the form
HKEY_CURRENT_USER\Software\Microsoft
Is it possible?


Fri, 27 May 2005 07:30:31 GMT  
 How can I read registry values?

Quote:

> Thank you,very interesting links.I have two further questions:
> 1)
> When I run the following script an error message appear(Errore: Tipo
> non corrispondente: 'UBound') because Software\Microsoft has no
> default value.How should I apply your script in this case?

When I run the script you posyed, I get no errors.

The key Software\Microsoft in the HKCU hive has no named values to enumerate
and the key itself has no default (unnamed) value.  So all you is get an
empty array of value names returned in sKeys.

Function EnumValues(Key, SubKey)
     Dim Ret()
     oReg.EnumValues Key,SubKey, sKeys, iKeyType 'fill the array
     ReDim Ret(UBound(sKeys))
     '=====
     ' if sKeys is an empty array UBound(sKeys) returns -1,
     ' so Ret is ReDim'd to another empty array...
     '=====
     wscript.echo "ubound(sKeys)", ubound(sKeys)
     wscript.echo "ubound(Ret)", ubound(Ret)
     ...
     ..
     .
End Function

Suggestion: sKeys and iKeyType aren't very good variable names to use here.
arValueNames and arValueTypes would be more descriptive given you are call
EnumValues and both arguments return arrays.

--
Michael Harris
Microsoft.MVP.Scripting
Seattle WA US



Fri, 27 May 2005 09:11:24 GMT  
 How can I read registry values?

Quote:

> The last question:
> I would like to use the function EnumValues with
> InputBox to enter the name of a key in the form
> HKEY_CURRENT_USER\Software\Microsoft
> Is it possible?

Here's a snippet of code that shows how to get input from the user and
validate it, converting the portion of the string that specifies the hive
into the ciorrect numeric (hex) value (e.g., "HKCU" or "HKEY_CURRENT_USER"
to the HKCU hex value of &H80000001).

const HKCR = &H80000000 'HKEY_CLASSES_ROOT
const HKCU = &H80000001 'HKEY_CURRENT_USER
const HKLM = &H80000002 'HKEY_LOCAL_MACHINE
const HKU  = &h80000003 'HKEY_USERS
const HKCC = &h80000005 'HKEY_CURRENT_CONFIG
const HKDD = &h80000006 'HKEY_DYNAMIC_DATA

sReply = InputBox("Enter key to enumerate...",,"HKEY_CURRENT_USER\Software")

if trim(sReply) = "" then wscript.quit

arWork = Split(sReply,"\",2)

if ubound(arWork) <> 1 then
  msgbox "Expected hive\key or hive\key\subkey\... format"
  wscript.quit
end if

sHive = arWork(0)
sKey = arWork(1)

select case ucase(sHive)
  case "HKCR", "HKEY_CLASSES_ROOT"   : hive = HKCR
  case "HKCU", "HKEY_CURRENT_USER"   : hive = HKCU
  case "HKLM", "HKEY_LOCAL_MACHINE"  : hive = HKLM
  case "HKU" , "HKEY_USERS"          : hive = HKU
  case "HKCC", "HKEY_CURRENT_CONFIG" : hive = HKCC
  case "HKDD", "HKEY_DYNAMIC_DATA"   : hive = HKDD
  case else
    msgbox "unrecognized hive: " & sHive
    wscript.quit
end select

wscript.echo "hive=",hex(hive)
wscript.echo "key=",sKey

msgbox "OK"

--
Michael Harris
Microsoft.MVP.Scripting
Seattle WA US



Fri, 27 May 2005 09:28:33 GMT  
 How can I read registry values?


: > Thank you,very interesting links.I have two further questions:
: > 1)
: > When I run the following script an error message appear(Errore: Tipo
: > non corrispondente: 'UBound') because Software\Microsoft has no
: > default value.How should I apply your script in this case?
: >
:
: When I run the script you posyed, I get no errors.

Thank you very much for your answers.However when I run
the script with Software\Microsoft I get the error:
Tipo non corrispondente: 'UBound'



Fri, 27 May 2005 22:00:14 GMT  
 How can I read registry values?

Quote:




>>> Thank you,very interesting links.I have two further questions:
>>> 1)
>>> When I run the following script an error message appear(Errore: Tipo
>>> non corrispondente: 'UBound') because Software\Microsoft has no
>>> default value.How should I apply your script in this case?

>> When I run the script you posyed, I get no errors.

> Thank you very much for your answers.However when I run
> the script with Software\Microsoft I get the error:
> Tipo non corrispondente: 'UBound'

What OS and service pack?

Works fine for me on W2K Pro sp2.

--
Michael Harris
Microsoft.MVP.Scripting
Seattle WA US



Sat, 28 May 2005 00:53:17 GMT  
 How can I read registry values?




: >>> Thank you,very interesting links.I have two further questions:
: >>> 1)
: >>> When I run the following script an error message appear(Errore: Tipo
: >>> non corrispondente: 'UBound') because Software\Microsoft has no
: >>> default value.How should I apply your script in this case?
: >>>
: >>
: >> When I run the script you posyed, I get no errors.
: >
: > Thank you very much for your answers.However when I run
: > the script with Software\Microsoft I get the error:
: > Tipo non corrispondente: 'UBound'
:
: What OS and service pack?
:
: Works fine for me on W2K Pro sp2.

XP Home,however it's not a problem,I'll use
on error resume next.Thanks!



Sat, 28 May 2005 02:22:30 GMT  
 
 [ 16 post ]  Go to page: [1] [2]

 Relevant Pages 

1. Reading user's registry value

2. How to read registry values?

3. Read a value in a remote registry

4. Reading registry values containing a backslash (\)

5. How do i read registry values

6. How to read a value from the registry ...

7. How to read registry key containing many string values

8. How can I read registry value(s) with vbscript?

9. Reading registry values

10. Reading binary registry values using regobj.dll

11. Reading Registry values remotely

12. Reading/Writing Hex values from the registry

 

 
Powered by phpBB® Forum Software