get registry information in python
Steven Nien
no-spam at invalide.com
Wed Aug 14 21:10:38 EDT 2002
Thank you for your reply. I know how to using win32api or _winreg get
registry data. For example:
import _winreg
host = "127.0.0.1"
key = _winreg.ConnectRegistry(host, _winreg.HKEY_LOCAL_MACHINE)
hkey = _winreg.OpenKey(key,
r"Software\Microsoft\Windows\CurrentVersion\Uninstall")
num = _winreg.QueryInfoKey(hkey)[0]
print "*** Found %d Software ***" % num
try:
for x in range(1000):
uninstall = _winreg.EnumKey(hkey, x)
skey = _winreg.OpenKey(key,
"Software\Microsoft\Windows\CurrentVersion\Uninstall\%s" % uninstall)
try:
software = _winreg.QueryValueEx(skey, "DisplayName")[0]
print "%s-%s" % (uninstall, software)
except:
pass
except:
pass
But the code can't run remotely for Win9x. (Win9x must install remotereg
from Win9x CD) So I try to use Microsoft Windows Management Instrumentation
(WMI) to get registry and hardware information.
I find scripts from Microsoft MSDN. For example:
HostName="localhost"
Dim oRegistry, sBaseKey, iRC, sKey, arSubKeys, sValue
Const HKLM = &H80000002 'HKEY_LOCAL_MACHINE
On Error Resume Next
Set oRegistry = _
GetObject("winmgmts:{ImpersonationLevel=Impersonate}//" & _
HostName & "/root/default:StdRegProv")
If Err <> 0 Then
wscript.echo "Line 9, " & Err.Number & ", " & _
Err.Description
Err.Clear
wscript.quit
End if
sBaseKey = "SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\"
iRC = oRegistry.EnumKey(HKLM, sBaseKey, arSubKeys)
If Err <> 0 Then
wscript.echo "Line 23, " & Err.Number & ", " & _
Err.Description
Err.Clear
wscript.quit
End if
For Each sKey In arSubKeys
iRC = oRegistry.GetStringValue(HKLM, sBaseKey & sKey, _
"DisplayName",sValue)
If Err <> 0 Then
wscript.echo "Line 32, " & Err.Number & ", " & _
Err.Description
Err.Clear
wscript.quit
End if
If iRC <> 0 Then
oRegistry.GetStringValue HKLM, sBaseKey & _
sKey, "QuietDisplayName", sValue
If Err <> 0 Then
wscript.echo "Line 42, " & Err.Number & ", " & _
Err.Description
Err.Clear
wscript.quit
End if
End If
If sValue <> "" Then
wscript.echo sValue
End If
Next
Run the script can display all uninstall software list. I don't
know how to use Python do it.
--
Steven Nien
"James J. Besemer" <jb at cascade-sys.com>
:mailman.1029303801.22934.python-list at python.org...
>
>
> Steven Nien wrote:
>
> > Hi,
> >
> > I try to get registry infomation with wmi in python.
> >
> > from win32com.client import GetObject
> > HostName="."
> > HKLM = 0x80000002L
> > oReg = GetObject("winmgmts:{impersonationLevel=impersonate}//" +
HostName +
> > "/root/default:StdRegProv")
> > sBaseKey = "SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall"
> > oReg.EnumKey(HKLM, sBaseKey, sKeys)
> > for sKey in sKeys:
> > print sKey
> >
> > But when I run the code, I get error message:
> >
> > Traceback (most recent call last):
> > File "D:\steven\ITIM\t1.py", line 6, in ?
> > oReg.EnumKey(HKLM, sBaseKey, sKeys)
> > NameError: name 'sKeys' is not defined
> >
> > I don't know why 'sKeys' is not defined. Could anybody help
> > me? Thanks!
>
> Because you use the variable before you assign to it.
>
> Without taking time to study and fully understand and debug
> your code snippet, it seems you want the oReg.EnumKey()
> call INSIDE the for loop. EnumKey does NOT return a list.
> Rather you call it once for each successive key with the third
> argument assuming successive integer values in xrange( number of
entries ).
>
> The number of entries can be retrieved via RegQueryInfoKey( key ).
>
> Attached are two example programs I wrote.
>
> One searches the registry for any and all keys or values that
> match a string.
>
> The other prints out a summary of windows file type extension
associations.
>
> Enjoy!
>
> --jb
>
> --
> James J. Besemer 503-280-0838 voice
> http://cascade-sys.com 503-280-0375 fax
> mailto:jb at cascade-sys.com
>
>
----------------------------------------------------------------------------
----
>
> # given a string, answer the musical question:
> # what registry keys, value names or string values
> # contain that particular string?
> #
> # Search may be confined to a single hive or it may
> # search all hives.
>
> import sys, os
> import win32api
> import _winreg
> import * from regconst
>
>
> stack = []
>
> def search1( root, keyname ):
>
> stack.append( root )
>
> key = win32api.RegOpenKeyEx( root, keyname )
>
> try:
>
> print keyname + ":"
>
> ( keycount, valcount, mdate ) = win32api.RegQueryInfoKey( key )
>
> print " Values:"
> for index in xrange( valcount ):
> vk, vd, vt = win32api.RegEnumValue( key, index )
> vd = regconst.RegValAsString( vd, vt )
> if not vk:
> vk = DefKeyNameSub
> print "\t%-20s %-25s" % ( vk, vd )
>
> print " SubKeys:"
> for index in xrange( keycount ):
> k = win32api.RegEnumKey( key, index )
> print "\t", k
>
> finally:
> win32api.RegCloseKey( key )
>
> stack.remove( root )
>
>
> def search( text, root = HKEY_LOCAL_MACHINE ):
> stack = []
> search1( text, root )
>
>
> def main():
>
> if len( sys.argv ) == 2:
>
> search( sys.argv[ 1 ], HKEY_CLASSES_ROOT )
> search( sys.argv[ 1 ], HKEY_CURRENT_USER )
> search( sys.argv[ 1 ], HKEY_LOCAL_MACHINE )
> search( sys.argv[ 1 ], HKEY_USERS )
> search( sys.argv[ 1 ], HKEY_CURRENT_CONFIG )
> search( sys.argv[ 1 ], HKEY_DYN_DATA )
>
> elif len( sys.argv ) == 3:
> try:
> root = HIVE_ABBR[ sys.argv[ 2 ]]
> except:
> return
>
> search( sys.argv[ 1 ], root )
>
> else:
> print "Syntax: RegSearch keyword [ HKEY=HKLM ]"
>
> main()
>
----------------------------------------------------------------------------
----
>
> import _winreg
>
> # hive key aliases
>
> HKEY_CLASSES_ROOT = _winreg.HKEY_CLASSES_ROOT
> HKEY_CURRENT_USER = _winreg.HKEY_CURRENT_USER
> HKEY_LOCAL_MACHINE = _winreg.HKEY_LOCAL_MACHINE
> HKEY_USERS = _winreg.HKEY_USERS
> HKEY_CURRENT_CONFIG = _winreg.HKEY_CURRENT_CONFIG
> HKEY_DYN_DATA = _winreg.HKEY_DYN_DATA
>
> ALL_HKEYS = [
> HKEY_CLASSES_ROOT,
> HKEY_CURRENT_USER,
> HKEY_LOCAL_MACHINE,
> HKEY_USERS,
> HKEY_CURRENT_CONFIG,
> HKEY_DYN_DATA,
> ]
>
> # reg value types
>
> REG_NONE = 0 # No value type
> REG_SZ = 1 # Unicode nul terminated string
> REG_EXPAND_SZ = 2 # Unicode nul terminated string
> # (with environment variable refere
> REG_BINARY = 3 # Free form binary
> REG_DWORD = 4 # 32-bit number
> REG_DWORD_LITTLE_ENDIAN = 4 # 32-bit number (same as REG_DWORD)
> REG_DWORD_BIG_ENDIAN = 5 # 32-bit number
> REG_LINK = 6 # Symbolic Link (unicode)
> REG_MULTI_SZ = 7 # Multiple Unicode strings
> REG_RESOURCE_LIST = 8 # Resource list in the resource map
> REG_FULL_RESOURCE_DESCRIPTOR = 9 # Resource list in the hardware des
> REG_RESOURCE_REQUIREMENTS_LIST = 10
>
> # map reg value type code to name
>
> RegValTypeName = {
> REG_NONE : REG_NONE,
> REG_SZ : REG_SZ,
> REG_EXPAND_SZ : REG_EXPAND_SZ,
> REG_BINARY : REG_BINARY,
> REG_DWORD : REG_DWORD,
> # REG_DWORD_LITTLE_ENDIAN : REG_DWORD_LITTLE_ENDIAN,
> REG_DWORD_BIG_ENDIAN : REG_DWORD_BIG_ENDIAN,
> REG_LINK : REG_LINK,
> REG_MULTI_SZ : REG_MULTI_SZ,
> REG_RESOURCE_LIST : REG_RESOURCE_LIST,
> REG_FULL_RESOURCE_DESCRIPTOR : REG_FULL_RESOURCE_DESCRIPTOR,
> REG_RESOURCE_REQUIREMENTS_LIST : REG_RESOURCE_REQUIREMENTS_LIST,
> }
>
>
> # hive abbreviations
>
> HIVE_ABBR = {
> "HKCR" : HKEY_CLASSES_ROOT,
> "HKCU" : HKEY_CURRENT_USER,
> "HKLM" : HKEY_LOCAL_MACHINE,
> "HKU" : HKEY_USERS,
> "HKCC" : HKEY_CURRENT_CONFIG,
> "HKDD" : HKEY_DYN_DATA,
> "HKEY_CLASSES_ROOT" : HKEY_CLASSES_ROOT,
> "HKEY_CURRENT_USER" : HKEY_CURRENT_USER,
> "HKEY_LOCAL_MACHINE" : HKEY_LOCAL_MACHINE,
> "HKEY_USERS" : HKEY_USERS,
> "HKEY_CURRENT_CONFIG" : HKEY_CURRENT_CONFIG,
> "HKEY_DYN_DATA" : HKEY_DYN_DATA,
> }
>
> HIVE_NAME = {
> HKEY_CLASSES_ROOT : "HKEY_CLASSES_ROOT",
> HKEY_CURRENT_USER : "HKEY_CURRENT_USER",
> HKEY_LOCAL_MACHINE : "HKEY_LOCAL_MACHINE",
> HKEY_USERS : "HKEY_USERS",
> HKEY_CURRENT_CONFIG : "HKEY_CURRENT_CONFIG",
> HKEY_DYN_DATA : "HKEY_DYN_DATA",
> }
>
> # misc consts
>
> DefKeyNameSub = "[Default]"
> DefKeyExtName = "@"
>
> # Convert reg val bytes (string) to vis string
>
> def RegValAsString( val, code ):
>
> if code == REG_SZ:
> return val
>
> if code == REG_EXPAND_SZ:
> return val
>
> if code == REG_DWORD:
> return ( "%d" % val )
>
> r = 0
> s = 0
> for ch in val:
> r |= ( ord( ch ) << s )
> s += 8
>
> return ( "%d" % r )
>
> if code == REG_DWORD_BIG_ENDIAN:
> r = 0
> s = 8 * 3
> for ch in val:
> r |= ( ord( ch ) << s )
> s -= 8
>
> return ( "0x%08x" % r )
>
> if code == REG_BINARY:
> r = ""
> for ch in val:
> r += ( "%02x " % ord( ch ))
> return r
>
> # punt:
>
> if code == REG_LINK:
> return "<<REG_LINK>>"
>
> if code == REG_MULTI_SZ:
> return "<<REG_MULTI_SZ>>"
>
> if code == REG_RESOURCE_LIST:
> return "<<REG_RESOURCE_LIST>>"
>
> if code == REG_FULL_RESOURCE_DESCRIPTOR:
> return "<<REG_FULL_RESOURCE_DESCRIPTOR>>"
>
> if code == REG_RESOURCE_REQUIREMENTS_LIST:
> return "<<REG_RESOURCE_REQUIREMENTS_LIST>>"
>
----------------------------------------------------------------------------
----
>
> import win32api
> import _winreg
> import regconst
>
> ROOT = _winreg.HKEY_CLASSES_ROOT
> root = ROOT
> DefKeyNameSub = "[Default]"
>
> index = 0
>
> def summarize1( root, keyname ):
>
> DefKeyName = None
> key = win32api.RegOpenKeyEx( root, keyname )
>
> try:
>
> print keyname + ":"
>
> ( keycount, valcount, mdate ) = win32api.RegQueryInfoKey( key )
>
> print " Values:"
> for index in xrange( valcount ):
> vk, vd, vt = win32api.RegEnumValue( key, index )
> vd = regconst.RegValAsString( vd, vt )
> if not vk:
> vk = DefKeyNameSub
> DefKeyName = vd
> print "\t%-20s %-25s" % ( vk, vd )
>
> print " SubKeys:"
> for index in xrange( keycount ):
> k = win32api.RegEnumKey( key, index )
> print "\t", k
>
> finally:
> win32api.RegCloseKey( key )
>
> return DefKeyName
>
> def summarize( root, keyname ):
>
> DefKeyName = summarize1( root, keyname )
>
> if DefKeyName:
> summarize1( root, DefKeyName )
>
> def main():
>
> ( keycount, valcount, mdate ) = win32api.RegQueryInfoKey( ROOT )
>
> keycount = min( keycount, 10 )
>
> for index in xrange( keycount ):
> keyname = win32api.RegEnumKey( ROOT, index )
> if keyname[0] == '.':
> summarize( ROOT, keyname )
>
>
> main()
>
>
>
More information about the Python-list
mailing list