In: Computer Science
Write a C program that would constantly watch all processes (say once every second), and if it encountered a notepad, it would kill it immediately.
Note: Done accordingly. Please comment for any problem. Please Uprate. Thanks.
Code:
#include <stdlib.h>
#include <windows.h> // for linux use #include
<unistd.h>
#include <tlhelp32.h>
bool IsProcessRunning(const wchar_t *processName)
{
bool exists = false;
PROCESSENTRY32 entry;
entry.dwSize = sizeof(PROCESSENTRY32);
HANDLE snapshot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, NULL);
if (Process32First(snapshot, &entry))
while (Process32Next(snapshot, &entry))
if (!wcsicmp(entry.szExeFile, processName))
           {
exists = true;
          
    break;
           }
CloseHandle(snapshot);
return exists;
}
int main(){
   wchar_t name[20]=L"notepad.exe";
   while(true){
   if(IsProcessRunning(name))//checking if notepad is
running
system("taskkill /IM notepad.exe /F");
   Sleep(1);
   }
}
Output:
