[Tutor] Window Registry Access

Gooch, John John.Gooch at echostar.com
Thu Oct 28 17:00:22 CEST 2004


Here is what I wound up doing: 

 hHandle = win32api.RegConnectRegistry( "\\\\"+currentRec.name[0],
win32con.HKEY_LOCAL_MACHINE)
 hHandle = win32api.RegOpenKeyEx( win32con.HKEY_LOCAL_MACHINE,
"SOFTWARE\\Microsoft\\Windows
NT\\CurrentVersion\\WinLogon",0,win32con.KEY_ALL_ACCESS)
 hNoOfKeys = win32api.RegQueryInfoKey(hHandle)[1]
 hCounter = 0
 while hCounter < hNoOfKeys:           
 	hData = win32api.RegEnumValue(hHandle,hCounter)
 	if hData[0]== "DefaultUserName":
      	username = hData[1]
            hCounter = hCounter + 1
if username:
	if debug: print "Last Logged in User is "+username+"\n"
	currentRec.lastlogon = username  


If works great, but I noticed that on many of the functions in this API, the
return value is 'undefined', so it is very hard to avoid raising exceptions,
so I have put try/except blocks around all of the i/o parts of my script. In
Perl, the return type was always defined so I would catch an exception
similarly ( different syntax, of course ) to this:
	if ( hHandle = win32api.RegConnectRegistry(
"\\\\"+currentRec.name[0], win32con.HKEY_LOCAL_MACHINE) ) {
		use the connection
	}
	else {
		log fact that connection failed and continue on to next code
block
	}



John A. Gooch
Systems Administrator
IT - Tools
EchoStar Satellite L.L.C.
9601 S. Meridian Blvd.
Englewood, CO  80112
Desk: 720-514-5708 


-----Original Message-----
From: Alan Gauld [mailto:alan.gauld at freenet.co.uk] 
Sent: Thursday, October 28, 2004 1:55 AM
To: Gooch, John; tutor at python.org
Subject: Re: [Tutor] Window Registry Access



> I am attempting to read information from a remote registry using
Python,

I'd recommend using WSH for this. You can access the WSH objects using the
Python win extensions as usual.

Here is some VBSCript sample code, converting to Python should be easy
...but I'm in a hurry :-(

Dim wshSHell
Set wshShell = CreateObject("WScript.Shell")

' Find the Windows OS version
MsgBox
wshShell.RegRead("HKLM\Software\Microsoft\Windows\CurrentVersion")
' CReate a new context menu item for Notepad
wshShell.RegWrite("HKCR\*\Shell\Notepad", "View with Notepad",
"REG_SZ")
wshShell.RegWrite("HKCR\*\Shell\Notepad\Command","%WINDIR%\notepad.exe
%","REG_EXPAND_SZ")

Its possible to do edits over the network too by giving a network path...
There are also networking objects within WSH...

Alan G.


More information about the Tutor mailing list