Esempio n. 1
0
int UnhookKeyboard() {
  if (!keyhook) {
    // Keyboard not hooked
    return 1;
  }

  // Remove keyboard hook
  if (UnhookWindowsHookEx(keyhook) == 0) {
    #ifdef DEBUG
    Error(L"UnhookWindowsHookEx(keyhook)", L"Could not unhook keyboard. Try restarting "APP_NAME".", GetLastError());
    #else
    if (showerror) {
      MessageBox(NULL, l10n.unhook_error, APP_NAME, MB_ICONINFORMATION|MB_OK|MB_TOPMOST|MB_SETFOREGROUND);
    }
    #endif
  }

  // Remove mouse hook (it probably isn't hooked, but just in case)
  UnhookMouse();

  // Success
  keyhook = NULL;
  UpdateTray();
  return 0;
}
Esempio n. 2
0
// Entry point
int WINAPI WinMain(HINSTANCE hInst, HINSTANCE hPrevInstance, LPSTR szCmdLine, int iCmdShow) {
	g_hinst = hInst;

	// Look for previous instance
	HWND previnst = FindWindow(APP_NAME, NULL);
	if (previnst != NULL) {
		SendMessage(previnst, WM_COMMAND, SWM_FIND, 0);
		return 0;
	}

	// Create window
	WNDCLASSEX wnd = {sizeof(WNDCLASSEX), 0, WindowProc, 0, 0, hInst, NULL, NULL, (HBRUSH)(COLOR_WINDOW+1), NULL, APP_NAME, NULL};
	wnd.hCursor = LoadImage(hInst, L"find", IMAGE_CURSOR, 0, 0, LR_DEFAULTCOLOR);
	RegisterClassEx(&wnd);
	g_hwnd = CreateWindowEx(WS_EX_TOOLWINDOW|WS_EX_TOPMOST|WS_EX_LAYERED, wnd.lpszClassName, APP_NAME, WS_POPUP, 0, 0, 0, 0, NULL, NULL, hInst, NULL);
	SetLayeredWindowAttributes(g_hwnd, 0, 1, LWA_ALPHA); // Almost transparent

	// Tray icon
	InitTray();
	UpdateTray();

	// Hook mouse
	if ((GetAsyncKeyState(VK_SHIFT)&0x8000)) {
		HookMouse();
	}

	// Message loop
	MSG msg;
	while (GetMessage(&msg,NULL,0,0)) {
		TranslateMessage(&msg);
		DispatchMessage(&msg);
	}
	return msg.wParam;
}
Esempio n. 3
0
LRESULT CALLBACK WindowProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam) {
	if (msg == WM_TRAY) {
		if (lParam == WM_LBUTTONDOWN) {
			HookMouse();
		}
		else if (lParam == WM_MBUTTONDOWN) {
			Sleep(3000);
			HookMouse();
		}
		else if (lParam == WM_RBUTTONDOWN) {
			ShowContextMenu(hwnd);
		}
	}
	else if (msg == WM_TASKBARCREATED) {
		tray_added = 0;
		UpdateTray();
	}
	else if (msg == WM_COMMAND) {
		int wmId = LOWORD(wParam), wmEvent = HIWORD(wParam);
		if (wmId == SWM_FIND) {
			HookMouse();
		}
		else if (wmId == SWM_FINDDELAY) {
			Sleep(3000);
			HookMouse();
		}
		else if (wmId == SWM_FINDALL) {
			FindAllWnds();
		}
		else if (wmId == SWM_ABOUT) {
			MessageBox(NULL, l10n->about, l10n->about_title, MB_ICONINFORMATION|MB_OK);
		}
		else if (wmId == SWM_EXIT) {
			DestroyWindow(hwnd);
		}
	}
	else if (msg == WM_DESTROY) {
		showerror = 0;
		UnhookMouse();
		RemoveTray();
		PostQuitMessage(0);
		return 0;
	}
	else if (msg == WM_LBUTTONDOWN || msg == WM_MBUTTONDOWN || msg == WM_RBUTTONDOWN) {
		// Hide the window if clicked on, this might happen if it wasn't hidden by the hooks for some reason
		ShowWindow(hwnd, SW_HIDE);
		// Since we take away the cursor, make sure the mouse is unhooked
		UnhookMouse();
	}
	return DefWindowProc(hwnd, msg, wParam, lParam);
}
Esempio n. 4
0
int HookKeyboard() {
  if (keyhook) {
    // Keyboard already hooked
    return 1;
  }

  // Set up the hook
  keyhook = SetWindowsHookEx(WH_KEYBOARD_LL, LowLevelKeyboardProc, g_hinst, 0);
  if (keyhook == NULL) {
    Error(L"SetWindowsHookEx(WH_KEYBOARD_LL)", L"Could not hook keyboard. Another program might be interfering.", GetLastError());
    return 1;
  }

  // Success
  UpdateTray();
  return 0;
}
Esempio n. 5
0
void CMuleTrayIcon::SetTrayToolTip(const wxString& Tip)
{
	CurrentTip = Tip;
	UpdateTray();
}
Esempio n. 6
0
void CMuleTrayIcon::SetTrayIcon(int Icon, uint32 percent)
{
	int Bar_ySize = 0;

	switch (Icon) {
		case TRAY_ICON_HIGHID:
			// Most likely case, test first
			Bar_ySize = HighId_Icon_size; 
			break;
		case TRAY_ICON_LOWID:
			Bar_ySize = LowId_Icon_size; 
			break;
		case TRAY_ICON_DISCONNECTED:
			Bar_ySize = Disconnected_Icon_size; 
			break;
		default:
			wxFAIL;
	}

	// Lookup this values for speed improvement: don't draw if not needed
	int NewSize = (Bar_ySize * percent) / 100;
	
	if ((Old_Icon != Icon) || (Old_SpeedSize != NewSize)) {

		if ((Old_SpeedSize > NewSize) || (Old_Icon != Icon)) {
			// We have to rebuild the icon, because bar is lower now.
			switch (Icon) {
				case TRAY_ICON_HIGHID:
					// Most likely case, test first
					CurrentIcon = wxIcon(mule_TrayIcon_big_ico_xpm);
					break;
				case TRAY_ICON_LOWID:
					CurrentIcon = wxIcon(mule_Tr_yellow_big_ico_xpm);
					break;
				case TRAY_ICON_DISCONNECTED:
					CurrentIcon = wxIcon(mule_Tr_grey_big_ico_xpm);
					break;
				default:
					wxFAIL;
			}
		}

		Old_Icon = Icon;
		Old_SpeedSize = NewSize;
		
		// Do whatever to the icon before drawing it (percent)
		
		wxBitmap TempBMP;
		TempBMP.CopyFromIcon(CurrentIcon);
		
		TempBMP.SetMask(NULL);

		IconWithSpeed.SelectObject(TempBMP);

		
		// Speed bar is: centered, taking 80% of the icon heigh, and 
		// right-justified taking a 10% of the icon width.
		
		// X
		int Bar_xSize = 4; 
		int Bar_xPos = CurrentIcon.GetWidth() - 5; 
		
		IconWithSpeed.SetBrush(*(wxTheBrushList->FindOrCreateBrush(CStatisticsDlg::getColors(11))));
		IconWithSpeed.SetPen(*wxTRANSPARENT_PEN);
		
		IconWithSpeed.DrawRectangle(Bar_xPos + 1, Bar_ySize - NewSize, Bar_xSize -2 , NewSize);
		
		// Unselect the icon.
		IconWithSpeed.SelectObject(wxNullBitmap);
		
		// Do transparency

		// Set a new mask with transparency set to red.
		wxMask* new_mask = new wxMask(TempBMP, wxColour(0xFF, 0x00, 0x00));
		
		TempBMP.SetMask(new_mask);
		CurrentIcon.CopyFromBitmap(TempBMP);

		UpdateTray();
	}
}
Esempio n. 7
0
DWORD WINAPI _CheckForUpdate(LPVOID arg) {
	int verbose = *(int*)arg;
	free(arg);
	
	//Check if we should check for beta
	wchar_t path[MAX_PATH];
	GetModuleFileName(NULL, path, ARRAY_SIZE(path));
	PathRemoveFileSpec(path);
	wcscat(path, L"\\"APP_NAME".ini");
	wchar_t txt[10];
	GetPrivateProfileString(L"Update", L"Beta", L"0", txt, ARRAY_SIZE(txt), path);
	int beta = _wtoi(txt);
	
	//Check if we are connected to the internet
	DWORD flags; //Not really used
	int tries = 0; //Try at least ten times, sleep one second between each attempt
	while (InternetGetConnectedState(&flags,0) == FALSE) {
		tries++;
		if (!verbose) {
			Sleep(1000);
		}
		if (tries >= 10 || verbose) {
			if (verbose) {
				Error(L"InternetGetConnectedState()", L"No internet connection.\nPlease check for update manually on the website.", GetLastError(), TEXT(__FILE__), __LINE__);
			}
			return 1;
		}
	}
	
	//Open connection
	HINTERNET http = InternetOpen(APP_NAME L" - " APP_VERSION, INTERNET_OPEN_TYPE_DIRECT, NULL, NULL, 0);
	if (http == NULL) {
		if (verbose) {
			Error(L"InternetOpen()", L"Could not establish connection.\nPlease check for update manually on the website.", GetLastError(), TEXT(__FILE__), __LINE__);
		}
		return 1;
	}
	HINTERNET file = InternetOpenUrl(http, (beta?APP_UPDATE_UNSTABLE:APP_UPDATE_STABLE), NULL, 0, INTERNET_FLAG_RELOAD|INTERNET_FLAG_NO_CACHE_WRITE|INTERNET_FLAG_NO_AUTH|INTERNET_FLAG_NO_AUTO_REDIRECT|INTERNET_FLAG_NO_COOKIES|INTERNET_FLAG_NO_UI, 0);
	if (file == NULL) {
		if (verbose) {
			Error(L"InternetOpenUrl()", L"Could not establish connection.\nPlease check for update manually on the website.", GetLastError(), TEXT(__FILE__), __LINE__);
		}
		InternetCloseHandle(http);
		return 1;
	}
	//Read file
	char data[20];
	DWORD numread;
	if (InternetReadFile(file,data,sizeof(data),&numread) == FALSE) {
		if (verbose) {
			Error(L"InternetReadFile()", L"Could not read file.\nPlease check for update manually on the website.", GetLastError(), TEXT(__FILE__), __LINE__);
		}
		InternetCloseHandle(file);
		InternetCloseHandle(http);
		return 1;
	}
	data[numread] = '\0';
	//Get response code
	wchar_t code[4];
	DWORD len = sizeof(code);
	HttpQueryInfo(file, HTTP_QUERY_STATUS_CODE, &code, &len, NULL);
	//Close connection
	InternetCloseHandle(file);
	InternetCloseHandle(http);
	
	//Make sure the response is valid
	//strcpy(data, "Version: 1.3"); //This is the format of the new update string
	//char header[] = "Version: ";
	if (wcscmp(code,L"200") /*|| strstr(data,header) != data*/) {
		if (verbose) {
			MessageBox(NULL, L"Could not determine if an update is available.\n\nPlease check for update manually on the website.", TEXT(APP_NAME), MB_ICONWARNING|MB_OK);
		}
		return 2;
	}
	
	//New version available?
	//char *latest = data+strlen(header);
	//int cmp = strcmp(latest, APP_VERSION);
	int cmp = strcmp(data, APP_VERSION);
	if (cmp > 0 || (beta && cmp != 0)) {
		update = 1;
		if (verbose) {
			SendMessage(g_hwnd, WM_COMMAND, SWM_UPDATE, 0);
		}
		else {
			wcsncpy(tray.szInfo, l10n->update_balloon, ARRAY_SIZE(tray.szInfo));
			tray.uFlags |= NIF_INFO;
			UpdateTray();
			tray.uFlags ^= NIF_INFO;
		}
	}
	else {
		update = 0;
		if (verbose) {
			MessageBox(NULL, l10n->update_nonew, TEXT(APP_NAME), MB_ICONINFORMATION|MB_OK);
		}
	}
	return 0;
}
Esempio n. 8
0
// Entry point
int WINAPI WinMain(HINSTANCE hInst, HINSTANCE hPrevInstance, LPSTR szCmdLine, int iCmdShow) {
  g_hinst = hInst;

  // 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;
  for (i=0; i < argc; i++) {
    if (!strcmp(argv[i],"-elevate") || !strcmp(argv[i],"-e")) {
      // -elevate = create a new instance with administrator privileges
      elevate = 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");

  // 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, NULL, NULL, SW_SHOWNORMAL);
      if (ret > 32) {
        return 0;
      }
    }
  }

  // Create window
  WNDCLASSEX wnd = { sizeof(WNDCLASSEX), 0, WindowProc, 0, 0, hInst, NULL, NULL, (HBRUSH)(COLOR_WINDOW+1), NULL, APP_NAME, NULL };
  wnd.hCursor = LoadImage(hInst, L"kill", IMAGE_CURSOR, 0, 0, LR_DEFAULTCOLOR);
  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 keyboard
  HookKeyboard();

  // TimerCheck
  GetPrivateProfileString(L"General", L"TimerCheck", L"0", txt, ARRAY_SIZE(txt), inipath);
  if (_wtoi(txt)) {
    SetTimer(g_hwnd, CHECKTIMER, CHECKINTERVAL, NULL);
  }

  // Message loop
  MSG msg;
  while (GetMessage(&msg,NULL,0,0)) {
    TranslateMessage(&msg);
    DispatchMessage(&msg);
  }
  return msg.wParam;
}
Esempio n. 9
0
LRESULT CALLBACK WindowProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam) {
  if (msg == WM_TRAY) {
    if (lParam == WM_LBUTTONDOWN || lParam == WM_LBUTTONDBLCLK) {
      ToggleState();
    }
    else if (lParam == WM_MBUTTONDOWN) {
      if ((GetAsyncKeyState(VK_SHIFT)&0x8000)) {
        ShellExecute(NULL, L"open", inipath, NULL, NULL, SW_SHOWNORMAL);
      }
      else {
        HookMouse();
      }
    }
    else if (lParam == WM_RBUTTONUP) {
      ShowContextMenu(hwnd);
    }
  }
  else if (msg == WM_UPDATESETTINGS) {
    wchar_t txt[10];
    // TimerCheck
    KillTimer(g_hwnd, CHECKTIMER);
    GetPrivateProfileString(L"General", L"TimerCheck", L"0", txt, ARRAY_SIZE(txt), inipath);
    if (_wtoi(txt)) {
      SetTimer(g_hwnd, CHECKTIMER, CHECKINTERVAL, NULL);
    }
  }
  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_ELEVATE) {
      wchar_t path[MAX_PATH];
      GetModuleFileName(NULL, path, ARRAY_SIZE(path));
      int ret = (INT_PTR) ShellExecute(NULL, L"runas", path, NULL, NULL, SW_SHOWNORMAL);
      if (ret > 32) {
        DestroyWindow(hwnd);
      }
    }
    else if (wmId == SWM_AUTOSTART_ON) {
      SetAutostart(1, 0);
    }
    else if (wmId == SWM_AUTOSTART_OFF) {
      SetAutostart(0, 0);
    }
    else if (wmId == SWM_AUTOSTART_ELEVATE_ON) {
      SetAutostart(1, 1);
    }
    else if (wmId == SWM_AUTOSTART_ELEVATE_OFF) {
      SetAutostart(1, 0);
    }
    else if (wmId == SWM_TIMERCHECK_ON) {
      WritePrivateProfileString(L"General", L"TimerCheck", L"1", inipath);
      SendMessage(g_hwnd, WM_UPDATESETTINGS, 0, 0);
    }
    else if (wmId == SWM_TIMERCHECK_OFF) {
      WritePrivateProfileString(L"General", L"TimerCheck", L"0", inipath);
      SendMessage(g_hwnd, WM_UPDATESETTINGS, 0, 0);
    }
    else if (wmId == SWM_WEBSITE) {
      OpenUrl(APP_URL);
    }
    else if (wmId == SWM_XKILL) {
      HookMouse();
    }
    else if (wmId == SWM_EXIT) {
      DestroyWindow(hwnd);
    }
  }
  else if (msg == WM_DESTROY) {
    showerror = 0;
    UnhookKeyboard();
    UnhookMouse();
    RemoveTray();
    PostQuitMessage(0);
  }
  else if (msg == WM_LBUTTONDOWN || msg == WM_MBUTTONDOWN || msg == WM_RBUTTONDOWN) {
    // Hide the window if clicked on, this might happen if it wasn't hidden by the hooks for some reason
    ShowWindow(hwnd, SW_HIDE);
    // Since we take away the skull, make sure we can't kill anything
    UnhookMouse();
  }
  else if (msg == WM_TIMER) {
    if (wParam == CHECKTIMER && ENABLED()) {
      if (GetAsyncKeyState(VK_LCONTROL)&0x8000
       && GetAsyncKeyState(VK_LMENU)&0x8000
       && GetAsyncKeyState(VK_F4)&0x8000) {
        // Get hwnd of foreground window
        HWND hwnd = GetForegroundWindow();
        if (hwnd == NULL) {
          return DefWindowProc(hwnd, msg, wParam, lParam);
        }

        // Kill it!
        Kill(hwnd);
      }
      else {
        // Reset when the user has released the keys
        killing = 0;
      }
    }
  }
  return DefWindowProc(hwnd, msg, wParam, lParam);
}
Esempio n. 10
0
// 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;
}
Esempio n. 11
0
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);
}
Esempio n. 12
0
int UnhookSystem() {
    if (!keyhook && !msghook) {
        // System not hooked
        return 1;
    }

    // Remove keyboard hook
    if (keyhook) {
        if (UnhookWindowsHookEx(keyhook) == 0) {
#ifdef DEBUG
            Error(L"UnhookWindowsHookEx(keyhook)", L"Could not unhook keyboard. Try restarting "APP_NAME".", GetLastError());
#else
            if (showerror) {
                MessageBox(NULL, l10n->unhook_error, APP_NAME, MB_ICONINFORMATION|MB_OK|MB_TOPMOST|MB_SETFOREGROUND);
            }
#endif
        }
        keyhook = NULL;
    }

    // Remove message hook
    if (msghook) {
        if (UnhookWindowsHookEx(msghook) == 0) {
#ifdef DEBUG
            Error(L"UnhookWindowsHookEx(msghook)", L"Could not unhook message hook. Try restarting "APP_NAME".", GetLastError());
#endif
        }
        msghook = NULL;

        // Close HookWindows_x64.exe
        if (x64) {
            HWND window = FindWindow(L"AltDrag-x64", NULL);
            if (window != NULL) {
                PostMessage(window, WM_CLOSE, 0, 0);
            }
        }

        // Send dummy messages to all processes to make them unload hooks.dll
        EnumWindows(EnumWindowsProc, 0);
    }

    // Tell dll file that we are unloading
    void (*Unload)() = (void*) GetProcAddress(hinstDLL, "Unload");
    if (Unload == NULL) {
        Error(L"GetProcAddress('Unload')", L"This probably means that the file hooks.dll is from an old version or corrupt. You can try reinstalling "APP_NAME".", GetLastError());
    }
    else {
        Unload();
    }

    // Unload library
    if (hinstDLL) {
        if (FreeLibrary(hinstDLL) == 0) {
            Error(L"FreeLibrary()", L"Could not free hooks.dll. Try restarting "APP_NAME".", GetLastError());
        }
        hinstDLL = NULL;
    }

    // Success
    UpdateTray();
    return 0;
}
Esempio n. 13
0
int HookSystem() {
    if (keyhook && msghook) {
        // System already hooked
        return 1;
    }

    // Load library
    if (!hinstDLL) {
        wchar_t path[MAX_PATH];
        GetModuleFileName(NULL, path, ARRAY_SIZE(path));
        PathRemoveFileSpec(path);
        wcscat(path, L"\\hooks.dll");
        hinstDLL = LoadLibrary(path);
        if (hinstDLL == NULL) {
            Error(L"LoadLibrary('hooks.dll')", L"This probably means that the file hooks.dll is missing. You can try reinstalling "APP_NAME".", GetLastError());
            return 1;
        }
    }

    // Load keyboard hook
    HOOKPROC procaddr;
    if (!keyhook) {
        // Get address to keyboard hook (beware name mangling)
        procaddr = (HOOKPROC) GetProcAddress(hinstDLL, "LowLevelKeyboardProc@12");
        if (procaddr == NULL) {
            Error(L"GetProcAddress('LowLevelKeyboardProc@12')", L"This probably means that the file hooks.dll is from an old version or corrupt. You can try reinstalling "APP_NAME".", GetLastError());
            return 1;
        }
        // Set up the keyboard hook
        keyhook = SetWindowsHookEx(WH_KEYBOARD_LL, procaddr, hinstDLL, 0);
        if (keyhook == NULL) {
            Error(L"SetWindowsHookEx(WH_KEYBOARD_LL)", L"Could not hook keyboard. Another program might be interfering.", GetLastError());
            return 1;
        }
    }

    // HookWindows
    wchar_t txt[10];
    GetPrivateProfileString(L"Advanced", L"HookWindows", L"0", txt, ARRAY_SIZE(txt), inipath);
    if (!msghook && _wtoi(txt)) {
        // Get address to message hook (beware name mangling)
        procaddr = (HOOKPROC) GetProcAddress(hinstDLL, "CallWndProc@12");
        if (procaddr == NULL) {
            Error(L"GetProcAddress('CallWndProc@12')", L"This probably means that the file hooks.dll is from an old version or corrupt. You can try reinstalling "APP_NAME".", GetLastError());
            return 1;
        }
        // Set up the message hook
        msghook = SetWindowsHookEx(WH_CALLWNDPROC, procaddr, hinstDLL, 0);
        if (msghook == NULL) {
            Error(L"SetWindowsHookEx(WH_CALLWNDPROC)", L"Could not hook message hook. Another program might be interfering.", GetLastError());
            return 1;
        }

        // x64
        if (x64) {
            wchar_t path[MAX_PATH];
            GetModuleFileName(NULL, path, ARRAY_SIZE(path));
            PathRemoveFileSpec(path);
            wcscat(path, L"\\HookWindows_x64.exe");
            ShellExecute(NULL, L"open", path, L"nowarning", NULL, SW_SHOWNORMAL);
        }
    }

    // Success
    UpdateTray();
    return 0;
}