diff --git a/comtypes/server/register.py b/comtypes/server/register.py index c8a4e827..1c4c1153 100644 --- a/comtypes/server/register.py +++ b/comtypes/server/register.py @@ -37,18 +37,19 @@ """ import _ctypes -import ctypes import logging import os import sys import winreg -from ctypes import WinError, windll +from ctypes import WinDLL, WinError +from ctypes.wintypes import HKEY, LONG, LPCWSTR from typing import Iterator, List, Optional, Tuple, Type, Union -import comtypes -import comtypes.server.inprocserver -from comtypes.hresult import * -from comtypes.server import w_getopt +from comtypes import CLSCTX_INPROC_SERVER, CLSCTX_LOCAL_SERVER +from comtypes.hresult import TYPE_E_CANTLOADLIBRARY, TYPE_E_REGISTRYACCESS +from comtypes.server.inprocserver import _clsid_to_class +from comtypes.server.localserver import run as run_localserver +from comtypes.server.w_getopt import w_getopt from comtypes.typeinfo import ( REGKIND_REGISTER, GetModuleFileName, @@ -73,9 +74,12 @@ def _non_zero(retval, func, args): return retval -SHDeleteKey = windll.shlwapi.SHDeleteKeyW +_shlwapi = WinDLL("shlwapi") +LSTATUS = LONG +SHDeleteKey = _shlwapi.SHDeleteKeyW SHDeleteKey.errcheck = _non_zero -SHDeleteKey.argtypes = ctypes.c_ulong, ctypes.c_wchar_p +SHDeleteKey.argtypes = HKEY, LPCWSTR +SHDeleteKey.restype = LSTATUS _KEYS = { @@ -334,8 +338,8 @@ def __iter__(self) -> Iterator[Tuple[int, str, str, str]]: yield (HKCR, f"{reg_novers_progid}\\CLSID", "", reg_clsid) # 3a clsctx: int = getattr(cls, "_reg_clsctx_", 0) - localsvr_ctx = bool(clsctx & comtypes.CLSCTX_LOCAL_SERVER) - inprocsvr_ctx = bool(clsctx & comtypes.CLSCTX_INPROC_SERVER) + localsvr_ctx = bool(clsctx & CLSCTX_LOCAL_SERVER) + inprocsvr_ctx = bool(clsctx & CLSCTX_INPROC_SERVER) if localsvr_ctx and self._frozendllhandle is None: exe = sys.executable @@ -361,10 +365,7 @@ def __iter__(self) -> Iterator[Tuple[int, str, str, str]]: _get_serverdll(self._frozendllhandle), ) # only for non-frozen inproc servers the PythonPath/PythonClass is needed. - if ( - self._frozendllhandle is None - or not comtypes.server.inprocserver._clsid_to_class - ): + if self._frozendllhandle is None or not _clsid_to_class: yield ( HKCR, rf"CLSID\{reg_clsid}\InprocServer32", @@ -405,9 +406,7 @@ def unregister(cls: Type) -> None: def UseCommandLine(*classes: Type) -> int: usage = f"""Usage: {sys.argv[0]} [-regserver] [-unregserver] [-nodebug] [-f logformat] [-l loggername=level]""" - opts, args = w_getopt.w_getopt( - sys.argv[1:], "regserver unregserver embedding l: f: nodebug" - ) + opts, args = w_getopt(sys.argv[1:], "regserver unregserver embedding l: f: nodebug") if not opts: sys.stderr.write(usage + "\n") return 0 # nothing for us to do @@ -440,9 +439,7 @@ def UseCommandLine(*classes: Type) -> int: Registrar().nodebug(cls) if runit: - import comtypes.server.localserver - - comtypes.server.localserver.run(classes) + run_localserver(classes) return 1 # we have done something