-
Notifications
You must be signed in to change notification settings - Fork 0
/
winapi.py
134 lines (118 loc) · 4.08 KB
/
winapi.py
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
import platform
import ctypes
from ctypes import wintypes
class PROCESSENTRY32W(ctypes.Structure):
_fields_ = [
('dwSize', wintypes.DWORD),
('cntUsage', wintypes.DWORD),
('th32ProcessID', wintypes.DWORD),
('th32DefaultHeapID', wintypes.PULONG),
('th32ModuleID', wintypes.DWORD),
('cntThreads', wintypes.DWORD),
('th32ParentProcessID', wintypes.DWORD),
('pcPriClassBase', wintypes.LONG),
('dwFlags', wintypes.DWORD),
('szExeFile', wintypes.WCHAR * 260) # Max Path
]
try:
kernel32 = ctypes.windll.kernel32
except (FileNotFoundError, AttributeError):
kernel32 = None
try:
ps_api = ctypes.windll.psapi
except (FileNotFoundError, AttributeError):
ps_api = None
try:
GetLastError = kernel32.GetLastError
GetLastError.argtypes = ()
GetLastError.restype = wintypes.DWORD
except (FileNotFoundError, AttributeError):
GetLastError = None
try:
CloseHandle = kernel32.CloseHandle
CloseHandle.argtypes = (wintypes.HANDLE, )
CloseHandle.restype = wintypes.BOOL
except (FileNotFoundError, AttributeError):
CloseHandle = None
try:
OpenProcess = kernel32.OpenProcess
OpenProcess.argtypes = (wintypes.DWORD, wintypes.BOOL, wintypes.DWORD)
OpenProcess.restype = wintypes.HANDLE
except (FileNotFoundError, AttributeError):
OpenProcess = None
try:
Process32FirstW = kernel32.Process32FirstW
Process32FirstW.argtypes = (wintypes.HANDLE, ctypes.POINTER(PROCESSENTRY32W))
Process32FirstW.restype = wintypes.BOOL
except (FileNotFoundError, AttributeError):
Process32FirstW = None
try:
Process32NextW = kernel32.Process32NextW
Process32NextW.argtypes = (wintypes.HANDLE, ctypes.POINTER(PROCESSENTRY32W))
Process32NextW.restype = wintypes.BOOL
except (FileNotFoundError, AttributeError):
Process32NextW = None
try:
CreateToolhelp32Snapshot = kernel32.CreateToolhelp32Snapshot
CreateToolhelp32Snapshot.argtypes = (wintypes.DWORD, wintypes.DWORD)
CreateToolhelp32Snapshot.restype = wintypes.HANDLE
except (FileNotFoundError, AttributeError):
CreateToolhelp32Snapshot = None
try:
GetModuleFileNameExW = ps_api.GetModuleFileNameExW
except (FileNotFoundError, AttributeError):
try:
GetModuleFileNameExW = kernel32.GetModuleFileNameExW
except (FileNotFoundError, AttributeError):
GetModuleFileNameExW = None
try:
GetModuleFileNameExW.argtypes = (wintypes.HANDLE, wintypes.HMODULE, wintypes.LPCWSTR, wintypes.DWORD)
GetModuleFileNameExW.restype = wintypes.DWORD
except (FileNotFoundError, AttributeError):
pass
try:
user32 = ctypes.windll.user32
MessageBoxW = user32.MessageBoxW
MessageBoxW.argtypes = (wintypes.HWND, wintypes.LPCWSTR, wintypes.LPCWSTR, wintypes.UINT)
MessageBoxW.restype = ctypes.c_int
except (FileNotFoundError, AttributeError):
user32 = None
MessageBoxW = None
try:
shell32 = ctypes.windll.shell32
ShellExecuteW = shell32.ShellExecuteW
ShellExecuteW.argtypes = (
wintypes.HWND,
wintypes.LPCWSTR,
wintypes.LPCWSTR,
wintypes.LPCWSTR,
wintypes.LPCWSTR,
ctypes.c_int
)
ShellExecuteW.restype = wintypes.HINSTANCE
except (FileNotFoundError, AttributeError):
shell32 = None
ShellExecuteW = None
try:
if int(platform.version().split('.')[-1]) < 17763: # noqa fuck Micro$oft fuck Micro$oft fuck Micro$oft fuck
raise FileNotFoundError('FUCK MICROSOFT DUDE WINDOWS IS PEACE OF SHIT')
ux_theme = ctypes.windll.uxtheme
ShouldUseDarkMode = ux_theme.__getitem__(132)
ShouldUseDarkMode.argtypes = ()
ShouldUseDarkMode.restype = ctypes.c_byte
except (FileNotFoundError, AttributeError):
ux_theme = None
ShouldUseDarkMode = None
try:
dwm_api = ctypes.windll.dwmapi
DwmSetWindowAttribute = dwm_api.DwmSetWindowAttribute
DwmSetWindowAttribute.argtypes = (
wintypes.HWND,
wintypes.DWORD,
wintypes.LPCVOID,
wintypes.DWORD
)
DwmSetWindowAttribute.restype = wintypes.LONG
except (FileNotFoundError, AttributeError):
dwm_api = None
DwmSetWindowAttribute = None