I'm doing a "process killer" and I do not know how to do that when pressing a key (F9 in this case) the code is executed even if it is minimized.
#include <iostream>
#include "conio.h"
#include "kill.h"
using namespace std;
int main(){
loop:
char tecla;
tecla = getch();
if(tecla==67) // 67=F9
{
killprocess("notepad.exe");
}
goto loop;
}
The file "kill.h" has this code:
#include <iostream>
#include <windows.h>
#include <process.h>
#include <Tlhelp32.h>
#include <winbase.h>
#include <string.h>
void killprocess(const char *filename)
{
HANDLE hSnapShot = CreateToolhelp32Snapshot(TH32CS_SNAPALL, NULL);
PROCESSENTRY32 pEntry;
pEntry.dwSize = sizeof (pEntry);
BOOL hRes = Process32First(hSnapShot, &pEntry);
while (hRes)
{
if (strcmp(pEntry.szExeFile, filename) == 0)
{
HANDLE hProcess = OpenProcess(PROCESS_TERMINATE, 0,
(DWORD) pEntry.th32ProcessID);
if (hProcess != NULL)
{
TerminateProcess(hProcess, 9);
CloseHandle(hProcess);
}
}
hRes = Process32Next(hSnapShot, &pEntry);
}
CloseHandle(hSnapShot);
}