Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Sep 12th, 2004, 3:48 PM   #1
lepricaun
Hobbyist Programmer
 
lepricaun's Avatar
 
Join Date: Aug 2004
Location: The Netherlands
Posts: 111
Rep Power: 5 lepricaun is on a distinguished road
Commandline 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 , 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:

/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * 
*                                         *
* File: prokill.c                                 *
*                                         *
* Purpose: commandline processkiller / taskmanager for windows          *
*                                         *    
* Usage: compile to prokill.exe and run it!                    *
*                                         *
* Copyright (C) 2004 Scorpius, scorpius_unknown@yahoo.com, 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 pid,exitcode,term;
  unsigned 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, scorpius_unknown@yahoo.com, 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

[edit]altered the code (made the long int an unsigned long) to make the program work on more systems [/edit]
__________________
http://www.white-scorpion.nl
lepricaun is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 7:08 PM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC