#include <windows.h>
#include <TlHelp32.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int ListProcess(void);
int PutPidByName(char *name);
int main(int argc,char **argv)
{
if(argc < 2)
{
printf("USAGE : ./listprocess -h\n");
exit(1);
}
if(!strcmp(argv[1],"-h"))
{
printf("Sakiir ListProcess V1.0\n");
printf("\n");
printf("\t -k <pid> // Kill A Process\n");
printf("\t -l // List All Processes\n");
printf("\t -g <name> // Put Pid By Name\n");
exit(1);
}
if(!strcmp(argv[1],"-g"))
{
int szRes;
if(argv[2] == NULL)
{
printf("[-] Arg2 i NULL .. \n");
exit(-1);
}
szRes= PutPidByName(argv[2]);
if(szRes < 0)
{
printf("[-] Failed To PutPidByName\n");
exit(-1);
}
}
if(!strcmp(argv[1],"-l"))
{
int szRes = ListProcess();
if(szRes < 0)
{
printf("[-] Failed To List Processes\n");
exit(-1);
}
}
if(!strcmp(argv[1],"-k"))
{
DWORD a;
DWORD pPriority = 0;
if(argv[2] != NULL)
{
DWORD pid = atoi(argv[2]);
HANDLE hProcess = OpenProcess(PROCESS_ALL_ACCESS, 0, pid);
if(hProcess == NULL)
{
printf("[-] Failed To OpenProcess %d\n",pid);
exit(-1);
}
pPriority = GetPriorityClass(hProcess);
if(!pPriority)
{
printf("[-] Failed To GetPriorityClass \n");
exit(-1);
}
a = TerminateProcess(hProcess,NULL);
if(a == 0)
{
printf("[-] Failed To Kill Process, Maybe Not Enought Priorties\n");
exit(-1);
}
printf("[+] Process Kill\n");
exit(1);
}else printf("[-] PID is NULL\n");
}
exit(1);
}
int ListProcess(void)
{
PROCESSENTRY32 pe32;
HANDLE hProcessSnap;
hProcessSnap = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
if(hProcessSnap == INVALID_HANDLE_VALUE)
{
MessageBox(NULL,"Failed To CreateToolhelp32Snapshot.. :/","ERROR",MB_OK);
exit(-1);
}
pe32.dwSize = sizeof(PROCESSENTRY32);
if( !Process32First( hProcessSnap, &pe32 ) )
{
MessageBox(NULL,"Failed To Process32First.. :/","ERROR",MB_OK);
exit(-1);
}
printf("\n");
do
{
printf("[~] %s \n", pe32.szExeFile);
printf("---> With Pid : %d\n\n",pe32.th32ProcessID);
}while(Process32Next(hProcessSnap,&pe32));
return 1;
}
int PutPidByName(wchar_t *name)
{
PROCESSENTRY32 pe32;
HANDLE hProcessSnap;
hProcessSnap = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
if(hProcessSnap == INVALID_HANDLE_VALUE)
{
MessageBox(NULL,"Failed To CreateToolhelp32Snapshot.. :/","ERROR",MB_OK);
exit(-1);
}
pe32.dwSize = sizeof(PROCESSENTRY32);
if( !Process32First( hProcessSnap, &pe32 ) )
{
MessageBox(NULL,"Failed To Process32First.. :/","ERROR",MB_OK);
exit(-1);
}
printf("\n");
do
{
if(wcscmp(name,pe32.szExeFile) == 0)
{
printf("[~] %s \n", pe32.szExeFile);
printf("---> With Pid : %d\n\n",pe32.th32ProcessID);
}
}while(Process32Next(hProcessSnap,&pe32));
return 1;
}