// (c) Anatoliy & Taras Slobodskyy 2004
// Shecks if process is running and kills it if necessary [windows application]
// Compile with lcc-win32
// version 1.1

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

//  Forward declarations:

int main( int argc, char *argv[] )
{
  HANDLE hProcessSnap;
  HANDLE hProcess;
  PROCESSENTRY32 pe32;
//  BOOL itruns;

  if (argc>1) {
   //initialize runing flag
//   itruns=0;

   // Take a snapshot of all processes in the system.
   hProcessSnap = CreateToolhelp32Snapshot( TH32CS_SNAPPROCESS, 0 );

   // Set the size of the structure before using it.
   pe32.dwSize = sizeof( PROCESSENTRY32 );

   // Retrieve information about the first process,
   // and exit if unsuccessful
   Process32First( hProcessSnap, &pe32 );
   // Now walk the snapshot of processes
   do
   {
	if (!strcmp(argv[1], pe32.szExeFile)) {
//	 itruns=1;
	 if (argc>2) {
	  hProcess = OpenProcess( PROCESS_ALL_ACCESS, FALSE, pe32.th32ProcessID );
	  TerminateProcess( hProcess, (DWORD) -1 );
	 } else {
	  printf( "runs" );
	  return 0;
	 }
	}
   } while( Process32Next( hProcessSnap, &pe32 ) );
//   if (!itruns) printf( "no" );
  } else {
   printf ("Use: pskill processname [c]\nc says to kill the process. It returns 'runs' if the process exists.");
  }
  return 1;
}
