Wie Hauptprogramm mit Unterprogramme schließen?
-
Hallo ich habe eine Kill Befehl der ein Prozess schließt, doch mir ist aufgefallen das nur dieser beendet wird, also nicht die Unterprogramme (Childprozesse)
Hier mein bisheriger Code:
#include <windows.h> #include <stdio.h> #include <tlhelp32.h> #include <iostream> #include "format.h" using namespace std; //void printError( TCHAR* msg ); void printError( char* msg ); BOOL KillProcessByName(char *szProcessToKill); BOOL KillProcessByName(char *szProcessToKill) { HANDLE hProcessSnap; HANDLE hProcess; PROCESSENTRY32 pe32; //DWORD dwPriorityClass; hProcessSnap = CreateToolhelp32Snapshot( TH32CS_SNAPPROCESS, 0 ); if( hProcessSnap == INVALID_HANDLE_VALUE ) { printError( "CreateToolhelp32Snapshot (of processes)" ); return( FALSE ); } pe32.dwSize = sizeof( PROCESSENTRY32 ); if( !Process32First( hProcessSnap, &pe32 ) ) { printError( "Process32First" ); CloseHandle( hProcessSnap ); return( FALSE ); } do { if(!strcmp(pe32.szExeFile,szProcessToKill)) { //printf("Prozess: %s \n",pe32.szExeFile); //printf("PID: %d \n",pe32.th32ProcessID ); textcolor(red); cout << "killed ["; textcolor(light); cout << pe32.szExeFile; textcolor(red); cout << "]"; textcolor(grey); cout << "PID: " << pe32.th32ProcessID << endl; hProcess = OpenProcess(PROCESS_TERMINATE,0, pe32.th32ProcessID); TerminateProcess(hProcess,0); CloseHandle(hProcess); } } while( Process32Next(hProcessSnap,&pe32) ); CloseHandle( hProcessSnap ); return( TRUE ); } void printError( TCHAR* msg ) { DWORD eNum; TCHAR sysMsg[256]; TCHAR* p; eNum = GetLastError( ); FormatMessage( FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS, NULL, eNum, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), // Default language sysMsg, 256, NULL ); // Trim the end of the line and terminate it with a null p = sysMsg; while( ( *p > 31 ) || ( *p == 9 ) ) ++p; do { *p-- = 0; } while( ( p >= sysMsg ) && ( ( *p == '.' ) || ( *p < 33 ) ) ); // Display the message printf( "\n WARNING: %s failed with error %d (%s)", msg, eNum, sysMsg ); } int main(int argc, char* argv[]) { if(argc > 1) { textcolor(grey); KillProcessByName(argv[1]); } else { textcolor(light); cout << "Verwendung: kill [Prozess]" << endl << endl; textcolor(yellow); cout << "Beispiel:" << endl; cout << "---------" << endl; textcolor(white); cout << "kill winamp.exe" << endl; } return 0; }Was muss am Code geändert werden das noch die Unterprogramme geschloßen werden?
-
Brauchst eine zweite Process32First / Next - Schleife in der jeder Prozess beendet wird für den gilt : "pr32.th32ParentProcessID == HauptprogrammID".
-
Also so wie ich es verstanden habe, muss ich alle Childprozesse beenden wenn diese mit dem hauptprogramm verknüpft sind oder?
Hab hier mal die do-Schleife aus dem Code verändert, aber irgendwie klappt es nicht wahrscheinlich ist die Bedingung falsch:
do { if(!strcmp(pe32.szExeFile,szProcessToKill)) { do { //Beende hier Childprozesse }while(pe32.th32ParentProcessID == pe32.th32ProcessID); textcolor(red); cout << "killed ["; textcolor(light); cout << pe32.szExeFile; textcolor(red); cout << "]"; textcolor(grey); cout << "PID: " << pe32.th32ProcessID << endl; hProcess = OpenProcess(PROCESS_TERMINATE,0, pe32.th32ProcessID); TerminateProcess(hProcess,0); CloseHandle(hProcess); } } while( Process32Next(hProcessSnap,&pe32) );
-
Mit der ersten Schleife suchst Du nur die ID des Hauptprogrammes.
Mit der zweiten Schleife beendest Du alle Programme wo "pe32.th32ParentProcessID == HauptprogrammID" ist.DWORD HauptprogrammID; CreateToolhelp32Snapshot (...); // erste Schleife Process32First (...); do { if(!strcmp(pe32.szExeFile,szProcessToKill)) { HauptprogrammID = pe32.th32ProcessID; break; } } while (Process32Next (...)); CloseHandle (...); CreateToolhelp32Snapshot (...); // zweite Schleife Process32First (...); do { if (pe32.th32ParentProcessID == HauptprogrammID) { // Prozess beenden } } while (Process32Next (...)); CloseHandle (...);
-
Habe es nun so gemacht wie du gesagt hast, doch es funktioniert nicht

Wenn ich die Prozesse ausgebe die geschlossen werden sollen, so werden alle Prozesse aufgelistet?!?!?BOOL KillProcessByName(char *szProcessToKill) { DWORD HauptprogrammID; HANDLE hProcessSnap; HANDLE hProcess; PROCESSENTRY32 pe32; //DWORD dwPriorityClass; hProcessSnap = CreateToolhelp32Snapshot( TH32CS_SNAPPROCESS, 0 ); if( hProcessSnap == INVALID_HANDLE_VALUE ) { printError( "CreateToolhelp32Snapshot (of processes)" ); return( FALSE ); } pe32.dwSize = sizeof( PROCESSENTRY32 ); if( !Process32First( hProcessSnap, &pe32 ) ) { printError( "Process32First" ); CloseHandle( hProcessSnap ); return( FALSE ); } do { if(!strcmp(pe32.szExeFile,szProcessToKill)) { HauptprogrammID = pe32.th32ProcessID; break; } } while( Process32Next(hProcessSnap,&pe32) ); CloseHandle( hProcessSnap ); hProcessSnap = CreateToolhelp32Snapshot( TH32CS_SNAPPROCESS, 0 ); if( hProcessSnap == INVALID_HANDLE_VALUE ) { printError( "CreateToolhelp32Snapshot (of processes)" ); return( FALSE ); } pe32.dwSize = sizeof( PROCESSENTRY32 ); if( !Process32First( hProcessSnap, &pe32 ) ) { printError( "Process32First" ); CloseHandle( hProcessSnap ); return( FALSE ); } do { if(pe32.th32ParentProcessID = HauptprogrammID) { textcolor(red); cout << "killed ["; textcolor(light); cout << pe32.szExeFile; textcolor(red); cout << "]"; textcolor(grey); cout << "PID: " << pe32.th32ProcessID << endl; hProcess = OpenProcess(PROCESS_TERMINATE,0, pe32.th32ProcessID); TerminateProcess(hProcess,0); CloseHandle(hProcess); } } while( Process32Next(hProcessSnap,&pe32) ); CloseHandle( hProcessSnap ); return( TRUE ); }
-
Kann mir jemand bitte weiterhelfen??
-
kernel64 schrieb:
Habe es nun so gemacht wie du gesagt hast, ...
*no comment*
... // if(pe32.th32ParentProcessID = HauptprogrammID) if(pe32.th32ParentProcessID == HauptprogrammID) ...
-
// if(pe32.th32ParentProcessID = HauptprogrammID)
if(pe32.th32ParentProcessID == HauptprogrammID)Jepp wahr mein Fehler

Habe meine Funktion so aufgebaut, das man angeben kann ob man ein Prozess mit oder ohne Unterprozesse geschlossen werden soll (killChildProcess)
Ich möchte noch eine Änderung vornehmen, wenn mehrere Prozesse z.B. Hallo.exe gleichzeitig laufen, dann kann ich diese mit dem Befehl kill Hallo.exe alle Prozesse gleichzeitig beenden, doch es funktioniert nicht mit der neuen Funktion.
Nochmal zum Verständnis, alle Prozesse mit dem gleichen Namen Test.exe haben Unterprogramme geöffnet, ich möchte nun alle Test.exe mit Unterprogramme schließen wie bei der normalen Methode, was muss man bei der else Teil ändern?BOOL KillProcessByName(char *szProcessToKill, bool killChildProcess) { DWORD HauptprogrammID = NULL; HANDLE hProcessSnap = NULL; HANDLE hProcess = NULL; PROCESSENTRY32 pe32; //DWORD dwPriorityClass; hProcessSnap = CreateToolhelp32Snapshot( TH32CS_SNAPPROCESS, 0 ); if( hProcessSnap == INVALID_HANDLE_VALUE ) { printError( "CreateToolhelp32Snapshot (of processes)" ); return( FALSE ); } pe32.dwSize = sizeof( PROCESSENTRY32 ); if( !Process32First( hProcessSnap, &pe32 ) ) { printError( "Process32First" ); CloseHandle( hProcessSnap ); return( FALSE ); } //Beende einen Prozess if(killChildProcess == false) { do { if(!strcmp(pe32.szExeFile,szProcessToKill)) { textcolor(red); cout << "killed ["; textcolor(light); cout << pe32.szExeFile; textcolor(red); cout << "]"; textcolor(grey); cout << "PID: " << pe32.th32ProcessID << endl; hProcess = OpenProcess(PROCESS_TERMINATE,0, pe32.th32ProcessID); TerminateProcess(hProcess,0); CloseHandle(hProcess); } } while( Process32Next(hProcessSnap,&pe32) ); CloseHandle( hProcessSnap ); return( TRUE ); } //Beende Hauptprozess mit Childprozesse else { do { if(!strcmp(pe32.szExeFile,szProcessToKill)) { HauptprogrammID = pe32.th32ProcessID; break; } } while( Process32Next(hProcessSnap,&pe32) ); CloseHandle( hProcessSnap ); hProcessSnap = CreateToolhelp32Snapshot( TH32CS_SNAPPROCESS, 0 ); if( hProcessSnap == INVALID_HANDLE_VALUE ) { printError( "CreateToolhelp32Snapshot (of processes)" ); return( FALSE ); } pe32.dwSize = sizeof( PROCESSENTRY32 ); if( !Process32First( hProcessSnap, &pe32 ) ) { printError( "Process32First" ); CloseHandle( hProcessSnap ); return( FALSE ); } do { if(pe32.th32ParentProcessID == HauptprogrammID) { textcolor(red); cout << "killed ["; textcolor(light); cout << pe32.szExeFile; textcolor(red); cout << "]"; textcolor(grey); cout << "PID: " << pe32.th32ParentProcessID << endl; hProcess = OpenProcess(PROCESS_TERMINATE,0, pe32.th32ProcessID); TerminateProcess(hProcess,0); CloseHandle(hProcess); } } while( Process32Next(hProcessSnap,&pe32) ); CloseHandle( hProcessSnap ); return( TRUE ); } }
-
kernel64 schrieb:
... alle Prozesse mit dem gleichen Namen Test.exe haben Unterprogramme geöffnet, ich möchte nun alle Test.exe mit Unterprogramme schließen ...
Dann besser zwei Funktionen :
Erste Funktion sucht nach "test.exe" und liefert ID zurück falls eine gefunden.
Zweite Funktion nimmt ID und "Child-Flag" und beendet alle Child-Prozesse und am Ende ID.Dann erste und zweite Funktion sooft aufrufen wie erste Funktion eine ID zurückgibt.
-
Hab es nun so gemacht wie du gesagt hast:
BOOL KillChildProcess(DWORD HauptprogrammID) { HANDLE hProcessSnap = NULL; HANDLE hProcess = NULL; PROCESSENTRY32 pe32; hProcessSnap = CreateToolhelp32Snapshot( TH32CS_SNAPPROCESS, 0 ); if( hProcessSnap == INVALID_HANDLE_VALUE ) { printError( "CreateToolhelp32Snapshot (of processes)" ); return( FALSE ); } pe32.dwSize = sizeof( PROCESSENTRY32 ); if( !Process32First( hProcessSnap, &pe32 ) ) { printError( "Process32First" ); CloseHandle( hProcessSnap ); return( FALSE ); } do { if(pe32.th32ParentProcessID == HauptprogrammID) { hProcess = OpenProcess(PROCESS_TERMINATE,0, pe32.th32ProcessID); TerminateProcess(hProcess,0); CloseHandle(hProcess); } } while( Process32Next(hProcessSnap,&pe32) ); CloseHandle( hProcessSnap ); return TRUE; } BOOL KillProcessTree(char *szProcessToKill) { HANDLE hProcessSnap = NULL; HANDLE hProcess = NULL; PROCESSENTRY32 pe32; //DWORD dwPriorityClass; hProcessSnap = CreateToolhelp32Snapshot( TH32CS_SNAPPROCESS, 0 ); if( hProcessSnap == INVALID_HANDLE_VALUE ) { printError( "CreateToolhelp32Snapshot (of processes)" ); return( FALSE ); } pe32.dwSize = sizeof( PROCESSENTRY32 ); if( !Process32First( hProcessSnap, &pe32 ) ) { printError( "Process32First" ); CloseHandle( hProcessSnap ); return( FALSE ); } do { //Suche nach Prozessnamen if(!strcmp(pe32.szExeFile,szProcessToKill)) { //Beende Child Prozess KillChildProcess(pe32.th32ProcessID); //Beende Hauptprozess hProcess = OpenProcess(PROCESS_TERMINATE,0, pe32.th32ProcessID); if(TerminateProcess(hProcess,0)) return TRUE; CloseHandle(hProcess); cout << endl; } } while( Process32Next(hProcessSnap,&pe32) ); CloseHandle( hProcessSnap ); return FALSE; }Ich habe mehrere Tests durchgeführt und es hat geklappt, falls euch doch was aufallen sollte dann bescheid sagen

Hatte noch vor den Prozessbaum in der Konsole auch als Baum darzustellen, so z.B.:
+Main.exe |-Hallo.exe |-Test.exe |-XYZ.exeWär sowas möglich da doch zuerst der unterste Prozess beendet wird?!?!?
-
kernel64 schrieb:
falls euch doch was aufallen sollte dann bescheid sagen
Falls ein Child auch noch Child-Prozesse hat, gibt es ein Problem. Aber dann nur das Child als "ProcessTree" betrachten.

kernel64 schrieb:
Wär sowas möglich da doch zuerst der unterste Prozess beendet wird?!?!?
Gib den Prozessnamen an der Stelle aus, wo er gefunden wird und nicht dort, wo er beendet wird.