[python-win32] python-win32 Digest, Vol 58, Issue 40

Mike Driscoll mdriscoll at co.marshall.ia.us
Thu Jan 24 21:05:17 CET 2008


Hi,

> thunder thunder54007 wrote:
> > I got it, I should get an instance first before i can use
> it, and here is
> > another problem, when I use Win32_Product to fetch the
> installed software on
> > my system, but only some of them are returned, I don't know
> why? how can i
> > get all the installed software on my system?
> 
> (Please keep copying back to this; you'll get more help that way)
> 
> That's a more general windows / Win32_Product question. If I remember 
> correctly, Win32_Product only lists products installed in a certain 
> way (probably via .MSI). Win32_SoftwareElement or associated classes 
> [1] may give you something. After that, you're just down to scraping 
> the registry, I think. I'm afraid this isn't really my area of 
> expertise. (He says, as though he *has* an area of expertise :)
> 
> TJG
> 
> [1] http://msdn2.microsoft.com/en-us/library/aa390887(VS.85).aspx
> 
> 
> ------------------------------

I had to write a script that was supposed to get all the installed
software on any system. There is no way to get everything because some
software does not write anything to the registry or if they do, they don't
follow the normal standard locations. The following script which I built
from examples I found will get you most of your installed software:

<code>

import StringIO
import traceback
import wmi
import win32api
from _winreg import HKEY_LOCAL_MACHINE, KEY_ALL_ACCESS, OpenKey,
EnumValue, QueryValueEx

# logs the software to a text file
# edit to your taste
appFile = open(r'c:\software.log', 'w')

r = wmi.Registry ()
result, names = r.EnumKey (hDefKey=HKEY_LOCAL_MACHINE,
sSubKeyName=r"Software\Microsoft\Windows\CurrentVersion\Uninstall")
index = 0

userName = win32api.GetUserName()
domainName = win32api.GetDomainName()
compName = win32api.GetComputerName()

appFile.write('Computer Name: \t%s\n' % compName)
appFile.write('Domain: \t\t%s\n' % domainName)
appFile.write('User ID: \t\t%s\n\n' % userName)

appFile.write('These subkeys are found under
"HKEY_LOCAL_MACHINE\Software\Microsoft\Windows\CurrentVersion\Uninstall"\n
\n')

for subkey in names:
    try:
 
appFile.write('+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
++++++++++++++\n\n')
        key = OpenKey(HKEY_LOCAL_MACHINE,
 
r"Software\Microsoft\Windows\CurrentVersion\Uninstall\%s" % subkey,0,
KEY_ALL_ACCESS) 
	try:            
            temp = QueryValueEx(key, 'DisplayName')
            display = str(temp[0])
            appFile.write('Display Name: ' + display + '\nRegkey: ' +
subkey + '\n')
        except:            
            appFile.write('Regkey: ' + subkey + '\n')
        
    except:        
        fp = StringIO.StringIO()
        traceback.print_exc(file=fp)
        errorMessage = fp.getvalue()
##        print 'Error for ' + key + '. Message follows:\n' + errorMessage
    index = 0

appFile.close()

</code>

Hope that helps. I have a more complicated one too, but I don't think it
gets much more information than the above.

Mike



More information about the python-win32 mailing list