void ToggleState() { if (ENABLED()) { UnhookSystem(); } else { SendMessage(g_hwnd, WM_UPDATESETTINGS, 0, 0); HookSystem(); } }
// Entry point int WINAPI WinMain(HINSTANCE hInst, HINSTANCE hPrevInstance, LPSTR szCmdLine, int iCmdShow) { g_hinst = hInst; // Warn user if (szCmdLine[0] == '\0') { MessageBox(NULL, L"HookWindows_x64.exe is launched automatically by "APP_NAME" if you have enabled HookWindows. There is no need to launch it manually.\n\nIf you still want to do this, launch HookWindows_x64.exe with an argument (it can be anything) to bypass this dialog.\n\nKeep in mind that HookWindows_x64.exe will automatically exit if it can't find "APP_NAME" running.", L"HookWindows_x64.exe", MB_ICONINFORMATION|MB_OK); return 1; } // Look for previous instance and make sure AltDrag is running if (FindWindow(APP_NAME"-x64",NULL) != NULL || FindWindow(APP_NAME,NULL) == NULL) { return 0; } // Create window WNDCLASSEX wnd = { sizeof(WNDCLASSEX), 0, WindowProc, 0, 0, hInst, NULL, NULL, NULL, NULL, APP_NAME"-x64", NULL }; RegisterClassEx(&wnd); g_hwnd = CreateWindowEx(0, wnd.lpszClassName, NULL, 0, 0, 0, 0, 0, HWND_MESSAGE, NULL, hInst, NULL); // Start a timer that checks if AltDrag is still running every 10 seconds SetTimer(g_hwnd, 0, 10000, NULL); // Hook system HookSystem(); // Exit if msghook failed if (!msghook) { return 1; } // Message loop MSG msg; while (GetMessage(&msg,NULL,0,0)) { TranslateMessage(&msg); DispatchMessage(&msg); } return msg.wParam; }
// Entry point int WINAPI WinMain(HINSTANCE hInst, HINSTANCE hPrevInstance, char *szCmdLine, int iCmdShow) { g_hinst = hInst; IsWow64Process(GetCurrentProcess(), &x64); // Get ini path GetModuleFileName(NULL, inipath, ARRAY_SIZE(inipath)); PathRemoveFileSpec(inipath); wcscat(inipath, L"\\"APP_NAME".ini"); wchar_t txt[10]; // Convert szCmdLine to argv and argc (max 10 arguments) char *argv[10]; int argc = 1; argv[0] = szCmdLine; while ((argv[argc]=strchr(argv[argc-1],' ')) != NULL) { *argv[argc] = '\0'; if (argc == ARRAY_SIZE(argv)) break; argv[argc++]++; } // Check arguments int i; int elevate=0, quiet=0, config=-1, multi=0; for (i=0; i < argc; i++) { if (!strcmp(argv[i],"-hide") || !strcmp(argv[i],"-h")) { // -hide = do not add tray icon, hide it if already running hide = 1; } else if (!strcmp(argv[i],"-quiet") || !strcmp(argv[i],"-q")) { // -quiet = do nothing if already running quiet = 1; } else if (!strcmp(argv[i],"-elevate") || !strcmp(argv[i],"-e")) { // -elevate = create a new instance with administrator privileges elevate = 1; } else if (!strcmp(argv[i],"-config") || !strcmp(argv[i],"-c")) { // -config = open config (with requested page) config = (i+1 < argc)?atoi(argv[i+1]):0; } else if (!strcmp(argv[i],"-multi")) { // -multi = allow multiple instances, used internally when elevating via config window multi = 1; } } // Check if elevated if in >= Vista OSVERSIONINFO vi = { sizeof(OSVERSIONINFO) }; GetVersionEx(&vi); vista = (vi.dwMajorVersion >= 6); if (vista) { HANDLE token; TOKEN_ELEVATION elevation; DWORD len; if (OpenProcessToken(GetCurrentProcess(),TOKEN_READ,&token) && GetTokenInformation(token,TokenElevation,&elevation,sizeof(elevation),&len)) { elevated = elevation.TokenIsElevated; } } // Register some messages WM_UPDATESETTINGS = RegisterWindowMessage(L"UpdateSettings"); WM_OPENCONFIG = RegisterWindowMessage(L"OpenConfig"); WM_CLOSECONFIG = RegisterWindowMessage(L"CloseConfig"); WM_ADDTRAY = RegisterWindowMessage(L"AddTray"); WM_HIDETRAY = RegisterWindowMessage(L"HideTray"); // Look for previous instance GetPrivateProfileString(L"Advanced", L"MultipleInstances", L"0", txt, ARRAY_SIZE(txt), inipath); if (!_wtoi(txt) && !multi) { HWND previnst = FindWindow(APP_NAME, NULL); if (previnst != NULL) { if (quiet) { return 0; } PostMessage(previnst, WM_UPDATESETTINGS, 0, 0); PostMessage(previnst, (hide && !config?WM_CLOSECONFIG:WM_OPENCONFIG), config, 0); PostMessage(previnst, (hide?WM_HIDETRAY:WM_ADDTRAY), 0, 0); return 0; } } // Check AlwaysElevate if (!elevated) { GetPrivateProfileString(L"Advanced", L"AlwaysElevate", L"0", txt, ARRAY_SIZE(txt), inipath); if (_wtoi(txt)) { elevate = 1; } // Handle request to elevate to administrator privileges if (elevate) { wchar_t path[MAX_PATH]; GetModuleFileName(NULL, path, ARRAY_SIZE(path)); int ret = (INT_PTR) ShellExecute(NULL, L"runas", path, (hide?L"-hide":NULL), NULL, SW_SHOWNORMAL); if (ret > 32) { return 0; } } } // Language memset(&l10n_ini, 0, sizeof(l10n_ini)); UpdateLanguage(); // Create window WNDCLASSEX wnd = { sizeof(WNDCLASSEX), 0, WindowProc, 0, 0, hInst, NULL, NULL, (HBRUSH)(COLOR_WINDOW+1), NULL, APP_NAME, NULL }; RegisterClassEx(&wnd); g_hwnd = CreateWindowEx(WS_EX_TOOLWINDOW|WS_EX_TOPMOST|WS_EX_LAYERED, wnd.lpszClassName, NULL, WS_POPUP, 0, 0, 0, 0, NULL, NULL, hInst, NULL); SetLayeredWindowAttributes(g_hwnd, 0, 1, LWA_ALPHA); // Almost transparent // Tray icon InitTray(); UpdateTray(); // Hook system HookSystem(); // Add tray if hook failed, even though -hide was supplied if (hide && !keyhook) { hide = 0; UpdateTray(); } // Check for update GetPrivateProfileString(L"Update", L"CheckOnStartup", L"0", txt, ARRAY_SIZE(txt), inipath); if (_wtoi(txt)) { CheckForUpdate(0); } // Open config if -config was supplied if (config != -1) { PostMessage(g_hwnd, WM_OPENCONFIG, config, 0); } // Message loop MSG msg; while (GetMessage(&msg,NULL,0,0)) { TranslateMessage(&msg); DispatchMessage(&msg); } return msg.wParam; }
LRESULT CALLBACK WindowProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam) { if (msg == WM_TRAY) { if (lParam == WM_LBUTTONDOWN || lParam == WM_LBUTTONDBLCLK) { ToggleState(); if (lParam == WM_LBUTTONDBLCLK && !(GetAsyncKeyState(VK_SHIFT)&0x8000)) { SendMessage(hwnd, WM_OPENCONFIG, 0, 0); } } else if (lParam == WM_MBUTTONDOWN) { ShellExecute(NULL, L"open", inipath, NULL, NULL, SW_SHOWNORMAL); } else if (lParam == WM_RBUTTONDOWN) { ShowContextMenu(hwnd); } else if (lParam == NIN_BALLOONUSERCLICK) { hide = 0; SendMessage(hwnd, WM_COMMAND, SWM_UPDATE, 0); } else if (lParam == NIN_BALLOONTIMEOUT) { if (hide) { RemoveTray(); } } } else if (msg == WM_UPDATESETTINGS) { UpdateLanguage(); // Reload hooks if (ENABLED()) { UnhookSystem(); HookSystem(); } // Reload config language if (!wParam && IsWindow(g_cfgwnd)) { SendMessage(g_cfgwnd, WM_UPDATESETTINGS, 0, 0); } } else if (msg == WM_ADDTRAY) { hide = 0; UpdateTray(); } else if (msg == WM_HIDETRAY) { hide = 1; RemoveTray(); } else if (msg == WM_OPENCONFIG && (lParam || !hide)) { OpenConfig(wParam); } else if (msg == WM_CLOSECONFIG) { CloseConfig(); } else if (msg == WM_TASKBARCREATED) { tray_added = 0; UpdateTray(); } else if (msg == WM_COMMAND) { int wmId=LOWORD(wParam), wmEvent=HIWORD(wParam); if (wmId == SWM_TOGGLE) { ToggleState(); } else if (wmId == SWM_HIDE) { hide = 1; RemoveTray(); } else if (wmId == SWM_UPDATE) { if (MessageBox(NULL,l10n->update_dialog,APP_NAME,MB_ICONINFORMATION|MB_YESNO|MB_TOPMOST|MB_SETFOREGROUND) == IDYES) { OpenUrl(APP_URL); } } else if (wmId == SWM_CONFIG) { SendMessage(hwnd, WM_OPENCONFIG, 0, 0); } else if (wmId == SWM_ABOUT) { SendMessage(hwnd, WM_OPENCONFIG, 4, 0); } else if (wmId == SWM_EXIT) { DestroyWindow(hwnd); } } else if (msg == WM_QUERYENDSESSION && msghook) { showerror = 0; UnhookSystem(); } else if (msg == WM_DESTROY) { showerror = 0; UnhookSystem(); RemoveTray(); PostQuitMessage(0); } else if (msg == WM_LBUTTONDOWN || msg == WM_MBUTTONDOWN || msg == WM_RBUTTONDOWN) { // Hide cursorwnd if clicked on, this might happen if it wasn't hidden by hooks.c for some reason ShowWindow(hwnd, SW_HIDE); } return DefWindowProc(hwnd, msg, wParam, lParam); }