MFC + Konsole + Fenster finden



  • Hallo

    Wollte fragen ob es möglich ist die MFC in einem Konsolenprogramm zu verwenden und falls ja wie man es realisieren könnte festzustellen ob ein Fenster geöffnet ist oder nicht. Irgendwie mit FindWindow(...) vermut ich mal oder ?

    Danke schon mal.



  • Sag doch mal was Du genau machen willst...



  • Es geht darum, ein Konsolenprogramm zu schreiben, das in regelmäßigen Zeitabständen nach einen bestimmten Programm ausschau hält und sich meldet wenn das andere Programm beendet wurde.



  • Wenn Du das Programm selber aus der Console gestartet hast ist das relativ einfach:
    http://windowssdk.msdn.microsoft.com/library/en-us/DllProc/base/creating_processes.asp

    Wenn nicht, dann kannst Du somit Abfragen ob der Process noch existiert bzw. kannst Dir ein Prozess-Handle holen und mit WaitForSingleObject darauf warten, bis er sich beendet...:

    #include <windows.h>
    #include <stdio.h>
    #include <tchar.h>
    #include <tlhelp32.h>
    
    DWORD GetProcessIdFromName(LPCTSTR szProcessName)
    {
      HANDLE hProcessSnap;
      HANDLE hProcess;
      PROCESSENTRY32 pe32;
      DWORD dwPriorityClass;
    
      // Take a snapshot of all processes in the system.
      hProcessSnap = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0 );
      if( hProcessSnap == INVALID_HANDLE_VALUE )
        return 0;
    
      // Set the size of the structure before using it.
      pe32.dwSize = sizeof(PROCESSENTRY32);
    
      // Retrieve information about the first process,
      // and exit if unsuccessful
      if( !Process32First(hProcessSnap, &pe32 ) )
      {
        DWORD gle = GetLastError();
        CloseHandle( hProcessSnap );     // Must clean up the snapshot object!
        SetLastError(gle);
        return 0;
      }
    
      // Now walk the snapshot of processes, and
      // display information about each process in turn
      do
      {
        if (_tcsicmp(pe32.szExeFile, szProcessName) == 0)
        {
          CloseHandle( hProcessSnap );
          SetLastError(0);
          return pe32.th32ProcessID;
        }
    
        //hProcess = OpenProcess( PROCESS_ALL_ACCESS, FALSE, pe32.th32ProcessID );
      } while( Process32Next( hProcessSnap, &pe32 ) );
    
      CloseHandle( hProcessSnap );
      return 0;
    }
    
    int _tmain()
    {
      DWORD dwPid = GetProcessIdFromName(_T("explorer.exe"));
      if (dwPid != 0)
      {
        HANDLE hProcess = OpenProcess(PROCESS_ALL_ACCESS, FALSE, dwPid);
        if (hProcess != NULL)
        {
          _tprintf(_T("Warte bis der Prozess beendet wird..."));
          WaitForSingleObject(hProcess, INFINITE);
        }
      }
      _tprintf(_T("\nProzess ist beendet."));
    }
    

Anmelden zum Antworten