get registry information in python

James J. Besemer jb at cascade-sys.com
Wed Aug 14 07:40:20 CEST 2002



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

-------------- next part --------------

# 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()
-------------- next part --------------

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>>"
-------------- next part --------------

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