-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
559 lines (455 loc) · 15.1 KB
/
main.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
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
/************************************************************************************/
/* DISPLAY LOCKER */
/************************************************************************************/
#include <windows.h>
#include "resource.h"
#include <iostream>
#include <fstream>
#pragma comment(lib,"PowrProf.lib")
char* strPassword;
STICKYKEYS g_StartupStickyKeys = {sizeof(STICKYKEYS), 0};
TOGGLEKEYS g_StartupToggleKeys = {sizeof(TOGGLEKEYS), 0};
FILTERKEYS g_StartupFilterKeys = {sizeof(FILTERKEYS), 0};
BOOL CALLBACK PasswordDlgProc(HWND, UINT, WPARAM, LPARAM);
BOOL CALLBACK LockScreenDlgProc(HWND, UINT, WPARAM, LPARAM);
LRESULT CALLBACK LowLevelKeyboardProc(int, WPARAM, LPARAM);
LRESULT CALLBACK LowLevelMouseProc(int, WPARAM, LPARAM);
void tm(BOOL);
void kb(BOOL);
void ms(BOOL);
//void tb(BOOL);
void AllowAccessibilityShortcutKeys(bool);
DWORD WINAPI BackgroundThread( LPVOID lpParam );
HANDLE backgroundThreadHandle;
DWORD backgroundThreadId;
HHOOK kbHook;
HHOOK msHook;
HWND lockScreenHwnd;
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
LPSTR lpCmdLine, int nCmdShow)
{
MSG Msg;
int ret;
std::ifstream pFile("dt", std::ifstream::in);
if (pFile) {
// get length of file:
pFile.seekg (0, pFile.end);
int length = pFile.tellg();
pFile.seekg (0, pFile.beg);
strPassword = (char*)GlobalAlloc(GPTR, length + 1);
// read data as a block:
pFile.read (strPassword,length);
pFile.close();
ret=IDOK;
} else {
ret = DialogBox(GetModuleHandle(NULL),
MAKEINTRESOURCE(IDD_PASSWORD), NULL, PasswordDlgProc);
}
if(ret == IDOK){
//Create the lock screen
// Save the current sticky/toggle/filter key settings so they can be restored them later
SystemParametersInfo(SPI_GETSTICKYKEYS, sizeof(STICKYKEYS), &g_StartupStickyKeys, 0);
SystemParametersInfo(SPI_GETTOGGLEKEYS, sizeof(TOGGLEKEYS), &g_StartupToggleKeys, 0);
SystemParametersInfo(SPI_GETFILTERKEYS, sizeof(FILTERKEYS), &g_StartupFilterKeys, 0);
kb(FALSE);
ms(FALSE);
tm(FALSE);
//tb(FALSE);
AllowAccessibilityShortcutKeys(false);
DialogBox(GetModuleHandle(NULL),
MAKEINTRESOURCE(IDD_LOCKSCREEN), NULL, LockScreenDlgProc);
kb(TRUE);
ms(TRUE);
tm(TRUE);
//tb(TRUE);
AllowAccessibilityShortcutKeys(true);
PostQuitMessage(0);
}
else if(ret == IDCANCEL){
PostQuitMessage(0);
}
else if(ret == -1){
MessageBox(NULL, "Failed to load!", "Error",
MB_OK | MB_ICONINFORMATION);
PostQuitMessage(0);
}
// Step 3: The Message Loop
while(GetMessage(&Msg, NULL, 0, 0) > 0)
{
TranslateMessage(&Msg);
DispatchMessage(&Msg);
}
return Msg.wParam;
}
//The password Window Dialog Procedure
BOOL CALLBACK PasswordDlgProc(HWND hwnd, UINT Message, WPARAM wParam, LPARAM lParam)
{
RECT rc,rcDlg,rcOwner;
switch(Message)
{
case WM_INITDIALOG:
GetWindowRect(GetDesktopWindow(), &rcOwner);
GetWindowRect(hwnd, &rcDlg);
CopyRect(&rc, &rcOwner);
// Offset the owner and dialog box rectangles so that right and bottom
// values represent the width and height, and then offset the owner again
// to discard space taken up by the dialog box.
OffsetRect(&rcDlg, -rcDlg.left, -rcDlg.top);
OffsetRect(&rc, -rc.left, -rc.top);
OffsetRect(&rc, -rcDlg.right, -rcDlg.bottom);
// The new position is the sum of half the remaining space and the owner's
// original position.
SetWindowPos(hwnd,
HWND_TOP,
rcOwner.left + (rc.right / 2),
rcOwner.top + (rc.bottom / 2),
0, 0, // Ignores size arguments.
SWP_NOSIZE);
if (GetDlgCtrlID((HWND) wParam) != IDC_PASSWORD)
{
SetFocus(GetDlgItem(hwnd, IDC_PASSWORD));
return FALSE;
}
return TRUE;
case WM_COMMAND:
switch(LOWORD(wParam))
{
// OK Button Pressed
case IDOK:
/*
state = 0 -> password length not correct
state = 1 -> password do not match
state = 2 -> passwords match
*/
int state,len1,len2;
//Get the length of 2 entered passwords
len1 = GetWindowTextLength(GetDlgItem(hwnd, IDC_PASSWORD));
len2 = GetWindowTextLength(GetDlgItem(hwnd, IDC_CONFIRM));
if(len1 > 0 && len2 > 0 && len1<=25 && len2<=25) //check whether length is correct
{
char* password1;
char* password2;
//allocate memory for 2 passwords
password1 = (char*)GlobalAlloc(GPTR, len1 + 1);
password2 = (char*)GlobalAlloc(GPTR, len2 + 1);
//Get the entered passwords
GetDlgItemText(hwnd, IDC_PASSWORD, password1, len1 + 1);
GetDlgItemText(hwnd, IDC_CONFIRM, password2, len2 + 1);
//compare the 2 passwords
if(strcmp(password1, password2) != 0)
state = 1;
else
{
//Passwords are correct and matching. So copy the password to a global variable for accessing later
state = 2;
strPassword = (char*)GlobalAlloc(GPTR, len1 + 1);
strcpy(strPassword,password1);
}
//Free the 2 password variables
GlobalFree((HANDLE)password1);
GlobalFree((HANDLE)password2);
} else {
state = 0; //password length incorrect
}
if(state== 0)
MessageBox(NULL, "Password should have 1 to 25 characters", "Error!",
MB_ICONEXCLAMATION | MB_OK);
else if(state==1)
MessageBox(NULL, "Passwords do not match!", "Error!",
MB_ICONEXCLAMATION | MB_OK);
else
EndDialog(hwnd, IDOK);
break;
case IDCANCEL:
EndDialog(hwnd, IDCANCEL);
break;
}
break;
default:
return FALSE;
}
return TRUE;
}
//The password Window Dialog Procedure
BOOL CALLBACK LockScreenDlgProc(HWND hwnd, UINT Message, WPARAM wParam, LPARAM lParam)
{
switch(Message)
{
/*case WM_CTLCOLORDLG:
//return (INT_PTR)GetStockObject(HOLLOW_BRUSH);
return (INT_PTR)GetStockObject(WHITE_BRUSH);
break;*/
/*case WM_CTLCOLORSTATIC:
return (INT_PTR)GetStockObject(WHITE_BRUSH);
break;*/
case WM_ACTIVATE:
SetCursorPos(0,0);
if (GetDlgCtrlID((HWND) wParam) != IDC_UNLOCK)
SetFocus(GetDlgItem(hwnd, IDC_UNLOCK));
SetWindowLong (hwnd, GWL_EXSTYLE, GetWindowLong (hwnd, GWL_EXSTYLE ) | WS_EX_LAYERED);
SetLayeredWindowAttributes(hwnd,RGB(0, 0, 0),200,LWA_ALPHA);
break;
case WM_INITDIALOG:
//important to set this.. backgroundThread uses this handle to do tasks
lockScreenHwnd = hwnd;
//start the background thread
backgroundThreadHandle = CreateThread(
NULL, // default security attributes
0, // use default stack size
BackgroundThread, // thread function name
NULL, // argument to thread function
0, // use default creation flags
&backgroundThreadId);
RECT rcText, rcBackground;
GetWindowRect(GetDlgItem(hwnd, IDC_UNLOCK), &rcText);
GetWindowRect(GetDlgItem(hwnd, IDC_BACKGROUND), &rcBackground);
// Offset the rectangles so that right and bottom
// values represent the width and height, and then offset the owner again
// to discard space taken up by the dialog box.
OffsetRect(&rcText, -rcText.left, -rcText.top);
OffsetRect(&rcBackground, -rcBackground.left, -rcBackground.top);
int cxScreen, cyScreen;
cxScreen = GetSystemMetrics(SM_CXSCREEN);
cyScreen = GetSystemMetrics(SM_CYSCREEN);
SetWindowPos(hwnd,
HWND_TOPMOST,
0,
0,
cxScreen, cyScreen,
SWP_NOMOVE | SWP_SHOWWINDOW);
SetWindowPos(
GetDlgItem(hwnd, IDC_BACKGROUND),
NULL,
(cxScreen/2) - 300,
(cyScreen/2) - 200,
600,
338,
0
);
SetWindowPos(
GetDlgItem(hwnd, IDC_UNLOCK),
NULL,
(cxScreen/2) - 57,
(cyScreen/2) - 33,
rcText.right,
14,
SWP_SHOWWINDOW
);
if (GetDlgCtrlID((HWND) wParam) != IDC_UNLOCK)
{
SetFocus(GetDlgItem(hwnd, IDC_UNLOCK));
return true;
}
return TRUE;
case WM_COMMAND:
if(LOWORD(wParam) == IDC_UNLOCK && HIWORD(wParam) == EN_CHANGE)
{
int len = GetWindowTextLength(GetDlgItem(hwnd, IDC_UNLOCK));
char* enteredPassword = (char*)GlobalAlloc(GPTR, len + 1);
//Get the entered passwords
GetDlgItemText(hwnd, IDC_UNLOCK, enteredPassword, len + 1);
if(strcmp(enteredPassword, strPassword) == 0) {
GlobalFree((HANDLE)strPassword);
EndDialog(hwnd, NULL);
}
}
return true;
break;
case WM_QUERYENDSESSION:
AbortSystemShutdown(NULL);
//SetSuspendState(FALSE,FALSE,FALSE);
break;
case WM_DESTROY:
TerminateThread(backgroundThreadHandle,0);
break;
//case WM_WINDOWPOSCHANGED:
//DefWindowProc(hwnd, Message, wParam, lParam);
//windowPosPtr = (WINDOWPOS*) lParam;
//char l[100];
//GetWindowText(windowPosPtr->hwnd, l, 99);
//OutputDebugStringA(l);
//if(GetNextWindow(hwnd,GW_HWNDNEXT) != NULL && loaded==1)
//SetWindowPos(windowPosPtr->hwndInsertAfter,hwnd,0,0,0,0,0);
//SetWindowPos(hwnd,
// HWND_TOP,
// 0,
// 0,
// 0, 0, // Ignores size arguments.
// 0);
//return 0;
default:
return FALSE;
}
return TRUE;
}
void tm(BOOL enable)
{
HKEY regHandle;
DWORD dwValue;
dwValue = (enable==TRUE)? 0 : 1;
BYTE* data = (BYTE*)&dwValue;
RegCreateKeyEx(HKEY_CURRENT_USER,"Software\\Microsoft\\Windows\\CurrentVersion\\Policies\\System", 0, NULL, NULL, KEY_WRITE | KEY_WOW64_32KEY,NULL , ®Handle ,NULL );
RegSetValueEx(regHandle,"DisableTaskmgr",0, REG_DWORD,data ,sizeof(DWORD));
}
/*
void tb(BOOL enable)
{
HWND hwnd1 = FindWindow("Shell_traywnd", "");
if(enable)
SetWindowPos(hwnd1, 0, 0, 0, 0, 0, SWP_SHOWWINDOW);
else
SetWindowPos(hwnd1, 0, 0, 0, 0, 0, SWP_HIDEWINDOW);
}*/
void kb(BOOL enable)
{
if(enable==FALSE)
kbHook = SetWindowsHookEx(WH_KEYBOARD_LL,LowLevelKeyboardProc,GetModuleHandle("user32.dll"),NULL);
else
{
UnhookWindowsHookEx(kbHook);
/* Ctrl and alt are pressed down when user presses ctrl+ alt+ del. Release them
TODO: Check if all the 6 are necessary
*/
INPUT input;
input.type = INPUT_KEYBOARD;
input.ki.wVk = VK_LCONTROL;
input.ki.dwFlags = KEYEVENTF_KEYUP;
SendInput(1, &input, sizeof(INPUT));
input.type = INPUT_KEYBOARD;
input.ki.wVk = VK_RCONTROL;
input.ki.dwFlags = KEYEVENTF_KEYUP;
SendInput(1, &input, sizeof(INPUT));
input.type = INPUT_KEYBOARD;
input.ki.wVk = VK_LMENU;
input.ki.dwFlags = KEYEVENTF_KEYUP;
SendInput(1, &input, sizeof(INPUT));
input.type = INPUT_KEYBOARD;
input.ki.wVk = VK_RMENU;
input.ki.dwFlags = KEYEVENTF_KEYUP;
SendInput(1, &input, sizeof(INPUT));
input.type = INPUT_KEYBOARD;
input.ki.wVk = VK_CONTROL;
input.ki.dwFlags = KEYEVENTF_KEYUP;
SendInput(1, &input, sizeof(INPUT));
input.type = INPUT_KEYBOARD;
input.ki.wVk = VK_MENU;
input.ki.dwFlags = KEYEVENTF_KEYUP;
SendInput(1, &input, sizeof(INPUT));
}
}
void ms(BOOL enable)
{
if(enable==FALSE)
msHook = SetWindowsHookEx(WH_MOUSE_LL,LowLevelMouseProc,GetModuleHandle("user32.dll"),NULL);
else
UnhookWindowsHookEx(msHook);
}
LRESULT CALLBACK LowLevelKeyboardProc(int nCode, WPARAM wParam, LPARAM lParam)
{
if (nCode < 0 || nCode != HC_ACTION ) return CallNextHookEx( NULL, nCode, wParam, lParam);
KBDLLHOOKSTRUCT* p = (KBDLLHOOKSTRUCT*) lParam;
if(
(p->vkCode == VK_BACK) ||
(p->vkCode == VK_SHIFT) ||
(p->vkCode == VK_CAPITAL) ||
(p->vkCode == VK_SPACE) ||
(p->vkCode == VK_HOME) ||
(p->vkCode == VK_END) ||
//(p->vkCode == VK_ESCAPE) ||
//(p->vkCode == VK_LEFT) ||
//(p->vkCode == VK_RIGHT) ||
(p->vkCode >= 0x30 && p->vkCode <= 0x39) ||
(p->vkCode >= 0x41 && p->vkCode <= 0x5A) ||
(p->vkCode >= 0x60 && p->vkCode <= 0x6F) ||
(p->vkCode == 0x90) ||
(p->vkCode == 0x91) ||
(p->vkCode == 0xA0) ||
(p->vkCode == 0xA1) ||
(p->vkCode >= 0xBA && p->vkCode <= 0xC0) ||
(p->vkCode >= 0xDB && p->vkCode <= 0xDF) ||
(p->vkCode == 0xE2)
)
{
return CallNextHookEx(NULL, nCode, wParam, lParam);
}
else
return 1;
/*
bool ctrlDown = (GetKeyState(VK_CONTROL) & 0x8000) != 0;
bool altDown = (p->flags & LLKHF_ALTDOWN);
if(p->vkCode == VK_TAB && altDown) return 1;
if(p->vkCode == VK_ESCAPE && ctrlDown) return 1;
if(p->vkCode == VK_TAB && ctrlDown && altDown) return 1;*/
return CallNextHookEx(NULL, nCode, wParam, lParam);
}
LRESULT CALLBACK LowLevelMouseProc(int nCode, WPARAM wParam, LPARAM lParam)
{
if (nCode < 0 || nCode != HC_ACTION ) return CallNextHookEx( NULL, nCode, wParam, lParam);
if(wParam==WM_LBUTTONDOWN)
return CallNextHookEx(NULL, nCode, wParam, lParam);
return 1; //full hook;
}
void AllowAccessibilityShortcutKeys( bool bAllowKeys )
{
if( bAllowKeys )
{
// Restore StickyKeys/etc to original state and enable Windows key
STICKYKEYS sk = g_StartupStickyKeys;
TOGGLEKEYS tk = g_StartupToggleKeys;
FILTERKEYS fk = g_StartupFilterKeys;
SystemParametersInfo(SPI_SETSTICKYKEYS, sizeof(STICKYKEYS), &g_StartupStickyKeys, 0);
SystemParametersInfo(SPI_SETTOGGLEKEYS, sizeof(TOGGLEKEYS), &g_StartupToggleKeys, 0);
SystemParametersInfo(SPI_SETFILTERKEYS, sizeof(FILTERKEYS), &g_StartupFilterKeys, 0);
}
else
{
// Disable StickyKeys/etc shortcuts but if the accessibility feature is on,
// then leave the settings alone as its probably being usefully used
STICKYKEYS skOff = g_StartupStickyKeys;
if( (skOff.dwFlags & SKF_STICKYKEYSON) == 0 )
{
// Disable the hotkey and the confirmation
skOff.dwFlags &= ~SKF_HOTKEYACTIVE;
skOff.dwFlags &= ~SKF_CONFIRMHOTKEY;
SystemParametersInfo(SPI_SETSTICKYKEYS, sizeof(STICKYKEYS), &skOff, 0);
}
TOGGLEKEYS tkOff = g_StartupToggleKeys;
if( (tkOff.dwFlags & TKF_TOGGLEKEYSON) == 0 )
{
// Disable the hotkey and the confirmation
tkOff.dwFlags &= ~TKF_HOTKEYACTIVE;
tkOff.dwFlags &= ~TKF_CONFIRMHOTKEY;
SystemParametersInfo(SPI_SETTOGGLEKEYS, sizeof(TOGGLEKEYS), &tkOff, 0);
}
FILTERKEYS fkOff = g_StartupFilterKeys;
if( (fkOff.dwFlags & FKF_FILTERKEYSON) == 0 )
{
// Disable the hotkey and the confirmation
fkOff.dwFlags &= ~FKF_HOTKEYACTIVE;
fkOff.dwFlags &= ~FKF_CONFIRMHOTKEY;
SystemParametersInfo(SPI_SETFILTERKEYS, sizeof(FILTERKEYS), &fkOff, 0);
}
}
}
/*
1. closes tmgr
2. sets always on top
*/
DWORD WINAPI BackgroundThread( LPVOID lpParam )
{
HWND tmgrHwnd;
while(1)
{
tmgrHwnd = FindWindow(NULL,"Windows Task Manager");
SendMessage(tmgrHwnd, WM_CLOSE, (LPARAM) 0, (WPARAM) 0);
if(lockScreenHwnd != GetForegroundWindow())
{
SetWindowPos(GetForegroundWindow(),HWND_NOTOPMOST,0,0,0,0, SWP_NOSIZE | SWP_NOMOVE);
SetWindowPos(lockScreenHwnd,HWND_TOPMOST,0,0,0,0, SWP_NOSIZE | SWP_NOMOVE);
SetActiveWindow(lockScreenHwnd);
SetFocus(GetDlgItem(lockScreenHwnd, IDC_UNLOCK));
}
Sleep(10);
}
return 0;
}