Guest User

lolhax - Trick window into activate

a guest
Aug 11th, 2021
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.06 KB | None | 0 0
  1. #include <Windows.h>
  2. #include <iostream>
  3.  
  4. void SetVisible(HWND WindowHWND, bool WindowVisible) {
  5.  
  6.     int WindowState = 0;
  7.     if (WindowVisible == true) {
  8.         WindowState = SW_SHOW;
  9.     }
  10.     else {
  11.         WindowState = SW_HIDE;
  12.     }
  13.  
  14.     ShowWindowAsync(WindowHWND, WindowState);
  15. }
  16.  
  17. bool CheckArg(const char* ArgumentName, int CurrentIndex, int Expected, int TotalArguments, char** Arguments) {
  18.     return (_strcmpi(Arguments[CurrentIndex], ArgumentName) == 0 && ((CurrentIndex + Expected) < TotalArguments));
  19. }
  20.  
  21. int main(int argc, char** argv)
  22. {
  23.  
  24.     char* ClassName = nullptr;
  25.     char* Caption = nullptr;
  26.     int KeyCode = 0;
  27.  
  28.     if (argc == 1) {
  29.         std::cout << "No arguments specified!\r\n\r\n";
  30.         std::cout << "/classname ""Window Classname""\r\n";
  31.         std::cout << "/caption ""Window Caption""\r\n";
  32.         std::cout << "/simulatekey 0xKeyCode - ie ESC=0x1B\r\n";
  33.         return 1;
  34.     }
  35.     else {
  36.  
  37.         for (int i = 1; i < argc; i++) {
  38.  
  39.             if (CheckArg("/classname", i, 1, argc, argv)) {
  40.                 ClassName = argv[++i];
  41.                 continue;
  42.             } else if (CheckArg("/caption", i, 1, argc, argv)) {
  43.                 Caption = argv[++i];
  44.                 continue;
  45.             }
  46.             else if (CheckArg("/simulatekey", i, 1, argc, argv)) {
  47.                 KeyCode = strtol(argv[++i], NULL, 16);
  48.                 continue;
  49.             }
  50.  
  51.         }
  52.     }
  53.  
  54.     HWND TargetHWND = FindWindowA(ClassName, Caption);
  55.  
  56.     if (TargetHWND != nullptr) {
  57.         printf("Target Window: 0x%p\r\n", TargetHWND);
  58.         SetVisible(TargetHWND, false);
  59.         SetVisible(TargetHWND, true);
  60.        
  61.         if (KeyCode != 0) {
  62.             //Simulate pressing ESC
  63.             printf("Simulating keypress\r\n");
  64.             SendMessage(TargetHWND, WM_KEYDOWN, KeyCode, 0);
  65.             SendMessage(TargetHWND, WM_KEYUP, KeyCode, 0);
  66.         }
  67.     }
  68.     else {
  69.         printf("Unable to find window - classname: %s, caption: %s", ClassName, Caption);
  70.     }
  71.  
  72.     return 0;
  73. }
Advertisement
Add Comment
Please, Sign In to add comment