Skip to content

Commit

Permalink
[tools\tlbparser.py] Import QueryPathOfRegTypeLib from typeinfo + doc…
Browse files Browse the repository at this point in the history
… adjustments (#763)

* [tools\tlbparser.py] Import QueryPathOfRegTypeLib from typeinfo

With this, we don't need to directly use the ctypes function

* [typeinfo.py] Add documentation for QueryPathOfRegTypeLib

Like this, we clearly understand that QueryPathOfRegTypeLib can sometimes raises a OSError.
  • Loading branch information
moi15moi authored Jan 26, 2025
1 parent b610843 commit 8945446
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 17 deletions.
33 changes: 17 additions & 16 deletions comtypes/tools/tlbparser.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import os
import sys
from ctypes import alignment, byref, c_void_p, sizeof, windll
from ctypes import alignment, c_void_p, sizeof, windll
from typing import Any, Dict, List, Optional, Tuple

from comtypes import BSTR, COMError, automation, typeinfo
Expand Down Expand Up @@ -739,21 +739,22 @@ def get_tlib_filename(tlib: typeinfo.ITypeLib) -> Optional[str]:
# seems if the typelib is not registered, there's no way to
# determine the filename.
la = tlib.GetLibAttr()
name = BSTR()
if 0 == windll.oleaut32.QueryPathOfRegTypeLib(
byref(la.guid), la.wMajorVerNum, la.wMinorVerNum, 0, byref(name)
):
full_filename = name.value.split("\0")[0]
if not os.path.isabs(full_filename):
# workaround Windows 7 bug in QueryPathOfRegTypeLib returning relative path
try:
dll = windll.LoadLibrary(full_filename)
full_filename = _get_module_filename(dll._handle)
del dll
except OSError:
return None
return full_filename
return None
try:
full_filename = typeinfo.QueryPathOfRegTypeLib(
str(la.guid), la.wMajorVerNum, la.wMinorVerNum
)
except OSError:
return None

if not os.path.isabs(full_filename):
# workaround Windows 7 bug in QueryPathOfRegTypeLib returning relative path
try:
dll = windll.LoadLibrary(full_filename)
full_filename = _get_module_filename(dll._handle)
del dll
except OSError:
return None
return full_filename


def _py2exe_hint():
Expand Down
20 changes: 19 additions & 1 deletion comtypes/typeinfo.py
Original file line number Diff line number Diff line change
Expand Up @@ -736,7 +736,25 @@ def CreateTypeLib(filename: str, syskind: int = SYS_WIN32) -> ICreateTypeLib2:
def QueryPathOfRegTypeLib(
libid: str, wVerMajor: int, wVerMinor: int, lcid: int = 0
) -> str:
"""Return the path of a registered type library"""
"""
Retrieve the path of a registered type library.
This function interacts with the Windows registry to locate the path of a
registered type library given its GUID, version, and locale.
Args:
libid (str): The GUID of the type library as a string.
wVerMajor (int): The major version of the type library.
wVerMinor (int): The minor version of the type library.
lcid (int, optional): The locale ID of the type library.
Returns:
str: The path of the registered type library.
Raises:
OSError: If QueryPathOfRegTypeLib fails.
"""

pathname = BSTR()
_QueryPathOfRegTypeLib(
byref(GUID(libid)), wVerMajor, wVerMinor, lcid, byref(pathname)
Expand Down

0 comments on commit 8945446

Please sign in to comment.