Advertisement
toribio

foreground.exe

Jun 5th, 2011
260
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.22 KB | None | 0 0
  1. /*
  2.  * Get process name from foreground window example application
  3.  * For Pawn/C++ SA:MP Scripting use
  4.  * http://www.orkut.com.br/Main#Community?cmm=38308205
  5.  * Author: Flávio Toribio <flavio_toribio@hotmail.com>
  6. */
  7.  
  8. #include <windows.h>
  9. #include <stdio.h>
  10. #include <psapi.h>
  11. #include <tchar.h>
  12.  
  13. #pragma comment(lib, "psapi.lib")
  14. #pragma comment(lib, "user32.lib")
  15.  
  16. static BOOL bEnded = FALSE;
  17.  
  18. DWORD WINAPI mainThread(LPVOID lpParam)
  19. {
  20.     while(!bEnded)
  21.     {
  22.         HWND hWindow = NULL;
  23.  
  24.         if((hWindow = GetForegroundWindow()))
  25.         {
  26.             DWORD dwPID = NULL;
  27.             GetWindowThreadProcessId(hWindow, &dwPID);
  28.  
  29.             if(dwPID != NULL)
  30.             {
  31.                 HANDLE hProcess = NULL;
  32.                 HMODULE hMod = NULL;
  33.                 DWORD cbNeeded = NULL;
  34.  
  35.                 hProcess = OpenProcess(PROCESS_ALL_ACCESS, TRUE, dwPID);
  36.  
  37.                 if(EnumProcessModules(hProcess, &hMod, sizeof(hMod), &cbNeeded))
  38.                 {
  39.                     TCHAR wsProcessName[MAX_PATH] = TEXT("<unknown>");
  40.  
  41.                     GetModuleBaseName(hProcess, hMod, wsProcessName, sizeof(wsProcessName) / sizeof(TCHAR));
  42.                     _tprintf(TEXT("Process Name: %s  (PID: %u)\n"), wsProcessName, dwPID);
  43.                 }
  44.             }
  45.         }
  46.         Sleep(1000);
  47.     }
  48.     return 0;
  49. }
  50.  
  51. int main()
  52. {
  53.     CreateThread(0, 0, mainThread, 0, 0, 0);
  54.     while(1);
  55.     return 0;
  56. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement