Difference between revisions of "Winreg"
Jump to navigation
Jump to search
PeterHarding (talk | contribs) |
PeterHarding (talk | contribs) |
||
Line 140: | Line 140: | ||
[[Category:WinXP]] | [[Category:WinXP]] | ||
[[Category:Python]] | [[Category:Python]] | ||
[[Category: | [[Category:Cygwin]] | ||
[[Category:PyWin]] | [[Category:PyWin]] |
Latest revision as of 17:59, 23 October 2009
Overview
Also see _winreg.py
See:
- http://tgolden.sc.sabren.com/python/wmi.html for WMI interface...
- http://tgolden.sc.sabren.com/python/win32_how_do_i.html
- http://tgolden.sc.sabren.com/python/win32_how_do_i/check-a-users-credentials.html
- http://www.thescripts.com/forum/thread598226-_winreg.html
I use _winreg to find out the path of the exe:
Code: ( text ) try: ### Read registry for InstaCal path instacalKey = _winreg.OpenKey(_winreg.HKEY_LOCAL_MACHINE, "SOFTWARE\Universal Library") self.pmdPath = str(_winreg.QueryValueEx(instacalKey, 'RootDirs')[0]) instacalKey.Close() except WindowsError: self.InstaCalBtn.Disable() self.pmdPath = ""
then:
Code: ( text )
def OnInstaCalButton(self, event): os.spawnl(os.P_DETACH, self.pmdPath + "inscal32.exe")
Need more info on the time between numbers, but you might want to check out the time module.
http://www.thescripts.com/forum/thread575308-_winreg.html
For those of you who have never used the *listofargs and **dictofargs syntax, here is a look at the latter. By using **kwargs, a dictionary of any size is sent into the fuction as dict(a=1, b=2). Inside the function, it's just a dictionary named kwargs (or whaterver, but this is conventional). The truly amazing thing to me is that I am able to write to the Windows registry with so little interface!
Code: ( text ) """Encapuslate a Default Values Object and Config File""" from inspect import getmembers import wx class DefaultValueHolder(object): """Intended for use with wxConfig (or maybe _winreg) to set up and/or get registry key names and values. Name attrs as default*. "default" will be stripped of when reading and writing to the config file""" ## defaultTestStr = "this is a test" ## defaultTestInt = 1 def __init__(self, appName, grpName): """The name of the config file that will be created""" self.appName = appName # if the key doesn't exit, it will be created self.grpName = grpName # same goes for groups. self.config = wx.Config(appName) # Open the file (HKCU in windows registry) def GetVariables(self): return [{"name":var[0][7:], "value":var[1], "type":type(var[1])} for var in getmembers(self) if var[0].startswith('default')] def SetVariables(self, **kwargs): for name, value in kwargs.items(): setattr(self, "default" + name, value) def InitFromConfig(self): self.config.SetPath(self.grpName) for var in self.GetVariables(): if var['type'] == str: self.config.Write(var['name'], var['value']) elif var['type'] == int: self.config.WriteInt(var['name'], var['value']) elif var['type'] == float: self.config.WriteFloat(var['name'], var['value']) if __name__ == "__main__": test = DefaultValueHolder("MyAppName", "Database") test.SetVariables(UserName = "joe", Password = "", ServerName = "MyServer") #### this also works: ## test.defaultUserName = "joe" test.InitFromConfig()
Registry Lookup
#!/usr/bin/echo THIS_IS_ONLY_FOR_WINDOWS_PYTHON from _winreg import * from os import environ class User: def __init__(self,masterkey,sid): self.sid = sid userkey = OpenKey(masterkey,sid) self.data = {} for valueno in range(QueryInfoKey(userkey)[1]): (name,data,type) = EnumValue(userkey,valueno) self.data[name] = data userkey.Close() profilePath = self.data['ProfileImagePath'].split("%") for i in range(1,len(profilePath),2): profilePath[i] = environ[profilePath[i]] self.profilePath = "".join(profilePath) users = [] masterkey = OpenKey(HKEY_LOCAL_MACHINE,"SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion\\ProfileList") for userno in range(QueryInfoKey(masterkey)[0]): users.append(User(masterkey,EnumKey(masterkey,userno))) masterkey.Close() profilePath = environ["USERPROFILE"] userName = environ["USERNAME"] userDomain = environ["USERDOMAIN"] for user in users: if user.profilePath == profilePath: # Not clear what to use for group ID. Is 513 universally OK? # This assumes you want your home directory in /home/userName print ":".join([userName, \ "unused_by_nt/2000/xp", \ user.sid.split("-")[-1], "513", \ "U-" + userDomain + "\\" + userName + "," + user.sid, \ "/home/" + userName, \ "/bin/bash"])