daily pastebin goal
15%
SHARE
TWEET

Kill process [Win32 API] (4 version)

appo Dec 27th, 2013 366 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #pragma hdrstop
  2. #pragma argsused
  3. #pragma comment (lib, "psapi.lib")
  4.  
  5. #include <tchar.h>
  6. #include <stdio.h>
  7. #include "windows.h"
  8. #include <psapi.h>
  9. #include <stdio.h>
  10.  
  11. DWORD GetProcessIdByName(char* lpProcessName)
  12. {
  13.     // Obtain a list of the process.
  14.     DWORD aProcesses[1024], cbNeeded, cProcesses;
  15.     unsigned int i;
  16.     if (!EnumProcesses (aProcesses, sizeof(aProcesses), &cbNeeded ))
  17.   return 0;
  18.    
  19.     cProcesses = cbNeeded / sizeof(DWORD);
  20.     char szProcessName[MAX_PATH] = "unknown";
  21.     for (i = 0; i < cProcesses; i++)
  22.     {
  23.   HANDLE hProcess = OpenProcess (PROCESS_QUERY_INFORMATION |
  24.   PROCESS_VM_READ,
  25.   FALSE, aProcesses[i]);
  26.   // Получить имя процесса.
  27.   if ( hProcess )
  28.   {
  29.     HMODULE hMod;
  30.     DWORD cbNeeded;
  31.     if ( EnumProcessModules (hProcess, &hMod, sizeof(hMod),
  32.     &cbNeeded) )
  33.     {
  34.     GetModuleBaseName (hProcess, hMod, szProcessName,
  35.         sizeof(szProcessName));
  36.     }
  37.   }
  38.  
  39.   if(!lstrcmpi (szProcessName, lpProcessName))
  40.   {
  41.     CloseHandle (hProcess);
  42.     return aProcesses[i];
  43.   }
  44.   CloseHandle (hProcess);
  45.     }
  46.     return NULL;
  47. }
  48. void Kill(char *szxProcName)
  49. {
  50.     HANDLE hProcess;
  51.            // Get the process ID by name
  52.             DWORD dwProcID = GetProcessIdByName(szxProcName);
  53. // Открываем процесс для его закрытия
  54.   hProcess = OpenProcess( PROCESS_TERMINATE, FALSE, dwProcID );
  55.   if ( (INVALID_HANDLE_VALUE == hProcess) || (NULL == hProcess) )
  56.    printf( "OpenProcess() failed, err = %lu\n", GetLastError() ); // Ошибка
  57.   else
  58.   {
  59.    // kill process
  60.    if ( ! TerminateProcess( hProcess, (DWORD) -1 ) )
  61.     printf( "TerminateProcess() failed, err = %lu\n", GetLastError() );// Ошибка
  62.    else
  63.     puts( "killed." );
  64. // Закрываем дескриптор
  65.    CloseHandle( hProcess );
  66.   }
  67. }
  68. void main()
  69. {
  70.  
  71.         Kill("notepad++.exe");
  72.         system("pause");
  73. }
  74.  
  75. // Coded by Appo //
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top