Skip to content

Commit

Permalink
patcher - safely retrieve sys32 path
Browse files Browse the repository at this point in the history
  • Loading branch information
th3w1zard1 committed Apr 14, 2024
1 parent 77e5ce6 commit 29efa3c
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 2 deletions.
16 changes: 16 additions & 0 deletions Libraries/Utility/src/utility/system/os_helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,22 @@ def get_mac_dot_app_dir(directory: os.PathLike | str) -> Path:
return Path.pathify(directory).parents[2]


def win_get_system32_dir() -> Path:
import ctypes
try: # PyInstaller sometimes fails to import wintypes.
ctypes.windll.kernel32.GetSystemDirectoryW.argtypes = [ctypes.c_wchar_p, ctypes.c_uint]
ctypes.windll.kernel32.GetSystemDirectoryW.restype = ctypes.c_uint
# Buffer size (MAX_PATH is generally 260 as defined by Windows)
buffer = ctypes.create_unicode_buffer(260)
ctypes.windll.kernel32.GetSystemDirectoryW(buffer, len(buffer))
return Path(buffer.value)
except Exception: # noqa: BLE001
get_root_logger().warning("Error accessing system directory via GetSystemDirectoryW. Attempting fallback.", exc_info=True)
buffer = ctypes.create_unicode_buffer(260)
ctypes.windll.kernel32.GetWindowsDirectoryW(buffer, len(buffer))
return Path(buffer.value).joinpath("system32")


class ChDir:
def __init__(self, path: os.PathLike | str, logger: Logger | None = None):
self.old_dir: Path = Path.cwd()
Expand Down
6 changes: 4 additions & 2 deletions Tools/HoloPatcher/src/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ def update_sys_path(path):
from utility.error_handling import format_exception_with_variables, universal_simplify_exception
from utility.misc import ProcessorArchitecture
from utility.string_util import striprtf
from utility.system.os_helper import win_get_system32_dir
from utility.system.path import Path
from utility.tkinter.tooltip import ToolTip
from utility.tkinter.updater import TkProgressDialog
Expand Down Expand Up @@ -716,10 +717,11 @@ def handle_exit_button(self):
pid = os.getpid()
try:
if sys.platform == "win32":
subprocess.run(["taskkill", "/F", "/PID", str(pid)], check=True)
system32_path = win_get_system32_dir()
subprocess.run([str(system32_path / "taskkill.exe"), "/F", "/PID", str(pid)], check=True) # noqa: S603
else:
subprocess.run(["kill", "-9", str(pid)], check=True)
except Exception as e:
except Exception as e: # noqa: BLE001
self._handle_general_exception(e, "Failed to kill process", msgbox=False)
finally:
# This code might not be reached, but it's here for completeness
Expand Down

0 comments on commit 29efa3c

Please sign in to comment.