Suraj Barkale schrieb:
[about finding typelibs for COM objects starting from the progid]

COM IDEs usually include a COM typelib browser which lists the type libraries
installed on the system and offering to "import" them into a project.
For example, PythonWin has such a utility which creates the win32com wrapper
for the selected typelib.  AFAIU these utilities scan the registry to find
registered typelibs.  I have attached some code that takes a somewhat different
approach (but it could possibly be a starting point to write such a browser).
The script tries to find the typelibrary starting with a progid.  For some
progids this approach works, for others it doesn't (Word, for example).

FWIW
Thomas

"""
from comtypes import GUID
from comtypes.client import GetModule
import _winreg

##progid = "MSScriptControl.ScriptControl"
##progid = "Word.Application"
progid = "InternetExplorer.Application"

def find_typelib_guid(progid):
    """Read the registry to find a typelib associated with 'progid'."""
    guid = GUID.from_progid(progid)
    key = _winreg.OpenKey(_winreg.HKEY_CLASSES_ROOT,
                          "CLSID\\%s" % guid,
                          _winreg.KEY_READ)
    return _winreg.QueryValue(key, "Typelib")

def enum_subkeys(root, key):
    """Generator that yields the names of all subkeys"""
    key = _winreg.OpenKey(root, key)
    index = 0
    while 1:
        try:
            yield _winreg.EnumKey(key, index)
        except WindowsError, details:
            if details[0] == 259:
                raise StopIteration
            raise
        index += 1

def get_typelib(libid):
    for version in enum_subkeys(_winreg.HKEY_CLASSES_ROOT,
                                "Typelib\\%s" % libid):
        # Full name of the type library
        libname = _winreg.QueryValue(_winreg.HKEY_CLASSES_ROOT,
                                     "Typelib\\%s\\%s" % (libid, version))
        # Pathname of the type library
        pathname = _winreg.QueryValue(_winreg.HKEY_CLASSES_ROOT,
                                     "Typelib\\%s\\%s\\0\\win32" % (libid, 
version))
        # version number
        major, minor = map(int, version.split("."))
        print ((major, minor), pathname, libname)
        print GetModule(pathname)
        print GetModule((libid, major, minor, 0))
    return

libid = find_typelib_guid(progid)

get_typelib(libid)
"""


-------------------------------------------------------------------------
This SF.net email is sponsored by: Splunk Inc.
Still grepping through log files to find problems?  Stop.
Now Search log events and configuration files using AJAX and a browser.
Download your FREE copy of Splunk now >> http://get.splunk.com/
_______________________________________________
comtypes-users mailing list
comtypes-users@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/comtypes-users

Reply via email to