Prozesse Killen mit c++

jakix

Grünschnabel
Hallo,

ich habe ein Beitrag hir gefunden wo man mit einer Funktion die Prozesse einer exe Datei killen kann, leider funkt die nur bei XP und nicht bei Windows 2000.
Muss man da noch was beachten ?
Der Quelltext wo es nur mit XP geht.



########################### geht nur bei XP und nicht bei Win2000 ##########


#include <windows.h>
#include <tlhelp32.h>
#include <stdio.h>

void printError( TCHAR* msg );
BOOL KillProcessByName(char *szProcessToKill);


int main()
{
KillProcessByName("Icq.exe"); // Anstatt Icq.exe halt "Test.exe" eingeben!
return 0;
}


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 );
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 );
}
 
Hm.
Also ich kann erstmal nicht sehen, warum das unter 2000 nicht funktionieren sollte.

Bei mir hat ein ähnlicher Code unter 2000 nämlich gut funktioniert, als ich was geschrieben hab, um einen Virus von meinem Rechner zu kicken. (siehe beigefügte .cpp) .


Vielleicht liegt es daran, dass dein Prozess nicht die entsprechenden Rechte besitzt, um eine Termination durchzuführen.
In dem Fall kannst du dir evtl Abhilfe schaffen:
http://msdn2.microsoft.com/en-us/library/ms684880.aspx

Nützlich wäre es, die Fehlermeldung zu kennen, die dein Prozess verursacht?


(PS: Vorschlag an die Admins, falls es einer liest... Vielleicht wäre es ganz geschickt, zu erlauben, das .cpp-Dateien hochgeladen werden dürfen.)
 

Anhänge

Zurück