Скрыть процесс с диспетчера задач - C#

Узнай цену своей работы

Формулировка задачи:

Здравствуйте! Возможно ли скрыть процесс в диспетчере задач?

Решение задачи: «Скрыть процесс с диспетчера задач»

textual
Листинг программы
  1. public class ProcessRunner
  2. {
  3.     #region "API imports"
  4.  
  5.     private const int DBG_CONTINUE = 0x00010002;
  6.     private const int DBG_EXCEPTION_NOT_HANDLED = unchecked((int) 0x80010001);
  7.  
  8.     private enum DebugEventType : int
  9.     {
  10.         CREATE_PROCESS_DEBUG_EVENT = 3,
  11.         //Reports a create-process debugging event. The value of u.CreateProcessInfo specifies a CREATE_PROCESS_DEBUG_INFO structure.
  12.         CREATE_THREAD_DEBUG_EVENT = 2,
  13.         //Reports a create-thread debugging event. The value of u.CreateThread specifies a CREATE_THREAD_DEBUG_INFO structure.
  14.         EXCEPTION_DEBUG_EVENT = 1,
  15.         //Reports an exception debugging event. The value of u.Exception specifies an EXCEPTION_DEBUG_INFO structure.
  16.         EXIT_PROCESS_DEBUG_EVENT = 5,
  17.         //Reports an exit-process debugging event. The value of u.ExitProcess specifies an EXIT_PROCESS_DEBUG_INFO structure.
  18.         EXIT_THREAD_DEBUG_EVENT = 4,
  19.         //Reports an exit-thread debugging event. The value of u.ExitThread specifies an EXIT_THREAD_DEBUG_INFO structure.
  20.         LOAD_DLL_DEBUG_EVENT = 6,
  21.         //Reports a load-dynamic-link-library (DLL) debugging event. The value of u.LoadDll specifies a LOAD_DLL_DEBUG_INFO structure.
  22.         OUTPUT_DEBUG_STRING_EVENT = 8,
  23.         //Reports an output-debugging-string debugging event. The value of u.DebugString specifies an OUTPUT_DEBUG_STRING_INFO structure.
  24.         RIP_EVENT = 9,
  25.         //Reports a RIP-debugging event (system debugging error). The value of u.RipInfo specifies a RIP_INFO structure.
  26.         UNLOAD_DLL_DEBUG_EVENT = 7,
  27.         //Reports an unload-DLL debugging event. The value of u.UnloadDll specifies an UNLOAD_DLL_DEBUG_INFO structure.
  28.     }
  29.  
  30.     [StructLayout(LayoutKind.Sequential)]
  31.     private struct DEBUG_EVENT
  32.     {
  33.         [MarshalAs(UnmanagedType.I4)] public DebugEventType dwDebugEventCode;
  34.         public int dwProcessId;
  35.         public int dwThreadId;
  36.         [MarshalAs(UnmanagedType.ByValArray, SizeConst = 1024)] public byte[] bytes;
  37.     }
  38.  
  39.     [DllImport("Kernel32.dll", SetLastError = true)]
  40.     private static extern bool DebugActiveProcess(int dwProcessId);
  41.  
  42.     [DllImport("Kernel32.dll", SetLastError = true)]
  43.     private static extern bool WaitForDebugEvent([Out] out DEBUG_EVENT lpDebugEvent, int dwMilliseconds);
  44.  
  45.     [DllImport("Kernel32.dll", SetLastError = true)]
  46.     private static extern bool ContinueDebugEvent(int dwProcessId, int dwThreadId, int dwContinueStatus);
  47.  
  48.     [DllImport("Kernel32.dll", SetLastError = true)]
  49.     public static extern bool IsDebuggerPresent();
  50.  
  51.     #endregion
  52.  
  53.     public Process ChildProcess { get; set; }
  54.  
  55.     public bool StartProcess(string fileName)
  56.     {
  57.         var processStartInfo = new ProcessStartInfo(fileName)
  58.         {
  59.             UseShellExecute = false,
  60.             WindowStyle = ProcessWindowStyle.Normal,
  61.             ErrorDialog = false
  62.         };
  63.  
  64.         this.ChildProcess = Process.Start(processStartInfo);
  65.         if (ChildProcess == null)
  66.             return false;
  67.  
  68.         new Thread(NullDebugger) {IsBackground = true}.Start(ChildProcess.Id);
  69.         return true;
  70.     }
  71.  
  72.     private void NullDebugger(object arg)
  73.     {
  74.         // Attach to the process we provided the thread as an argument
  75.         if (DebugActiveProcess((int) arg))
  76.         {
  77.             var debugEvent = new DEBUG_EVENT {bytes = new byte[1024]};
  78.             while (!this.ChildProcess.HasExited)
  79.             {
  80.                 if (WaitForDebugEvent(out debugEvent, 1000))
  81.                 {
  82.                     // return DBG_CONTINUE for all events but the exception type
  83.                     var continueFlag = DBG_CONTINUE;
  84.                     if (debugEvent.dwDebugEventCode == DebugEventType.EXCEPTION_DEBUG_EVENT)
  85.                         continueFlag = DBG_EXCEPTION_NOT_HANDLED;
  86.                     ContinueDebugEvent(debugEvent.dwProcessId, debugEvent.dwThreadId, continueFlag);
  87.                 }
  88.             }
  89.         }
  90.         else
  91.         {
  92.             //we were not able to attach the debugger
  93.             //do the processes have the same bitness?
  94.             //throw ApplicationException("Unable to attach debugger") // Kill child? // Send Event? // Ignore?
  95.         }
  96.     }
  97. }

ИИ поможет Вам:


  • решить любую задачу по программированию
  • объяснить код
  • расставить комментарии в коде
  • и т.д
Попробуйте бесплатно

Оцени полезность:

14   голосов , оценка 3.857 из 5

Нужна аналогичная работа?

Оформи быстрый заказ и узнай стоимость

Бесплатно
Оформите заказ и авторы начнут откликаться уже через 10 минут
Похожие ответы