Thread: command line taskmgr / taskkiller for windows

  1. #1
    noob lepricaun's Avatar
    Join Date
    Jul 2004
    Posts
    26

    command line taskmgr / taskkiller for windows

    hi all,

    a couple of months ago i was searching really hard for a command line version of the windows taskmgr, but i wasn't able to find one

    so today i decided to write one myself, my brain was extremely overheating during the coding :lol:, but i have finished it just a few minutes ago

    since no one could give me a link or name from a similar program, i thought that it might come in handy for some people, so i decided to publish it under the GPL, so here is the source:

    Code:
    /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * 
    *                                                                                  *
    *  File: prokill.c                                                                 *
    *                                                                                  *
    *  Purpose: commandline processkiller / taskmanager for windows                    *
    *                                                                                  *       
    *  Usage: compile to prokill.exe and run it!                                       *
    *                                                                                  *
    *  Copyright (C) 2004  Scorpius, [email protected], all rights reserved   *
    *                                                                                  *
    *  This program is free software; you can redistribute it and/or                   *
    *  modify it under the terms of the GNU General Public License                     *
    *  as published by the Free Software Foundation; either version 2                  *
    *  of the License, or (at your option) any later version.                          *
    *                                                                                  *
    *  This program is distributed in the hope that it will be useful,                 *
    *  but WITHOUT ANY WARRANTY; without even the implied warranty of                  *
    *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the                   *
    *  GNU General Public License for more details.                                    *
    *                                                                                  *
    *  You should have received a copy of the GNU General Public License               *
    *  along with this program; if not, write to the Free Software                     *
    *  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.     *
    *                                                                                  *
    * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
    
    #include <stdio.h>
    #include <windows.h>
    #include <tlhelp32.h>
    
    int main(void)
    {
        int info,pid,exitcode,term;
        long code;
        HANDLE Snap,Process;
        PROCESSENTRY32 proc32;
        
        Snap=CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS,0);/*take a snap of all processes*/
      if(Snap==INVALID_HANDLE_VALUE)
      {
        printf("Error creating snapshot of current processes");
        return EXIT_FAILURE;
      }
      proc32.dwSize=sizeof(PROCESSENTRY32); /*set size of structure*/  
      
      system("cls");
      printf("Prokill.exe by Scorpius, [email protected], 2004.\n\n");
      printf("PID:\t\tPROCESS NAME:\n");
      while((Process32Next(Snap,&proc32))==TRUE)/*while we haven't reached the final process*/
      {
          printf("\n%d\t\t%s",proc32.th32ProcessID,proc32.szExeFile);/*print pid and processname*/
      } 
      CloseHandle(Snap);/*cleaning up*/
      printf("\n\nEnter PID of process to kill (or 0 to quit): ");
      scanf("%d",&pid);/*get the PID of the process to kill*/
      if(pid<1)
      {
          printf("Illegal PID.");
          return EXIT_FAILURE;
      }
      Process=OpenProcess(PROCESS_QUERY_INFORMATION,FALSE,pid);/*obtain a handle to the process*/
      if(Process==NULL)
      {
          printf("Illegal PID.");
          CloseHandle(Process);
          return EXIT_FAILURE;
      }
      exitcode=GetExitCodeProcess(Process,&code);/*get the exitcode from the process*/   
      if(exitcode==0)
      {
          printf("Unable to retrieve exitcode.");
          CloseHandle(Process);
          return EXIT_FAILURE;
      }
      Process=OpenProcess(PROCESS_TERMINATE,FALSE,pid);/*see if we have terminate rights*/
      if(Process==NULL)
      {
          printf("Unable to terminate process.");
          CloseHandle(Process);
          return EXIT_FAILURE;
      }
      term=TerminateProcess(Process,code);/*terminate the process*/
      if (term==0)
      {
             printf("Terminating process %d failed.",pid);
             CloseHandle(Process);
             return EXIT_FAILURE;
      }
      printf("Process %d killed successfully.",pid);/*all went fine, process is killed*/
      CloseHandle(Process);
      return EXIT_SUCCESS;
    }
    i hope you find it useful
    The path of access leads to the tower of wisdom...
    __________________________________________________ ___________________

    Code:
    
    #include <stdio.h>
    
    int main(void)
    {
            const char buf[17]="Hello everybody!";
    	printf("%s\n",buf);
            return 0;
    }
    

  2. #2
    Registered User
    Join Date
    Mar 2004
    Posts
    161
    mine

    Code:
    #include <windows.h>
    #include <psapi.h>
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    int Kill(int pID)
    {
    	int ret;
    	HANDLE  hProcess=OpenProcess(PROCESS_ALL_ACCESS,
    TRUE,pID);
    				
    	if(hProcess==NULL)
    	{
    		return 1;
    	}
    	
    	ret = !TerminateProcess(hProcess,0);
    	CloseHandle(hProcess);
    	return ret;
    }
    
    char *  GetProcessName(DWORD processID ,char * szProcessName, DWORD bufsiz)
    {   
    
        HANDLE hProcess = OpenProcess( PROCESS_QUERY_INFORMATION |
                                       PROCESS_VM_READ,
                                       0, processID );
    
    	szProcessName[0]='\0';
    
        if (NULL != hProcess )
        {
            HMODULE hMod;
            DWORD cbNeeded;
    
            if ( EnumProcessModules( hProcess, &hMod, sizeof(hMod), 
                 &cbNeeded) )
            {
                GetModuleBaseName( hProcess, hMod, szProcessName, 
                                   bufsiz );
            }
            else return NULL;
        }
        else return NULL;
    
       
        CloseHandle( hProcess );
    
    	return szProcessName;
    }
    
    
    
    int main(int argc, char**argv)
    {
    
        char szProcessName[MAX_PATH];
    	
    	DWORD aProcesses[1024], cbNeeded, cProcesses;
        unsigned int i;
    
        if ( GetVersion() > 0x80000000 )
          
          {
          printf("Funziono solo su XP!!\n");
          exit; 
          }
       	
           
        if ( !EnumProcesses( aProcesses, sizeof(aProcesses), &cbNeeded ) )
            return 1;
       
        cProcesses = cbNeeded / sizeof(DWORD);
    
    	    
        printf("\n");
        printf(" [");
        printf("Bianconiglio's TaskManager");
        printf("]\n");
        printf(" [--Processi in Esecuzione--]\n");
        printf("\n");
    	
        for ( i = 0; i < cProcesses; ++i )
    	{	
     		GetProcessName(aProcesses[i],szProcessName,MAX_PATH);
    		if ( szProcessName[0] != '\0' ) {
        printf(" %s\n", szProcessName);
    }
    	
    	}
    
    	printf("\n");
    	       
        system("pause");	
    	return 0;
    }
    This forum is the best one I've ever seen. Great ppl, great coders

Popular pages Recent additions subscribe to a feed