Advertisement
Guest User

focus.exe

a guest
Apr 2nd, 2016
202
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <Windows.h>
  2. #include <time.h>
  3. #include <stdio.h>
  4.  
  5. #pragma warning(disable: 4996)
  6.  
  7. int main(int argc, char* argv[])
  8. {
  9.     HWND hWnd = 0;
  10.     DWORD lastProcessId = 0;
  11.     WCHAR processImageName[260];
  12.     WCHAR msg[128];
  13.     time_t Time;
  14.  
  15.     while (1)
  16.     {
  17.         HWND hCurrentWnd;
  18.  
  19.         do
  20.         {
  21.             Sleep(0x64u);
  22.             hCurrentWnd = GetForegroundWindow();
  23.         } while (hCurrentWnd == hWnd);
  24.  
  25.         hWnd = hCurrentWnd;
  26.  
  27.         if (hWnd)
  28.         {
  29.             DWORD dwProcessId;
  30.  
  31.             GetWindowThreadProcessId(hWnd, &dwProcessId);
  32.             CloseHandle(hWnd);
  33.  
  34.             if (dwProcessId != lastProcessId)
  35.             {
  36.                 HANDLE process;
  37.  
  38.                 lastProcessId = dwProcessId;
  39.                 process = OpenProcess(0x1000u, 0, dwProcessId);
  40.  
  41.                 if (process)
  42.                 {
  43.                     DWORD lpDwSize = 260;
  44.  
  45.                     if (!QueryFullProcessImageNameW(process, 0, processImageName, &lpDwSize))
  46.                     {
  47.                         processImageName[0] = L'\0';
  48.                     }
  49.                    
  50.                     time(&Time);
  51.                    
  52.                     wsprintfW(msg, L"%s", _wasctime(localtime(&Time)));
  53.                     wprintf(L"%lu:%s | %s", dwProcessId, processImageName, msg);
  54.  
  55.                     CloseHandle(process);
  56.                 }
  57.                 else
  58.                 {
  59.                     time(&Time);
  60.  
  61.                     wsprintfW(msg, L"%s", _wasctime(localtime(&Time)));
  62.                     wprintf(L"%lu:Could not open process | %s", dwProcessId, msg);
  63.                 }
  64.             }
  65.         }
  66.         else
  67.         {
  68.             time(&Time);
  69.            
  70.             wsprintfW(msg, L"%s", _wasctime(localtime(&Time)));
  71.             wprintf(L"No foreground application | %s", msg);
  72.         }
  73.     }
  74. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement