|
| 1 | +#if defined(UNICODE) && !defined(_UNICODE) |
| 2 | + #define _UNICODE |
| 3 | +#elif defined(_UNICODE) && !defined(UNICODE) |
| 4 | + #define UNICODE |
| 5 | +#endif |
| 6 | + |
| 7 | +#include <stdio.h> |
| 8 | +#include <tchar.h> |
| 9 | +#include <windows.h> |
| 10 | +#include <string.h> |
| 11 | +#include <limits.h> |
| 12 | +#include <unistd.h> |
| 13 | +#include <stdio.h> |
| 14 | +#include <stdlib.h> |
| 15 | + |
| 16 | +char usingWhile = 1; |
| 17 | +char usingKeyboard = 0; |
| 18 | +char UsingTimerShutDown = 0; |
| 19 | +char isPressedNo = 0; |
| 20 | +char numberTemp[10]; |
| 21 | +char currentDir[PATH_MAX]; |
| 22 | + |
| 23 | +int currentWaktu = 0; |
| 24 | +int waktuLimit = 0; |
| 25 | +int currentCountDown = 0, currentCountDown2 = 0; |
| 26 | +int countDown = 0; |
| 27 | + |
| 28 | +long lastX = 0; |
| 29 | +long lastY = 0; |
| 30 | +long FilterMousePosX = 0; |
| 31 | +long FilterMousePosY = 0; |
| 32 | + |
| 33 | +LRESULT CALLBACK WindowProcedure (HWND, UINT, WPARAM, LPARAM); |
| 34 | +TCHAR szClassName[ ] = _T("AutoShutdown"); |
| 35 | +HWND tField, buttonYes, buttonNo, mainMessage, mainWindow; |
| 36 | +MSG messages, keyMessage; |
| 37 | +HHOOK hHook; |
| 38 | +DWORD IDThread[3]; |
| 39 | +POINT mousePoint; |
| 40 | +HANDLE HThread[3]; |
| 41 | + |
| 42 | +char* myitoa(int num) { |
| 43 | + if (numberTemp == NULL) return NULL; |
| 44 | + sprintf(numberTemp, "%d", num); |
| 45 | + return numberTemp; |
| 46 | +} |
| 47 | + |
| 48 | +int getCharTotal(char* varStr, char valChar) { |
| 49 | + int res = 0; |
| 50 | + for (int a = 0; a < strlen(varStr); a++) if (varStr[a] == valChar) res++; |
| 51 | + return res; |
| 52 | +} |
| 53 | + |
| 54 | +LRESULT CALLBACK keyboard_hook(const int code, const WPARAM wParam, const LPARAM lParam) { |
| 55 | + if (wParam == WM_KEYDOWN) usingKeyboard = 1; |
| 56 | + return CallNextHookEx(hHook, code, wParam, lParam); |
| 57 | +} |
| 58 | + |
| 59 | +void loadConfig() { |
| 60 | + char resultWindowConf[33]; |
| 61 | + memset(resultWindowConf, 0, 33); |
| 62 | + snprintf(resultWindowConf, 33, "%c:/AutoShutdown/AutoShutdown.conf", currentDir[0]); |
| 63 | + FILE* fp = fopen(resultWindowConf, "r"); |
| 64 | + if (!fp) { |
| 65 | + waktuLimit = 5; |
| 66 | + countDown = 30; |
| 67 | + FilterMousePosX = 0; |
| 68 | + FilterMousePosY = 0; |
| 69 | + } |
| 70 | + else { |
| 71 | + fseek(fp, 0, SEEK_END); |
| 72 | + int fSize = ftell(fp); |
| 73 | + fseek(fp, 0, SEEK_SET); |
| 74 | + char* fData = malloc(fSize); |
| 75 | + fread(fData, fSize, 1, fp); |
| 76 | + fclose(fp); |
| 77 | + memcpy(&waktuLimit, fData, 4); |
| 78 | + memcpy(&countDown, fData + 4, 4); |
| 79 | + memcpy(&FilterMousePosX, fData + 8, 4); |
| 80 | + memcpy(&FilterMousePosY, fData + 12, 4); |
| 81 | + free(fData); |
| 82 | + } |
| 83 | +} |
| 84 | + |
| 85 | +int WINAPI WinMain (HINSTANCE hThisInstance, |
| 86 | + HINSTANCE hPrevInstance, |
| 87 | + LPSTR lpszArgument, |
| 88 | + int nCmdShow) |
| 89 | +{ |
| 90 | + getcwd(currentDir, PATH_MAX); |
| 91 | + if (lpszArgument[0] == '1') { |
| 92 | + HWND hWnd = GetConsoleWindow(); |
| 93 | + ShowWindow( hWnd, SW_HIDE ); |
| 94 | + |
| 95 | + WNDCLASSEX wincl; /* Data structure for the windowclass */ |
| 96 | + |
| 97 | + wincl.hInstance = hThisInstance; |
| 98 | + wincl.lpszClassName = szClassName; |
| 99 | + wincl.lpfnWndProc = WindowProcedure; /* This function is called by windows */ |
| 100 | + wincl.style = CS_DBLCLKS; /* Catch double-clicks */ |
| 101 | + wincl.cbSize = sizeof (WNDCLASSEX); |
| 102 | + |
| 103 | + wincl.hIcon = LoadIcon (NULL, IDI_APPLICATION); |
| 104 | + wincl.hIconSm = LoadIcon (NULL, IDI_APPLICATION); |
| 105 | + wincl.hCursor = LoadCursor (NULL, IDC_ARROW); |
| 106 | + wincl.lpszMenuName = NULL; /* No menu */ |
| 107 | + wincl.cbClsExtra = 0; /* No extra bytes after the window class */ |
| 108 | + wincl.cbWndExtra = 0; /* structure or the window instance */ |
| 109 | + wincl.hbrBackground = (HBRUSH)COLOR_WINDOW; |
| 110 | + if (!RegisterClassEx (&wincl)) return 0; |
| 111 | + RECT rect; |
| 112 | + |
| 113 | + GetClientRect(GetDesktopWindow(), &rect); |
| 114 | + rect.left = (rect.right/2) - (330/2); |
| 115 | + rect.top = (rect.bottom/2) - (120/2); |
| 116 | + |
| 117 | + mainWindow = CreateWindowEx (0, /* Extended possibilites for variation */ |
| 118 | + szClassName, /* Classname */ |
| 119 | + _T("AutoShutdown"), /* Title Text */ |
| 120 | + WS_BORDER, /* default window */ |
| 121 | + rect.left, /* Windows decides the position */ |
| 122 | + rect.top, /* where the window ends up on the screen */ |
| 123 | + 330, /* The programs width */ |
| 124 | + 120, /* and height in pixels */ |
| 125 | + HWND_DESKTOP, /* The window is a child-window to desktop */ |
| 126 | + NULL, /* No menu */ |
| 127 | + hThisInstance, /* Program Instance handler */ |
| 128 | + NULL /* No Window Creation data */ |
| 129 | + ); |
| 130 | + |
| 131 | + ShowWindow (mainWindow, SW_HIDE); |
| 132 | + SetWindowPos(mainWindow, HWND_TOPMOST, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE); |
| 133 | + while (GetMessage (&messages, NULL, 0, 0)) { |
| 134 | + TranslateMessage(&messages); |
| 135 | + DispatchMessage(&messages); |
| 136 | + } |
| 137 | + return messages.wParam; |
| 138 | + } else { |
| 139 | + printf("[+] AutoShutdown (C) Kevin Adhaikal\n"); |
| 140 | + printf("\n[+] Jika anda tidak mau menggunakan AutoShutdown, anda bisa mengclose console ini"); |
| 141 | + printf("\n[+] Jika konsol nya hilang sendiri, berarti aplikasi nya berjalan tanpa ada masalah!\n"); |
| 142 | + printf("\n[+] AutoShutdown dimulai dalam 5"); |
| 143 | + Sleep(1000); |
| 144 | + printf("\b4"); |
| 145 | + Sleep(1000); |
| 146 | + printf("\b3"); |
| 147 | + Sleep(1000); |
| 148 | + printf("\b2"); |
| 149 | + Sleep(1000); |
| 150 | + printf("\b1"); |
| 151 | + Sleep(1000); |
| 152 | + char* currentDirArr[getCharTotal(currentDir, '\\') + 1]; |
| 153 | + char* strtokVar = strtok(currentDir, "\\"); |
| 154 | + int currentDirPos = 0; |
| 155 | + while (strtokVar != NULL) { |
| 156 | + currentDirArr[currentDirPos++] = strtokVar; |
| 157 | + strtokVar = strtok(NULL, "\\"); |
| 158 | + } |
| 159 | + char resultWindowExe[40]; |
| 160 | + memset(resultWindowExe, 0, 40); |
| 161 | + snprintf(resultWindowExe, 40, "start %s/AutoShutdown/AutoShutdown.exe 1", currentDirArr[0]); |
| 162 | + system(resultWindowExe); |
| 163 | + exit(0); |
| 164 | + } |
| 165 | + return 0; |
| 166 | +} |
| 167 | + |
| 168 | +void keyEvent() { |
| 169 | + hHook = SetWindowsHookEx(WH_KEYBOARD_LL, keyboard_hook, NULL, 0); |
| 170 | + while (GetMessage(&keyMessage, NULL, 0, 0)); |
| 171 | +} |
| 172 | + |
| 173 | +void changeButtonYes() { |
| 174 | + isPressedNo = 0; |
| 175 | + for (currentCountDown = countDown; currentCountDown > 0; currentCountDown--) { |
| 176 | + char strTime[5 + strlen(myitoa(currentCountDown)) + 1]; |
| 177 | + sprintf(strTime, "Yes (%d)", currentCountDown); |
| 178 | + SetWindowText(buttonYes, strTime); |
| 179 | + Sleep(1000); |
| 180 | + currentCountDown2++; |
| 181 | + } |
| 182 | + if ((UsingTimerShutDown || currentCountDown2 == countDown) && !isPressedNo) { |
| 183 | + currentCountDown2 = 0; |
| 184 | + ShowWindow(mainWindow, SW_HIDE); |
| 185 | + system("shutdown -s -f -t 0"); |
| 186 | + } else currentCountDown2 = 0; |
| 187 | +} |
| 188 | + |
| 189 | +void runningEvent() { |
| 190 | + HThread[0] = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)keyEvent, NULL, 0, &IDThread[0]); |
| 191 | + |
| 192 | + while (usingWhile) { |
| 193 | + Sleep(1000); |
| 194 | + GetCursorPos(&mousePoint); |
| 195 | + |
| 196 | + if ((mousePoint.x < lastX + FilterMousePosX + 1 && mousePoint.x > lastX - (FilterMousePosX + 1)) && (mousePoint.y < lastY + FilterMousePosY + 1 && mousePoint.y > lastY - (FilterMousePosY + 1))) { |
| 197 | + currentWaktu++; |
| 198 | + if (currentWaktu > waktuLimit) usingWhile = 0; |
| 199 | + } else { |
| 200 | + currentWaktu = 0; |
| 201 | + usingKeyboard = 0; |
| 202 | + } |
| 203 | + lastX = mousePoint.x; |
| 204 | + lastY = mousePoint.y; |
| 205 | + } |
| 206 | + |
| 207 | + usingWhile = 1; |
| 208 | + currentWaktu = 0; |
| 209 | + |
| 210 | + PostThreadMessage(IDThread[0], WM_QUIT, 0, 0); |
| 211 | + CloseHandle(HThread[0]); |
| 212 | + ShowWindow(mainWindow, SW_SHOW); |
| 213 | + |
| 214 | + HThread[1] = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)changeButtonYes, NULL, 0, &IDThread[1]); |
| 215 | +} |
| 216 | + |
| 217 | +LRESULT CALLBACK WindowProcedure (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam) { |
| 218 | + switch (message) { |
| 219 | + case WM_CREATE: { |
| 220 | + tField = CreateWindow("STATIC", |
| 221 | + "Apakah komputer ini mau di matikan sekarang", |
| 222 | + WS_VISIBLE | WS_CHILD, |
| 223 | + 15,5,320,20, |
| 224 | + hwnd, NULL, NULL, NULL); |
| 225 | + |
| 226 | + buttonYes = CreateWindow("BUTTON", |
| 227 | + "Yes", |
| 228 | + WS_VISIBLE | WS_CHILD | WS_BORDER, |
| 229 | + 200, 40, 100, 40, |
| 230 | + hwnd, (HMENU)1, NULL, NULL); |
| 231 | + |
| 232 | + buttonNo = CreateWindow("BUTTON", |
| 233 | + "No", |
| 234 | + WS_VISIBLE | WS_CHILD | WS_BORDER, |
| 235 | + 20, 40, 100, 40, |
| 236 | + hwnd, (HMENU)2, NULL, NULL); |
| 237 | + loadConfig(); |
| 238 | + HThread[2] = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)runningEvent, NULL, 0, &IDThread[2]); |
| 239 | + break; |
| 240 | + } |
| 241 | + case WM_COMMAND: { |
| 242 | + switch(LOWORD(wParam)) { |
| 243 | + case 1: { |
| 244 | + UsingTimerShutDown = 1; |
| 245 | + currentCountDown = 0; |
| 246 | + ShowWindow(mainWindow, SW_HIDE); |
| 247 | + break; |
| 248 | + } |
| 249 | + case 2: { |
| 250 | + UsingTimerShutDown = 0; |
| 251 | + currentCountDown = 0; |
| 252 | + isPressedNo = 1; |
| 253 | + CloseHandle(HThread[1]); |
| 254 | + CloseHandle(HThread[2]); |
| 255 | + ShowWindow(mainWindow, SW_HIDE); |
| 256 | + HThread[2] = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)runningEvent, NULL, 0, &IDThread[2]); |
| 257 | + break; |
| 258 | + } |
| 259 | + default: { |
| 260 | + break; |
| 261 | + } |
| 262 | + } |
| 263 | + break; |
| 264 | + } |
| 265 | + case WM_DESTROY: { |
| 266 | + PostQuitMessage(0); |
| 267 | + break; |
| 268 | + } |
| 269 | + case WM_CLOSE: { |
| 270 | + break; |
| 271 | + } |
| 272 | + default: { |
| 273 | + return DefWindowProc (hwnd, message, wParam, lParam); |
| 274 | + } |
| 275 | + } |
| 276 | + return 0; |
| 277 | +} |
0 commit comments