Advertisement
appo

Kill process [Win32 API] (Option 1)

Dec 27th, 2013
462
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.92 KB | None | 0 0
  1. #include <windows.h>
  2. #include <process.h>
  3. #include <Tlhelp32.h>
  4. #include <winbase.h>
  5. #include <string.h>
  6. void killProcessByName(const char *filename)
  7. {
  8.     HANDLE hSnapShot = CreateToolhelp32Snapshot(TH32CS_SNAPALL, NULL);
  9.     PROCESSENTRY32 pEntry;
  10.     pEntry.dwSize = sizeof (pEntry);
  11.     BOOL hRes = Process32First(hSnapShot, &pEntry);
  12.     while (hRes)
  13.     {
  14.         if (strcmp(pEntry.szExeFile, filename) == 0)
  15.         {
  16.             HANDLE hProcess = OpenProcess(PROCESS_TERMINATE, 0,
  17.                                           (DWORD) pEntry.th32ProcessID);
  18.             if (hProcess != NULL)
  19.             {
  20.                 TerminateProcess(hProcess, 9);
  21.                 CloseHandle(hProcess);
  22.             }
  23.         }
  24.         hRes = Process32Next(hSnapShot, &pEntry);
  25.     }
  26.     CloseHandle(hSnapShot);
  27. }
  28. int main()
  29. {
  30.     killProcessByName("notepad++.exe"); //process Name
  31.     return 0;
  32. }
  33.  
  34. // Coded by Appo //
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement