Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[server\register.py] Use WinDLL instead of windll + import improvement #737

Merged
merged 2 commits into from
Jan 13, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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