VB icon

Command Line Process Killer

Email
Submitted on: 1/7/2015 4:17:00 PM
By: #2pencil (from psc cd)  
Level: Intermediate
User Rating: By 2 Users
Compatibility: C, C++ (general), Microsoft Visual C++, Borland C++
Views: 1585
 
     Command line utility for Windows. If no options are used, it simply shows you what processes are running & the size of heap used. After you see what is running, supply a process name & it will obtain it's process ID & kill the process, freeing the memory heap.
 

INCLUDE files:

Can't Copy and Paste this?
Click here for a copy-and-paste friendly version of this code!
//**************************************
//INCLUDE files for :Command Line Process Killer
//**************************************
#include <windows.h>
#include <winuser.h>
#include <tlhelp32.h>
#include <stdio.h>
code:
Can't Copy and Paste this?
Click here for a copy-and-paste friendly version of this code!
				
//**************************************
// Name: Command Line Process Killer
// Description:Command line utility for Windows. If no options are used, it simply shows you what processes are running & the size of heap used. After you see what is running, supply a process name & it will obtain it's process ID & kill the process, freeing the memory heap.
// By: #2pencil (from psc cd)
//
// Inputs:none required, process name
//
// Returns:heap size per process
//
// Side Effects:You will loose any data if you do not save before killing a process. For example, you have notepad open with updated information, & you kill the process.
//**************************************

#include <windows.h>
#include <winuser.h>
#include <tlhelp32.h>
#include <stdio.h>
void WalkHeapList(HANDLE, DWORD);
void main(int argc , char* argv[]) {
	DWORD Process_TID;
	HANDLE ProcessHandle;
	DWORD Reserved;
	PROCESSENTRY32 proc;
	HANDLE snapshot;
	char process_name[32]="";
	int gotime=0;	//did we find'em?
	proc.dwSize = sizeof(proc);
	snapshot = CreateToolhelp32Snapshot(TH32CS_SNAPALL,0);
	Process32First(snapshot, &proc);
	if(argc==2){
		if(strlen(argv[1])+1 > sizeof(process_name)){
			printf("Process request exceeds buffer limitations");
			exit(1);
		}
		else {
			strcpy(process_name,argv[1]);
			printf("\nLooking for %s",argv[1]);
		}
}
do {
	// Find the Given Process
		if(strcmp(proc.szExeFile,process_name)==0) {
			if(argc==2) {
				printf("\nKilling %s:%d",proc.szExeFile,proc.th32ProcessID);
				Process_TID = proc.th32ProcessID;
				gotime++;
			}
		}
		else {
			if(argc==1) {
				printf("\n%s:%d",proc.szExeFile,proc.th32ProcessID);
				WalkHeapList(snapshot, proc.th32ProcessID);
			}
	}
}while (Process32Next(snapshot, &proc));
CloseHandle(snapshot);
// Get the Process's handle and blow it away
if(gotime>0) {
		ProcessHandle = OpenProcess(PROCESS_ALL_ACCESS | PROCESS_TERMINATE, FALSE, Process_TID);
	 	TerminateProcess(ProcessHandle, (DWORD)0);
 	}
}
void WalkHeapList(HANDLE snapshot, DWORD PID) {
HEAPLIST32 heap;
HEAPENTRY32 block;
unsigned long heapsize;
unsigned long freesize;
heap.dwSize = sizeof(heap);
block.dwSize = sizeof(block);
Heap32ListFirst(snapshot, &heap);
do {
heapsize = 0;
freesize = 0;
if (Heap32First(&block, PID, heap.th32HeapID)) {
do {
heapsize += block.dwBlockSize;
if (block.dwFlags & LF32_FREE)
freesize += block.dwBlockSize;
} while (Heap32Next(&block));
printf("\n\tHeap: %lu bytes [%lu free]",heapsize,freesize);
}
} while (Heap32ListNext(snapshot, &heap));
}


Report Bad Submission
Use this form to tell us if this entry should be deleted (i.e contains no code, is a virus, etc.).
This submission should be removed because:

Your Vote

What do you think of this code (in the Intermediate category)?
(The code with your highest vote will win this month's coding contest!)
Excellent  Good  Average  Below Average  Poor (See voting log ...)
 

Other User Comments


 There are no comments on this submission.
 

Add Your Feedback
Your feedback will be posted below and an email sent to the author. Please remember that the author was kind enough to share this with you, so any criticisms must be stated politely, or they will be deleted. (For feedback not related to this particular code, please click here instead.)
 

To post feedback, first please login.