1  /* EnumWin 1.1 - 2011-08-23
  2  
  3  Update:
  4    http://myc01.free.fr/enumwin/
  5  
  6  License:
  7    Public Domain Dedication (CC0 1.0)
  8    http://creativecommons.org/publicdomain/zero/1.0/
  9  
 10  Usage :
 11    enumwin.exe ["window name"] [/s]
 12    - Without parameters, returns a list of open windows in the current session.
 13    - With the "window name" followed by the name refers "****" found.
 14    - With the parameters "window name" /s : silent mode.
 15    Returns %errorlevel% = 1 if the name is found, otherwise 0
 16  
 17  Examples :
 18    enumwin.exe
 19    enumwin.exe "Untitled - Notepad"
 20    enumwin.exe "Untitled - Notepad" /s
 21  */
 22  
 23  #include <windows.h>
 24  #include <psapi.h>  //link: MinGW\lib\libpsapi.a (psapi.dll)
 25  #include <stdio.h>
 26  
 27  #define MAX_STR (MAX_PATH + 1)
 28  
 29  char *cTest    = NULL;
 30  BOOL bFindList = FALSE;
 31  
 32  typedef DWORD (WINAPI *QUERYFULLPROCESSIMAGENAME) (HANDLE, DWORD, LPTSTR, PDWORD);
 33  
 34  DWORD GetWindowPath (HWND hWnd, char *szEXEName, DWORD nExeName)
 35  {
 36      if (!szEXEName || !nExeName)
 37          return 0;
 38  
 39      DWORD dwProcessID = 0;
 40      HANDLE hProcess = NULL;
 41      HINSTANCE hKernel32 = NULL;
 42      QUERYFULLPROCESSIMAGENAME pQueryFullProcessImageName = NULL;
 43  
 44      // Convert from Window to Process ID
 45      GetWindowThreadProcessId (hWnd, &dwProcessID);
 46  
 47      // Get a handle to the process from the Process ID
 48      if (!(hProcess = OpenProcess (PROCESS_QUERY_INFORMATION | PROCESS_VM_READ, FALSE, dwProcessID)))
 49          return 0;
 50  
 51      // Get the process name for Windows Vista/Seven
 52      if ((hKernel32 = LoadLibrary ("kernel32.dll")))
 53      {
 54          if ((pQueryFullProcessImageName = (QUERYFULLPROCESSIMAGENAME) GetProcAddress (hKernel32, "QueryFullProcessImageNameA")))
 55              pQueryFullProcessImageName (hProcess, 0, szEXEName, &nExeName);
 56          else
 57              // Get the process name for Windows 2000/XP
 58              GetModuleFileNameEx (hProcess, NULL, szEXEName, nExeName);
 59          FreeLibrary (hKernel32);
 60      }
 61      CloseHandle (hProcess);
 62      return dwProcessID;
 63  }
 64  
 65  BOOL CALLBACK EnumWindowsProc (HWND hWnd, long lParam)
 66  {
 67      char buff[MAX_STR], BUFF[MAX_STR], path[MAX_STR];
 68  
 69      if (IsWindowVisible (hWnd))
 70      {
 71          GetWindowText (hWnd, buff, MAX_STR);
 72          DWORD PID = GetWindowPath (hWnd, path, MAX_STR);
 73          if (cTest)
 74          {
 75              if (strstr (_strupr (strcpy (BUFF, buff)), cTest) == BUFF)
 76              {
 77                  if (lParam) printf ("**** ");
 78                  bFindList = TRUE;
 79              }
 80          }
 81          if (lParam && buff[0])
 82          {
 83              CharToOemBuff (buff, buff, MAX_STR);
 84              CharToOemBuff (path, path, MAX_STR);
 85              printf ("%s\n  PID:%ld  \"%s\"\n", buff, PID, path);
 86          }
 87      }
 88      return TRUE;
 89  }
 90  
 91  int main (int argc, char **argv)
 92  {
 93      BOOL bViewInfo = TRUE;
 94      if (argc > 1)
 95          if (strcmpi (argv[1], "/s"))
 96              cTest = _strupr (argv[1]);
 97  
 98      if (argc > 2)
 99          if (!strcmpi (argv[2], "/s"))
100              bViewInfo = FALSE;
101  
102      EnumWindows (EnumWindowsProc, bViewInfo);
103  
104      return bFindList;
105  }