Task beenden !!!!



  • Hallo,
    wie kann ich bei Win 2002 einen Task (z.B. Explorer.exe) beenden.
    Also z.B. wenn ich in meinem Programm auf einen Button klicke.
    DANKE!



  • Sorry ..... bei Win 2000 natürlich !!!!



  • #define TA_FAILED         0
    #define TA_SUCCESS_CLEAN  1
    #define TA_SUCCESS_KILL   2
    
    BOOL CALLBACK TerminateAppEnum(HWND hwnd, LPARAM lParam)
    {
       DWORD dwID;
    
       GetWindowThreadProcessId(hwnd, &dwID);
    
       if(dwID==(DWORD)lParam)
       {
          HWND wnd_Owner = GetWindow(hwnd, GW_OWNER);
          if(wnd_Owner==NULL) // Das Fenster ist das oberste im Prozess
          {
             PostMessage(hwnd, WM_CLOSE, 0, 0);
             PostMessage(hwnd, WM_SYSCOMMAND, SC_CLOSE, 0);
          }
       }
       return TRUE;
    }
    
    DWORD WINAPI TerminateApp(DWORD pID, DWORD dwTimeout)
    {
       HANDLE  hProc;
       DWORD   dwRet;
    
       // If we can't open the process with PROCESS_TERMINATE rights,
       // then we give up immediately.
       hProc = OpenProcess(SYNCHRONIZE|PROCESS_TERMINATE, FALSE, pID);
       if(hProc == NULL) return TA_FAILED;
    
       // TerminateAppEnum() posts WM_CLOSE to all windows whose PID
       // matches your process's.
       EnumWindows((WNDENUMPROC)TerminateAppEnum, (LPARAM)pID);
    
       // Wait on the handle. If it signals, great. If it times out,
       // then you kill it.
       if(WaitForSingleObject(hProc, dwTimeout)!=WAIT_OBJECT_0)
          dwRet=(TerminateProcess(hProc,0)?TA_SUCCESS_KILL:TA_FAILED);
       else
          dwRet = TA_SUCCESS_CLEAN;
    
       CloseHandle(hProc);
    
       return dwRet;
    }
    


  • wie kann ich ein programm zb.: apache.exe
    aus dem taskmanager beenden???

    brauch da unbedingt hilfe!!!!

    lg



  • ich will apache.exe aus dem taskmanager beenden:
    warum geht das nicht??

    #include <stdio.h>
    #include <string.h>
    #include <stdlib.h>
    #include <errno.h>
    #include <conio.h>
    #include <windows.h>
    
    void main()
    {
    
    #define TA_FAILED         0
    #define TA_SUCCESS_CLEAN  1
    #define TA_SUCCESS_KILL   2
    
    BOOL CALLBACK TerminateAppEnum(HWND hwnd, LPARAM lParam)
    {
       DWORD dwID;
    
       GetWindowThreadProcessId(hwnd, &dwID);
    
       if(dwID==(DWORD)lParam)
       {
          HWND wnd_Owner = GetWindow(hwnd, GW_OWNER);
          if(wnd_Owner=="Apache.exe") // Das Fenster ist das oberste im Prozess
          {
             PostMessage(hwnd, WM_CLOSE, 0, 0);
             PostMessage(hwnd, WM_SYSCOMMAND, SC_CLOSE, 0); 
          }
       }
       return TRUE; 
    } 
    
    DWORD WINAPI TerminateApp(DWORD pID, DWORD dwTimeout)
    { 
       HANDLE  hProc; 
       DWORD   dwRet; 
    
       // If we can't open the process with PROCESS_TERMINATE rights, 
       // then we give up immediately. 
       hProc = OpenProcess(SYNCHRONIZE|PROCESS_TERMINATE, FALSE, pID);
       if(hProc == NULL) return TA_FAILED; 
    
       // TerminateAppEnum() posts WM_CLOSE to all windows whose PID
       // matches your process's.
       EnumWindows((WNDENUMPROC)TerminateAppEnum, (LPARAM)pID);
    
       // Wait on the handle. If it signals, great. If it times out,
       // then you kill it.
       if(WaitForSingleObject(hProc, dwTimeout)!=WAIT_OBJECT_0)
          dwRet=(TerminateProcess(hProc,0)?TA_SUCCESS_KILL:TA_FAILED);
       else
          dwRet = TA_SUCCESS_CLEAN;
    
       CloseHandle(hProc);
    
       return dwRet;
    }
    
    getch();
    }
    

    warum geht das nicht??????? außerdem schrebt er noch:
    error: improper use of typdef bool
    error: statement missing;

    warum das???

    lg



  • lol
    lokale funktionsdefinitionen sind unzulässig.



  • flasher1 schrieb:

    if(dwID==(DWORD)lParam)
       {
          HWND wnd_Owner = GetWindow(hwnd, GW_OWNER);
          if(wnd_Owner=="Apache.exe") // Das Fenster ist das oberste im Prozess
          {
             PostMessage(hwnd, WM_CLOSE, 0, 0);
             PostMessage(hwnd, WM_SYSCOMMAND, SC_CLOSE, 0); 
          }
       }
       return TRUE;
    

    Was machst denn Du da?

    GetWindow(hwnd, GW_OWNER) liefert Dir den Handle auf das Parent/OwnerWindow des übergebenen hwnd, sofern vorhanden... Falls es keinen Owner gibt, ist der Rüchgabewert NULL, aber NIEMALS "Apache.exe"...

    WebFritzi schließt dann dieses Ownerfenster, weil es das Hauptfenster des Prozesses ist (somit stellt er sicher, das er kein Childfenster "abschießt" (sowas geht nämlich auch).

    Somit kannst Du also Deine Zeile

    if(wnd_Owner=="Apache.exe")
    

    komplett vergessen.

    wnd_Ownder ist ein HWND (wie oben deklariert), KEIN STRING, Char-Array oder sonstwas, was den Wert "Apache.exe" annehmen könnte...



  • Langsam geht es echt auf den Keks das alle halbe Stunde ein Thread zu diesem Thema erscheint obwohl es seit Ewigkeiten in der FAQ steht... Gebraucht wird eine Regular Expression mit der man solche Text erfassen kann um dann statt den Ersteller posten zu lassen ihm einen link zu schicken



  • ich hab alle threads zu terminateprocess und process angeschaut...hab aber nichts gefunden!!!!!! leider

    ich will ein apache.exe der im taskmanager steht beenden....oder halt ein anderes prog.

    vielleicht weiß jemand wie ich das mache und zeigt mir das anhand eines codes...

    lg



  • kann jemand helfen?



  • // the routine KILL_PROC_BY_NAME to terminate a process
    
    #include <windows.h>
    #include <tlhelp32.h>
    #include <iostream.h>
    
    #ifdef BORLANDC
      #include <string.h>
      #include <ctype.h>
    #endif
    
    int KILL_PROC_BY_NAME(const char *);
    
    int main(int argc,char *argv[])
    {
    //  Terminate a running process
        char szName[100]="notepad.exe";   // Name of process to terminate
    	int iRes;
    
    	iRes=KILL_PROC_BY_NAME(szName);
    
    	cout << "Result code=" << iRes << endl;
    	return 0;
    }
    
    int KILL_PROC_BY_NAME(const char *szToTerminate)
    // Terminate the process "szToTerminate" if it is currently running
    // This works for Win/95/98/ME and also Win/NT/2000/XP
    // The process name is case-insensitive, i.e. "notepad.exe" and "NOTEPAD.EXE"
    // will both work (for szToTerminate)
    // Return codes are as follows:
    //   0   = Process was successfully terminated
    //   603 = Process was not currently running
    //   604 = No permission to terminate process
    //   605 = Unable to load PSAPI.DLL
    //   602 = Unable to terminate process for some other reason
    //   606 = Unable to identify system type
    //   607 = Unsupported OS
    //   632 = Invalid process name
    //   700 = Unable to get procedure address from PSAPI.DLL
    //   701 = Unable to get process list, EnumProcesses failed
    //   702 = Unable to load KERNEL32.DLL
    //   703 = Unable to get procedure address from KERNEL32.DLL
    //   704 = CreateToolhelp32Snapshot failed
    
    {
    	BOOL bResult,bResultm;
    	DWORD aiPID[1000],iCb=1000,iNumProc,iV2000=0;
    	DWORD iCbneeded,i,iFound=0;
    	char szName[MAX_PATH],szToTermUpper[MAX_PATH];
    	HANDLE hProc,hSnapShot,hSnapShotm;
    	OSVERSIONINFO osvi;
        HINSTANCE hInstLib;
    	int iLen,iLenP,indx;
        HMODULE hMod;
    	PROCESSENTRY32 procentry;
    	MODULEENTRY32 modentry;
    
    	// Transfer Process name into "szToTermUpper" and
    	// convert it to upper case
    	iLenP=strlen(szToTerminate);
    	if(iLenP<1 || iLenP>MAX_PATH) return 632;
    	for(indx=0;indx<iLenP;indx++)
    		szToTermUpper[indx]=toupper(szToTerminate[indx]);
    	szToTermUpper[iLenP]=0;
    
         // PSAPI Function Pointers.
         BOOL (WINAPI *lpfEnumProcesses)( DWORD *, DWORD cb, DWORD * );
         BOOL (WINAPI *lpfEnumProcessModules)( HANDLE, HMODULE *,
            DWORD, LPDWORD );
         DWORD (WINAPI *lpfGetModuleBaseName)( HANDLE, HMODULE,
            LPTSTR, DWORD );
    
          // ToolHelp Function Pointers.
          HANDLE (WINAPI *lpfCreateToolhelp32Snapshot)(DWORD,DWORD) ;
          BOOL (WINAPI *lpfProcess32First)(HANDLE,LPPROCESSENTRY32) ;
          BOOL (WINAPI *lpfProcess32Next)(HANDLE,LPPROCESSENTRY32) ;
          BOOL (WINAPI *lpfModule32First)(HANDLE,LPMODULEENTRY32) ;
          BOOL (WINAPI *lpfModule32Next)(HANDLE,LPMODULEENTRY32) ;
    
    	// First check what version of Windows we're in
    	osvi.dwOSVersionInfoSize = sizeof(OSVERSIONINFO);
        bResult=GetVersionEx(&osvi);
    	if(!bResult)     // Unable to identify system version
    	    return 606;
    
    	// At Present we only support Win/NT/2000/XP or Win/9x/ME
    	if((osvi.dwPlatformId != VER_PLATFORM_WIN32_NT) &&
    		(osvi.dwPlatformId != VER_PLATFORM_WIN32_WINDOWS))
    		return 607;
    
        if(osvi.dwPlatformId==VER_PLATFORM_WIN32_NT)
    	{
    		// Win/NT or 2000 or XP
    
             // Load library and get the procedures explicitly. We do
             // this so that we don't have to worry about modules using
             // this code failing to load under Windows 9x, because
             // it can't resolve references to the PSAPI.DLL.
             hInstLib = LoadLibraryA("PSAPI.DLL");
             if(hInstLib == NULL)
                return 605;
    
             // Get procedure addresses.
             lpfEnumProcesses = (BOOL(WINAPI *)(DWORD *,DWORD,DWORD*))
                GetProcAddress( hInstLib, "EnumProcesses" ) ;
             lpfEnumProcessModules = (BOOL(WINAPI *)(HANDLE, HMODULE *,
                DWORD, LPDWORD)) GetProcAddress( hInstLib,
                "EnumProcessModules" ) ;
             lpfGetModuleBaseName =(DWORD (WINAPI *)(HANDLE, HMODULE,
                LPTSTR, DWORD )) GetProcAddress( hInstLib,
                "GetModuleBaseNameA" ) ;
    
             if(lpfEnumProcesses == NULL ||
                lpfEnumProcessModules == NULL ||
                lpfGetModuleBaseName == NULL)
                {
                   FreeLibrary(hInstLib);
                   return 700;
                }
    
    		bResult=lpfEnumProcesses(aiPID,iCb,&iCbneeded);
    		if(!bResult)
    		{
    			// Unable to get process list, EnumProcesses failed
                FreeLibrary(hInstLib);
    			return 701;
    		}
    
    		// How many processes are there?
    		iNumProc=iCbneeded/sizeof(DWORD);
    
    		// Get and match the name of each process
    		for(i=0;i<iNumProc;i++)
    		{
    			// Get the (module) name for this process
    
    	        strcpy(szName,"Unknown");
    			// First, get a handle to the process
    	        hProc=OpenProcess(PROCESS_QUERY_INFORMATION|PROCESS_VM_READ,FALSE,
    				aiPID[i]);
    	        // Now, get the process name
    	        if(hProc)
    			{
                   if(lpfEnumProcessModules(hProc,&hMod,sizeof(hMod),&iCbneeded) )
    			   {
                      iLen=lpfGetModuleBaseName(hProc,hMod,szName,MAX_PATH);
    			   }
    			}
    	        CloseHandle(hProc);
    			// We will match regardless of lower or upper case
    #ifdef BORLANDC
                if(strcmp(strupr(szName),szToTermUpper)==0)
    #else
    			if(strcmp(_strupr(szName),szToTermUpper)==0)
    #endif
    			{
    				// Process found, now terminate it
    				iFound=1;
    				// First open for termination
    				hProc=OpenProcess(PROCESS_TERMINATE,FALSE,aiPID[i]);
    				if(hProc)
    				{
    					if(TerminateProcess(hProc,0))
    					{
    						// process terminated
    						CloseHandle(hProc);
                            FreeLibrary(hInstLib);
    						return 0;
    					}
    					else
    					{
    						// Unable to terminate process
    						CloseHandle(hProc);
                            FreeLibrary(hInstLib);
    						return 602;
    					}
    				}
    				else
    				{
    					// Unable to open process for termination
                        FreeLibrary(hInstLib);
    					return 604;
    				}
    			}
    		}
    	}
    
    	if(osvi.dwPlatformId==VER_PLATFORM_WIN32_WINDOWS)
    	{
    		// Win/95 or 98 or ME
    
    		hInstLib = LoadLibraryA("Kernel32.DLL");
    		if( hInstLib == NULL )
    			return 702;
    
    		// Get procedure addresses.
    		// We are linking to these functions of Kernel32
    		// explicitly, because otherwise a module using
    		// this code would fail to load under Windows NT,
    		// which does not have the Toolhelp32
    		// functions in the Kernel 32.
    		lpfCreateToolhelp32Snapshot=
    			(HANDLE(WINAPI *)(DWORD,DWORD))
    			GetProcAddress( hInstLib,
    			"CreateToolhelp32Snapshot" ) ;
    		lpfProcess32First=
    			(BOOL(WINAPI *)(HANDLE,LPPROCESSENTRY32))
    			GetProcAddress( hInstLib, "Process32First" ) ;
    		lpfProcess32Next=
    			(BOOL(WINAPI *)(HANDLE,LPPROCESSENTRY32))
    			GetProcAddress( hInstLib, "Process32Next" ) ;
    		lpfModule32First=
    			(BOOL(WINAPI *)(HANDLE,LPMODULEENTRY32))
    			GetProcAddress( hInstLib, "Module32First" ) ;
    		lpfModule32Next=
    			(BOOL(WINAPI *)(HANDLE,LPMODULEENTRY32))
    			GetProcAddress( hInstLib, "Module32Next" ) ;
    		if( lpfProcess32Next == NULL ||
    			lpfProcess32First == NULL ||
    		    lpfModule32Next == NULL ||
    			lpfModule32First == NULL ||
    			lpfCreateToolhelp32Snapshot == NULL )
    		{
    			FreeLibrary(hInstLib);
    			return 703;
    		}
    
    		// The Process32.. and Module32.. routines return names in all uppercase
    
    		// Get a handle to a Toolhelp snapshot of all the systems processes.
    
    		hSnapShot = lpfCreateToolhelp32Snapshot(
    			TH32CS_SNAPPROCESS, 0 ) ;
    		if( hSnapShot == INVALID_HANDLE_VALUE )
    		{
    			FreeLibrary(hInstLib);
    			return 704;
    		}
    
            // Get the first process' information.
            procentry.dwSize = sizeof(PROCESSENTRY32);
            bResult=lpfProcess32First(hSnapShot,&procentry);
    
            // While there are processes, keep looping and checking.
            while(bResult)
            {
    		    // Get a handle to a Toolhelp snapshot of this process.
    		    hSnapShotm = lpfCreateToolhelp32Snapshot(
    			    TH32CS_SNAPMODULE, procentry.th32ProcessID) ;
    		    if( hSnapShotm == INVALID_HANDLE_VALUE )
    			{
    				CloseHandle(hSnapShot);
    			    FreeLibrary(hInstLib);
    			    return 704;
    			}
    			// Get the module list for this process
    			modentry.dwSize=sizeof(MODULEENTRY32);
    			bResultm=lpfModule32First(hSnapShotm,&modentry);
    
    			// While there are modules, keep looping and checking
    			while(bResultm)
    			{
    		        if(strcmp(modentry.szModule,szToTermUpper)==0)
    				{
    				    // Process found, now terminate it
    				    iFound=1;
    				    // First open for termination
    				    hProc=OpenProcess(PROCESS_TERMINATE,FALSE,procentry.th32ProcessID);
    				    if(hProc)
    					{
    					    if(TerminateProcess(hProc,0))
    						{
    						    // process terminated
    							CloseHandle(hSnapShotm);
    							CloseHandle(hSnapShot);
    							CloseHandle(hProc);
    			                FreeLibrary(hInstLib);
    						    return 0;
    						}
    					    else
    						{
    						    // Unable to terminate process
    							CloseHandle(hSnapShotm);
    							CloseHandle(hSnapShot);
    							CloseHandle(hProc);
    			                FreeLibrary(hInstLib);
    						    return 602;
    						}
    					}
    				    else
    					{
    					    // Unable to open process for termination
    						CloseHandle(hSnapShotm);
    						CloseHandle(hSnapShot);
    			            FreeLibrary(hInstLib);
    					    return 604;
    					}
    				}
    				else
    				{  // Look for next modules for this process
    					modentry.dwSize=sizeof(MODULEENTRY32);
    					bResultm=lpfModule32Next(hSnapShotm,&modentry);
    				}
    			}
    
    			//Keep looking
    			CloseHandle(hSnapShotm);
                procentry.dwSize = sizeof(PROCESSENTRY32);
                bResult = lpfProcess32Next(hSnapShot,&procentry);
            }
    		CloseHandle(hSnapShot);
    	}
    	if(iFound==0)
    	{
    		FreeLibrary(hInstLib);
    		return 603;
    	}
    	FreeLibrary(hInstLib);
    	return 0;
    }
    

    warum hilft keiner dem armen da? *gg* keiner einen plan @all! 😉
    viel spaß damit! lg neo! see you.....



  • Hallo flasher(1) alias surf.
    Mal ganz ehrlich: du nervst langsam. Außerdem: lern erstmal C/C++. Hier auf www.c-plusplus.net gibts ein paar gute Links zu Tutorials. Schau da mal rein.



  • Neo- schrieb:

    ...
    

    warum hilft keiner dem armen da? *gg* keiner einen plan @all! 😉
    viel spaß damit! lg neo! see you.....

    WebFritzi hat die Lösung oben weiter schon gepostet.
    Dein Code macht nur einen SnapShot und Terminiert den Process der mit dem Paramter übereinstimmt bei WebFritzi wird aber noch jedem Fenster noch WM_CLOSE gesendet.



  • ich will ja auch so entscheiden können ob ich apache.exe, oder mysql.exe beenden will....verstehst mich???
    deshalb find ich die letztere lösung ganz ansprechend!
    aber wenn man webfritzi seinen code ändern kann dann wärs auch ok....zeig mal was da ändern musst!

    lg surfman!



  • wenn ich zb. 2 mal apache.exe starte....wird nur einmal apache.exe beendet!
    ist schon logo weil die prozesse eine unterschiedliche ip haben.....aber kann man das a so machen wenn ich apache.exe beenden will das egal wie oft apache.exe gestartet wurde alle prozesse beendet werden?

    lg



  • surf. schrieb:

    aber wenn man webfritzi seinen code ändern kann dann wärs auch ok....zeig mal was da ändern musst!

    Nö. Ich geb dir hier nur ne Anleitung (zum x-ten Mal). Du kannst Neo's Code verwenden (nach einiger Umänderung), um die Process-ID zu deiner laufenden Anwendung zu bekommen. Dann kannst du meine Funktion TerminateApp() verwenden, um den Prozess zu beenden.
    Und frag jetzt bitte nicht nochmal nach Code. Dir wurde jetzt hier und auf winapi.net schon genug vorgesetzt. Mach endlich was draus, oder, wenn du es nicht hinbekommst, geh erstmal ein C/C++ - Tutorial durch.


Anmelden zum Antworten