-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSource.cpp
277 lines (222 loc) · 6.29 KB
/
Source.cpp
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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
#include <windows.h>
//#include <Windowsx.h>
#include <tlhelp32.h>
LRESULT CALLBACK WinProc(HWND, UINT, WPARAM, LPARAM);
HANDLE OpenProcessByName(LPCTSTR Name, DWORD dwAccess);
DWORD SearchProcessByName(LPCTSTR Name);
ATOM RegisterApplicationWinClass();
//HWND MakeWindow(HWND parent);
HWND MakeMainWindow();
void MessageProcess();
bool IsApplicationActve(LPCTSTR Name);
DWORD WINAPI TerminateApp(DWORD dwPID, DWORD dwTimeout);
HINSTANCE hin;
LPCTSTR windowClassName = "uc-window-class";
LPCTSTR mainTitle = "Main Title";
LPCTSTR otherName = "Second Title";
HWND WinHandler, MainHandler;
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShowCmd)
{
hin = hInstance;
if (RegisterApplicationWinClass() == 0) return -1;
MainHandler = MakeMainWindow();
//WinHandler = MakeWindow(MainHandler);
//ShowWindow(MainHandler, nShowCmd);
//UpdateWindow(MainHandler);
//ShowWindow(WinHandler, nShowCmd);
//UpdateWindow(WinHandler);
MessageProcess();
return 0;
}
void MessageProcess()
{
MSG msg;
while (GetMessage(&msg, 0, 0, 0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
}
ATOM RegisterApplicationWinClass()
{
WNDCLASSEX winapp;
winapp.cbSize = sizeof(WNDCLASSEX);
winapp.hInstance = hin;
winapp.lpszClassName = windowClassName;
winapp.lpfnWndProc = WinProc;
winapp.style = CS_OWNDC | CS_HREDRAW | CS_VREDRAW;
winapp.hIcon = LoadIcon(NULL, IDI_APPLICATION);
winapp.hIconSm = winapp.hIcon;
winapp.hCursor = LoadCursor(NULL, IDC_ARROW);
winapp.lpszMenuName = NULL;
winapp.cbClsExtra = 0;
winapp.cbWndExtra = 0;
winapp.hbrBackground = (HBRUSH)COLOR_WINDOW;
return RegisterClassEx(&winapp);
}
LRESULT CALLBACK WinProc(HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam)
{
switch (msg)
{
case WM_ENDSESSION:
SetWindowText(hwnd, "WM_ENDSESSION");
break;
case WM_QUERYENDSESSION:
SetWindowText(hwnd, "Check for uTorrent");
// wait for utorrent close!!!
/*MessageBox(
hwnd, IsApplicationActve("uTorrent.exe") ? "Correct Handle" : "Invalid Handle", "Process Check", 0
);*/
ShowWindow(MainHandler, SW_SHOWNORMAL);
UpdateWindow(MainHandler);
if (IsApplicationActve("uTorrent.exe")) {
DWORD pID = SearchProcessByName("uTorrent.exe");
TerminateApp(pID, 0);
SetWindowText(hwnd, "Waiting for uTorrent...");
Sleep(1000);
}
while (IsApplicationActve("uTorrent.exe")) {
Sleep(1000);
}
SetWindowText(hwnd, "Waiting for uTorrent - Success!");
/*MessageBox(
hwnd, "Exit Complete!", "Process Check", 0
);*/
//Sleep(5000);
return 1;
break;
case WM_DESTROY:
PostQuitMessage(0);
break;
case WM_CREATE:
return 1;
break;
case WM_LBUTTONUP:
//GET_X_LPARAM(lparam);
//SetWindowText(hwnd, windowClassName);
//SetWindowText(MainHandler, otherName);
MessageBox(
hwnd, IsApplicationActve("uTorrent.exe") ? "Correct Handle" : "Invalid Handle", "Process Check", 0
);
if (IsApplicationActve("uTorrent.exe")) {
DWORD pID = SearchProcessByName("uTorrent.exe");
TerminateApp(pID, 0);
Sleep(1000);
}
break;
case WM_LBUTTONDOWN:
//SetWindowText(hwnd, otherName);
//SetWindowText(MainHandler, windowClassName);
break;
default:
return DefWindowProc(hwnd, msg, wparam, lparam);
}
return 0;
}
/*HWND MakeWindow(HWND parent)
{
return CreateWindowEx(0, windowClassName, mainTitle,
WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
parent, 0, hin, 0);
}*/
HWND MakeMainWindow()
{
//HWND hHiddenWnd = CreateWindowEx(0, windowClassName, "", WS_POPUP, 0, 0, 0, 0, 0, 0, hin, 0);
return CreateWindowEx(WS_EX_TOOLWINDOW, windowClassName, otherName,
WS_CLIPSIBLINGS | WS_OVERLAPPEDWINDOW,
/*GetSystemMetrics(SM_CXSCREEN) / 2,
GetSystemMetrics(SM_CYSCREEN) / 2,
0, 0, */
//CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
-100, -100, 20, 20,
0, 0, hin, 0);
}
DWORD SearchProcessByName(LPCTSTR Name)
{
DWORD dwAccess = PROCESS_VM_READ;
HANDLE hSnap = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
if (hSnap != INVALID_HANDLE_VALUE)
{
PROCESSENTRY32 pe;
ZeroMemory(&pe, sizeof(PROCESSENTRY32));
pe.dwSize = sizeof(PROCESSENTRY32);
Process32First(hSnap, &pe);
do
{
if (!lstrcmpi(pe.szExeFile, Name))
{
return pe.th32ProcessID;
}
} while (Process32Next(hSnap, &pe));
}
return 0;
}
HANDLE OpenProcessByName(LPCTSTR Name, DWORD dwAccess)
{
HANDLE hSnap = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
if (hSnap != INVALID_HANDLE_VALUE)
{
PROCESSENTRY32 pe;
ZeroMemory(&pe, sizeof(PROCESSENTRY32));
pe.dwSize = sizeof(PROCESSENTRY32);
Process32First(hSnap, &pe);
do
{
if (!lstrcmpi(pe.szExeFile, Name))
{
return OpenProcess(dwAccess, 0, pe.th32ProcessID);
}
} while (Process32Next(hSnap, &pe));
}
return INVALID_HANDLE_VALUE;
}
bool IsApplicationActve(LPCTSTR Name)
{
HANDLE hFire = OpenProcessByName(Name, PROCESS_VM_READ);
if (hFire != INVALID_HANDLE_VALUE) {
CloseHandle(hFire);
return true;
}
else
return false;
}
#define TA_FAILED 0
#define TA_SUCCESS_CLEAN 1
#define TA_SUCCESS_KILL 2
#define TA_SUCCESS_16 3
BOOL CALLBACK TerminateAppEnum(HWND hwnd, LPARAM lParam)
{
DWORD dwID;
GetWindowThreadProcessId(hwnd, &dwID);
if (dwID == (DWORD)lParam)
{
PostMessage(hwnd, WM_CLOSE, 0, 0);
}
return TRUE;
}
DWORD WINAPI TerminateApp(DWORD dwPID, DWORD dwTimeout)
{
HANDLE hProc;
DWORD dwRet = 0;
// If we can't open the process with PROCESS_TERMINATE rights,
// then we give up immediately.
hProc = OpenProcess(SYNCHRONIZE | PROCESS_TERMINATE, FALSE,
dwPID);
if (hProc == NULL)
{
return TA_FAILED;
}
// TerminateAppEnum() posts WM_CLOSE to all windows whose PID
// matches your process's.
EnumWindows((WNDENUMPROC)TerminateAppEnum, (LPARAM)dwPID);
if (dwTimeout > 0) {
// Wait on the handle. If it signals, great. If it times out,
// then you kill it.
if (WaitForSingleObject(hProc, dwTimeout) != WAIT_OBJECT_0)
dwRet = (TerminateProcess(hProc, 0) ? TA_SUCCESS_KILL : TA_FAILED);
else
dwRet = TA_SUCCESS_CLEAN;
}
CloseHandle(hProc);
return dwRet;
}