Skip to content

Commit

Permalink
[server\register.py] Use WinDLL instead of windll + import improvement (
Browse files Browse the repository at this point in the history
#737)

* [server\register.py] Use WinDLL instead of windll

Partially fix #735

* [server\register.py] Replace * import with specific imports

- Prefer `from import` over `import`
- Use LPCWSTR instead of c_wchar_p.
- Don't directly import `comtypes.server.localserver` in a function.
  • Loading branch information
moi15moi authored Jan 13, 2025
1 parent 41e2eac commit 9853ac4
Showing 1 changed file with 17 additions and 20 deletions.
37 changes: 17 additions & 20 deletions comtypes/server/register.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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 = {
Expand Down Expand Up @@ -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
Expand All @@ -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",
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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

Expand Down

0 comments on commit 9853ac4

Please sign in to comment.