Programmüberwachung Wie?



  • Hi,
    ich möchte ein Programm schreiben ,das überwacht ob ein bestimmter Prozess gestartet ist und wenn er das ist ihn wieder schliesst. Kann mir jemand erklären wie ich das am leichtesten realisieren kann.

    (C++ Builder 6)

    THX



  • Hallo,

    ich würde alle laufenden Prozesse mittels WMI auslesen und dann schauen ob der gewünschte darunter ist.
    Aus einem Versuch von mir:

    //-- Prozessliste
    bool GetProcessList(void)
    {
      HANDLE hProcessSnap;
      HANDLE hProcess;
      PROCESSENTRY32 pe32;
      PROCESS_MEMORY_COUNTERS pmc;
      DWORD dwPriorityClass;
      int i=0;
      iTotalMemoryUsed = 0;
    
      // Take a snapshot of all processes in the system.
      hProcessSnap = CreateToolhelp32Snapshot( TH32CS_SNAPPROCESS, 0 );
      if( hProcessSnap == INVALID_HANDLE_VALUE )
      {
        ShowMessage("CreateToolhelp32Snapshot of processes");
        return false;
      }
    
      // 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 ) )
      {
        ShowMessage("Process32First");        // show cause of failure
        CloseHandle( hProcessSnap );          // clean the snapshot object
        return false;
      }
    
      // Now walk the snapshot of processes, and
      // display information about each process in turn
    
      while(Process32Next(hProcessSnap,&pe32))
      {
        i++;
    
        // Retrieve the priority class.
        dwPriorityClass = 0;
        hProcess = OpenProcess( PROCESS_ALL_ACCESS, FALSE, pe32.th32ProcessID );
    
        if( hProcess == NULL )
          Form1->StringGrid1->Cells[2][i] = "NULL";
        else
        {
          dwPriorityClass = GetPriorityClass(hProcess);
          if(!dwPriorityClass)
          {
            Form1->StringGrid1->Cells[2][i] = "NULL";
          }
          CloseHandle( hProcess );
        }
    
        hProcess = OpenProcess( PROCESS_QUERY_INFORMATION |PROCESS_VM_READ,FALSE,pe32.th32ProcessID);
        GetProcessMemoryInfo(hProcess, &pmc, sizeof(pmc));
    
        //ProcessID
        Form1->StringGrid1->Cells[0][i] = pe32.th32ProcessID;
    
        [b]//Processname
          Form1->StringGrid1->Cells[1][i] = pe32.szExeFile;
    [/b]
        //ProcessMemUsage
        if ((pmc.WorkingSetSize/1024) > 10240)
        {
          Form1->StringGrid1->Cells[2][i] = IntToStr(pmc.WorkingSetSize/1024/1024) + " MB" ;
        }
        else
        {
          Form1->StringGrid1->Cells[2][i] = IntToStr(pmc.WorkingSetSize/1024) + " KB" ;
        }
        //Threads
        Form1->StringGrid1->Cells[3][i] = pe32.cntThreads;
        //ParentProcessID
        Form1->StringGrid1->Cells[4][i] = pe32.th32ParentProcessID;
        //PriorityBase
        Form1->StringGrid1->Cells[5][i] = pe32.pcPriClassBase;
        //PriorityClass
        //Form1->StringGrid1->Cells[6][i] = dwPriorityClass;
        switch (dwPriorityClass)
        {
          case 0:           Form1->StringGrid1->Cells[6][i] = "0"; break;
          case 256:         Form1->StringGrid1->Cells[6][i] = "Echtzeit"; break;
          case 128:         Form1->StringGrid1->Cells[6][i] = "Hoch"; break;
          case 32768:       Form1->StringGrid1->Cells[6][i] = "Höher"; break;
          case 32:          Form1->StringGrid1->Cells[6][i] = "Normal"; break;
          case 16384:       Form1->StringGrid1->Cells[6][i] = "Niedriger"; break;
          case 64:          Form1->StringGrid1->Cells[6][i] = "Niedrig"; break;
          default:          Form1->StringGrid1->Cells[6][i] = "0"; break;
        }
    
        //iTotalMemoryUsed += pmc.WorkingSetSize/1024;
      }
      CloseHandle(hProcessSnap);
      return true;
    }
    

    Du benötigst natürlich für den Prozessnamen nur den fett markierten Teil. Aber möglich wäre so auch eine Identifizierung über die ProcessID.

    MfG
    Tippo

    PS: Sicher sind diese Beispiele hier interessant für den Umgang mit der WMI.



  • Danke, hilft mir schon mal weiter !


Anmelden zum Antworten