xtn-tools-pro 1.0.1.3.2__py3-none-any.whl → 1.0.1.3.4__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,99 @@
1
+ #!/usr/bin/env python
2
+ # -*- coding: utf-8 -*-
3
+
4
+ # 说明:
5
+ # win 操作
6
+ # History:
7
+ # Date Author Version Modification
8
+ # --------------------------------------------------------------------------------------------------
9
+ # 2025/8/11 xiatn V00.01.000 新建
10
+ # --------------------------------------------------------------------------------------------------
11
+ import psutil
12
+ import ctypes
13
+ from ctypes import wintypes
14
+
15
+ user32 = ctypes.WinDLL("user32", use_last_error=True)
16
+
17
+ # 回调类型:BOOL CALLBACK EnumWindowsProc(HWND hwnd, LPARAM lParam)
18
+ EnumWindowsProc = ctypes.WINFUNCTYPE(wintypes.BOOL, wintypes.HWND, wintypes.LPARAM)
19
+
20
+ # --- 明确声明所有用到的 WinAPI 的参数/返回类型 ---
21
+ user32.EnumWindows.argtypes = [EnumWindowsProc, wintypes.LPARAM]
22
+ user32.EnumWindows.restype = wintypes.BOOL
23
+
24
+ user32.GetWindowThreadProcessId.argtypes = [wintypes.HWND, ctypes.POINTER(wintypes.DWORD)]
25
+ user32.GetWindowThreadProcessId.restype = wintypes.DWORD
26
+
27
+ user32.IsWindowVisible.argtypes = [wintypes.HWND]
28
+ user32.IsWindowVisible.restype = wintypes.BOOL
29
+
30
+ user32.GetWindowTextLengthW.argtypes = [wintypes.HWND]
31
+ user32.GetWindowTextLengthW.restype = ctypes.c_int # 长度(TCHAR 数),可能为 0
32
+
33
+ user32.GetWindowTextW.argtypes = [wintypes.HWND, wintypes.LPWSTR, ctypes.c_int]
34
+ user32.GetWindowTextW.restype = ctypes.c_int # 实际拷贝的字符数
35
+
36
+
37
+ def enum_windows(only_visible=True):
38
+ """
39
+ 遍历顶层窗口,返回 [{hwnd, pid, title}, ...]
40
+ - only_visible=True 只返回可见窗口
41
+ """
42
+ results = []
43
+
44
+ @EnumWindowsProc
45
+ def callback(hwnd, lparam):
46
+ try:
47
+ # 可选过滤
48
+ if only_visible and not user32.IsWindowVisible(hwnd):
49
+ return True # 继续枚举
50
+
51
+ # 取 PID
52
+ pid = wintypes.DWORD(0)
53
+ user32.GetWindowThreadProcessId(hwnd, ctypes.byref(pid))
54
+
55
+ # 取标题
56
+ length = user32.GetWindowTextLengthW(hwnd)
57
+ title = ""
58
+ if length > 0:
59
+ buf = ctypes.create_unicode_buffer(length + 1)
60
+ copied = user32.GetWindowTextW(hwnd, buf, length + 1)
61
+ if copied > 0:
62
+ title = buf.value
63
+
64
+ # 结果里再把 HWND 规范化为无符号整数,避免负数显示
65
+ results.append({
66
+ "hwnd": ctypes.c_size_t(hwnd).value,
67
+ "pid": pid.value,
68
+ "title": title
69
+ })
70
+ except Exception:
71
+ # 回调里不要把异常抛给 WinAPI,否则会出现 “Exception ignored on calling ctypes callback function”
72
+ pass
73
+ return True # 继续
74
+
75
+ user32.EnumWindows(callback, 0)
76
+ return results
77
+
78
+
79
+ def kill_process_by_name(process_name, x=False):
80
+ """
81
+ 遍历根据进程名杀死所有相关进程
82
+ :param process_name:
83
+ :return:
84
+ """
85
+ for proc in psutil.process_iter(['pid', 'name']):
86
+ try:
87
+ if proc.info['name'] and process_name.lower() in proc.info['name'].lower():
88
+ if x:
89
+ print(f"Killing process: {proc.info['name']} (PID: {proc.info['pid']})")
90
+ proc.kill()
91
+ except (psutil.NoSuchProcess, psutil.AccessDenied):
92
+ continue
93
+
94
+
95
+ if __name__ == "__main__":
96
+ pass
97
+ # 枚举win窗口获取句柄和pid
98
+ # for win in enum_windows(only_visible=True):
99
+ # print(win)
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: xtn-tools-pro
3
- Version: 1.0.1.3.2
3
+ Version: 1.0.1.3.4
4
4
  Summary: xtn 开发工具
5
5
  Author: xtn
6
6
  Author-email: czw011122@gmail.com
@@ -20,5 +20,6 @@ Requires-Dist: tqdm
20
20
  Requires-Dist: fabric
21
21
  Requires-Dist: pyotp
22
22
  Requires-Dist: pycryptodome
23
+ Requires-Dist: psutil
23
24
 
24
25
  xtnkk-tools
@@ -64,9 +64,10 @@ xtn_tools_pro/utils/set_data.py,sha256=UR5S8xBqc4Vk8zpbrNVsxKhwaBAenaEdC7O-N3s7h
64
64
  xtn_tools_pro/utils/shell.py,sha256=s6sJedxLLQokcI1hF5YSD3qgGUPK0brNcP-D3-yv54I,3790
65
65
  xtn_tools_pro/utils/sql.py,sha256=EAKzbkZP7Q09j15Gm6o0_uq0qgQmcCQT6EAawbpp4v0,6263
66
66
  xtn_tools_pro/utils/time_utils.py,sha256=TUtzG61PeVYXhaQd6pBrXAdlz7tBispNIRQRcGhE2No,4859
67
- xtn_tools_pro-1.0.1.3.2.dist-info/LICENSE,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
68
- xtn_tools_pro-1.0.1.3.2.dist-info/METADATA,sha256=E9GDeG_G6ggOAy8PK0XyZn0qkf2mATpdaXJnR46C38I,574
69
- xtn_tools_pro-1.0.1.3.2.dist-info/WHEEL,sha256=oiQVh_5PnQM0E3gPdiz09WCNmwiHDMaGer_elqB3coM,92
70
- xtn_tools_pro-1.0.1.3.2.dist-info/entry_points.txt,sha256=t8CtXWOgw7nRDW3XNlZh8MT_P4l8EzsFnyWi5b3ovrc,68
71
- xtn_tools_pro-1.0.1.3.2.dist-info/top_level.txt,sha256=jyB3FLDEr8zE1U7wHczTgIbvUpALhR-ULF7RVEO7O2U,14
72
- xtn_tools_pro-1.0.1.3.2.dist-info/RECORD,,
67
+ xtn_tools_pro/utils/win.py,sha256=D5_58WMKOGe9ZwJuJr2JGAWmVISwMOZUx4t7wXUCviU,3512
68
+ xtn_tools_pro-1.0.1.3.4.dist-info/LICENSE,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
69
+ xtn_tools_pro-1.0.1.3.4.dist-info/METADATA,sha256=KF4Y3olOdoFIPNCusHUXBAhqHu0M-aUE5YOoWfR5PwU,597
70
+ xtn_tools_pro-1.0.1.3.4.dist-info/WHEEL,sha256=oiQVh_5PnQM0E3gPdiz09WCNmwiHDMaGer_elqB3coM,92
71
+ xtn_tools_pro-1.0.1.3.4.dist-info/entry_points.txt,sha256=t8CtXWOgw7nRDW3XNlZh8MT_P4l8EzsFnyWi5b3ovrc,68
72
+ xtn_tools_pro-1.0.1.3.4.dist-info/top_level.txt,sha256=jyB3FLDEr8zE1U7wHczTgIbvUpALhR-ULF7RVEO7O2U,14
73
+ xtn_tools_pro-1.0.1.3.4.dist-info/RECORD,,