Skip to content

Commit d5a9763

Browse files
Merge pull request #209 from forderud/tlb-clsid
Extend GetModule with CLSID support
2 parents 59af98a + 9f1acfb commit d5a9763

File tree

2 files changed

+19
-2
lines changed

2 files changed

+19
-2
lines changed

comtypes/client/_generate.py

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,8 @@ def GetModule(tlib):
3737
"""Create a module wrapping a COM typelibrary on demand.
3838
3939
'tlib' must be an ITypeLib COM pointer instance, the pathname of a
40-
type library, or a tuple/list specifying the arguments to a
41-
comtypes.typeinfo.LoadRegTypeLib call:
40+
type library, a COM CLSID GUID, or a tuple/list specifying the
41+
arguments to a comtypes.typeinfo.LoadRegTypeLib call:
4242
4343
(libid, wMajorVerNum, wMinorVerNum, lcid=0)
4444
@@ -93,6 +93,19 @@ def GetModule(tlib):
9393
logger.debug("GetModule(%s)", tlib)
9494
pathname = tlib
9595
tlib = comtypes.typeinfo.LoadTypeLibEx(tlib)
96+
elif isinstance(tlib, comtypes.GUID):
97+
# tlib contain a clsid
98+
clsid = str(tlib)
99+
100+
# lookup associated typelib in registry
101+
import _winreg
102+
with _winreg.OpenKey(_winreg.HKEY_CLASSES_ROOT, r"CLSID\%s\TypeLib" % clsid, 0, _winreg.KEY_READ) as key:
103+
typelib = _winreg.EnumValue(key, 0)[1]
104+
with _winreg.OpenKey(_winreg.HKEY_CLASSES_ROOT, r"CLSID\%s\Version" % clsid, 0, _winreg.KEY_READ) as key:
105+
version = _winreg.EnumValue(key, 0)[1].split(".")
106+
107+
logger.debug("GetModule(%s)", typelib)
108+
tlib = comtypes.typeinfo.LoadRegTypeLib(comtypes.GUID(typelib), int(version[0]), int(version[1]), 0)
96109
elif isinstance(tlib, (tuple, list)):
97110
# sequence containing libid and version numbers
98111
logger.debug("GetModule(%s)", (tlib,))

comtypes/test/test_client.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,10 @@ def test_clsid_string(self):
2626
comtypes.client.CreateObject(unicode(Scripting.Dictionary._reg_clsid_))
2727
comtypes.client.CreateObject(str(Scripting.Dictionary._reg_clsid_))
2828

29+
def test_GetModule_clsid(self):
30+
clsid = comtypes.GUID.from_progid("MediaPlayer.MediaPlayer")
31+
tlib = comtypes.client.GetModule(clsid)
32+
2933
def test_remote(self):
3034
ie = comtypes.client.CreateObject("InternetExplorer.Application",
3135
machine="localhost")

0 commit comments

Comments
 (0)