-
Notifications
You must be signed in to change notification settings - Fork 107
Expand file tree
/
Copy pathhelper.py
More file actions
194 lines (148 loc) · 4.43 KB
/
Copy pathhelper.py
File metadata and controls
194 lines (148 loc) · 4.43 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
import random
import struct
import sys
import time
import traceback
from datetime import datetime
import psutil
import win32api
import win32gui
def get_process_name():
# 获取当前活动窗口句柄
hwnd = win32gui.GetForegroundWindow()
# 获取窗口标题
window_title = win32gui.GetWindowText(hwnd)
return window_title
def get_process_id_by_name(name: str) -> int:
pid = 0
ps = psutil.process_iter()
for p in ps:
if p.name() == name:
pid = p.pid
break
return pid
def find_window(lp_class_name, lp_window_name):
"""
获取指定窗口句柄
"""
h_wnd = win32gui.FindWindow(lp_class_name, lp_window_name)
if h_wnd == 0:
return None
return h_wnd
def get_module_handle(pid: int, name: str) -> int:
"""
获取模块句柄
:param pid: 进程id
:param name: 模块名称
:return:
"""
process = psutil.Process(pid)
# 获取进程的所有模块句柄
modules = process.memory_maps()
# 遍历所有模块句柄并打印
for module in modules:
module_name = module.path.split("\\")
if module_name[3] == name:
return module.rss
start_time = datetime.now() # 记录程序启动时间
def get_app_run_time():
"""返回程序运行时间的格式化字符串"""
current_time = datetime.now() # 获取当前时间
duration = current_time - start_time # 计算时间差
hours, remainder = divmod(duration.total_seconds(), 3600)
minutes, seconds = divmod(remainder, 60)
# 格式化时间字符串
time_string = f"{int(hours):02}:{int(minutes):02}:{int(seconds):02}"
return time_string
def get_now_date():
"""
get_now_date 获取系统当前日期
:return: string
"""
now = datetime.now()
current_time = now.strftime("%Y-%m-%d %H:%M:%S")
return current_time
def int_to_bytes(int_val, int_type):
"""
int转bytes
:param int_val: int
:param int_type:
:return: bytes
"""
if int_type == 2:
return struct.pack('<H', int_val)
if int_type == 4:
return struct.pack('<I', int_val)
if int_type == 8:
return struct.pack('<Q', int_val)
def float_to_bytes(float_val, float_type):
"""
float转bytes
:param float_val: float
:param float_type: int
:return: bytes
"""
if float_val == 4:
return struct.pack('<f', float_type)
if float_val == 8:
return struct.pack('<d', float_type)
def add_bytes(old_bytes: bytes, *new_bytes_arr):
"""
追加bytes
:param old_bytes:
:param new_bytes_arr:
:return: bytes
Example: add_byte(b'\x83\x84', [236, 0, 1, 0, 0], [1, 2, 3, 4]) -> b'\x83\x84\xec\x00\x01\x00\x00\x01\x02\x03\x04'
"""
ret_bytes = add_list(list(old_bytes), *new_bytes_arr)
return bytes(ret_bytes)
def add_list(old_list: list, *new_list_arr: list) -> list:
"""
追加list
:param old_list: list
:param new_list_arr:
:return: bytes
# Example: add_byte([72], [129], [236, 0, 1, 0, 0], [1, 2, 3, 4]) -> [72, 129, 236, 0, 1, 0, 0, 1, 2, 3, 4]
"""
if len(new_list_arr) == 0:
return old_list
for list_arr in new_list_arr:
old_list += list_arr
return old_list
def get_empty_bytes(count: int) -> bytes:
result = list()
for i in range(count):
result.append(0)
return bytes(result)
def message_box(msg):
win32api.MessageBoxEx(0, msg, "Helper")
def ascii_to_unicode(string: str) -> list:
bytes_arr = bytes()
for c in string:
hex_int = ord(c)
bytes_arr = bytes_arr + hex_int.to_bytes(2, byteorder='little')
return list(bytes_arr)
def unicode_to_ascii(ls: list) -> str:
if isinstance(ls, bytes):
ls = list(ls)
text = ""
for i in range(0, len(ls), 2):
if ls[i] == 0 and ls[i + 1] == 0:
break
a = ls[i + 1] << 8
b = ls[i]
text += chr(a + b)
return text
def sleep(timer: int):
time.sleep(timer / 1000.0)
def array_rand(data: list):
index = random.randint(0, len(data) - 1)
return data[index]
def print_trace(title: str, err):
print("-----------{}:出错-----------".format(title))
except_type, _, except_traceback = sys.exc_info()
err_str = ','.join(str(i) for i in err.args)
print(except_type)
print(err_str)
for i in traceback.extract_tb(except_traceback):
print("函数{},文件:{},行:{}".format(i.name, i.filename, i.lineno))