Cuando se tienen requerimientos muy particulares a la plataforma Windows esta clase puede ser muy útil, por ejemplo podríamos escribir un keylogger con relativa facilidad o una aplicación espía se que se encargue de tomar screen shots de cierta ventana o del escritorio completo, o algunas otras aplicaciones.
En seguida coloco el código.
using System; using System.Runtime.InteropServices; using System.Text; namespace F.Win { /// <summary> /// win32 class to wrap win32 API services /// </summary> public sealed class WinAPI32 { #region Message Constants public const int EM_GETPASSWORDCHAR = 0xD2; public const int EM_SETPASSWORDCHAR = 0xCC; public const int LB_FINDSTRING = 0x18F; public const int WM_NULL = 0x0; public const int WM_CREATE = 0x1; public const int WM_DESTROY = 0x2; public const int WM_MOVE = 0x3; public const int WM_SIZE = 0x5; public const int WM_ACTIVATE = 0x6; public const int WM_SETFOCUS = 0x7; public const int WM_KILLFOCUS = 0x8; public const int WM_ENABLE = 0xA; public const int WM_SETREDRAW = 0xB; public const int WM_SETTEXT = 0xC; public const int WM_GETTEXT = 0xD; public const int WM_GETTEXTLENGTH = 0xE; public const int WM_PAINT = 0xF; public const int WM_CLOSE = 0x10; public const int WM_QUERYENDSESSION = 0x11; public const int WM_QUIT = 0x12; public const int WM_QUERYOPEN = 0x13; public const int WM_ERASEBKGND = 0x14; public const int WM_SYSCOLORCHANGE = 0x15; public const int WM_ENDSESSION = 0x16; public const int WM_SHOWWINDOW = 0x18; public const int WM_WININICHANGE = 0x1A; public const int WM_DEVMODECHANGE = 0x1B; public const int WM_ACTIVATEAPP = 0x1C; public const int WM_FONTCHANGE = 0x1D; public const int WM_TIMECHANGE = 0x1E; public const int WM_CANCELMODE = 0x1F; public const int WM_SETCURSOR = 0x20; public const int WM_MOUSEACTIVATE = 0x21; public const int WM_CHILDACTIVATE = 0x22; public const int WM_QUEUESYNC = 0x23; public const int WM_GETMINMAXINFO = 0x24; public const int WM_PAINTICON = 0x26; public const int WM_ICONERASEBKGND = 0x27; public const int WM_NEXTDLGCTL = 0x28; public const int WM_SPOOLERSTATUS = 0x2A; public const int WM_DRAWITEM = 0x2B; public const int WM_MEASUREITEM = 0x2C; public const int WM_DELETEITEM = 0x2D; public const int WM_VKEYTOITEM = 0x2E; public const int WM_CHARTOITEM = 0x2F; public const int WM_SETFONT = 0x30; public const int WM_GETFONT = 0x31; public const int WM_SETHOTKEY = 0x32; public const int WM_GETHOTKEY = 0x33; public const int WM_QUERYDRAGICON = 0x37; public const int WM_COMPAREITEM = 0x39; public const int WM_COMPACTING = 0x41; public const int WM_COMMNOTIFY = 0x44 ; //'/* no longer suported */ public const int WM_WINDOWPOSCHANGING = 0x46; public const int WM_WINDOWPOSCHANGED = 0x47; public const int WM_POWER = 0x48; public const int WM_COPYDATA = 0x4A; public const int WM_CANCELJOURNAL = 0x4B; public const int WM_NOTIFY = 0x4E; public const int WM_INPUTLANGCHANGEREQUEST = 0x50; public const int WM_INPUTLANGCHANGE = 0x51; public const int WM_TCARD = 0x52; public const int WM_HELP = 0x53; public const int WM_USERCHANGED = 0x54; public const int WM_NOTIFYFORMAT = 0x55; public const int WM_CONTEXTMENU = 0x7B; public const int WM_STYLECHANGING = 0x7C; public const int WM_STYLECHANGED = 0x7D; public const int WM_DISPLAYCHANGE = 0x7E; public const int WM_GETICON = 0x7F; public const int WM_SETICON = 0x80; public const int WM_NCCREATE = 0x81; public const int WM_NCDESTROY = 0x82; public const int WM_NCCALCSIZE = 0x83; public const int WM_NCHITTEST = 0x84; public const int WM_NCPAINT = 0x85; public const int WM_NCACTIVATE = 0x86; public const int WM_GETDLGCODE = 0x87; public const int WM_SYNCPAINT = 0x88; public const int WM_NCMOUSEMOVE = 0xA0; public const int WM_NCLBUTTONDOWN = 0xA1; public const int WM_NCLBUTTONUP = 0xA2; public const int WM_NCLBUTTONDBLCLK = 0xA3; public const int WM_NCRBUTTONDOWN = 0xA4; public const int WM_NCRBUTTONUP = 0xA5; public const int WM_NCRBUTTONDBLCLK = 0xA6; public const int WM_NCMBUTTONDOWN = 0xA7; public const int WM_NCMBUTTONUP = 0xA8; public const int WM_NCMBUTTONDBLCLK = 0xA9; public const int WM_NCXBUTTONDOWN = 0xAB; public const int WM_NCXBUTTONUP = 0xAC; public const int WM_NCXBUTTONDBLCLK = 0xAD; public const int WM_INPUT = 0xFF; public const int WM_KEYFIRST = 0x100; public const int WM_KEYDOWN = 0x100; public const int WM_KEYUP = 0x101; public const int WM_CHAR = 0x102; public const int WM_DEADCHAR = 0x103; public const int WM_SYSKEYDOWN = 0x104; public const int WM_SYSKEYUP = 0x105; public const int WM_SYSCHAR = 0x106; public const int WM_SYSDEADCHAR = 0x107; public const int WM_UNICHAR = 0x109; public const int WM_KEYLAST = 0x109; public const int WM_IME_STARTCOMPOSITION = 0x10D; public const int WM_IME_ENDCOMPOSITION = 0x10E; public const int WM_IME_COMPOSITION = 0x10F; public const int WM_IME_KEYLAST = 0x10F; public const int WM_INITDIALOG = 0x110; public const int WM_COMMAND = 0x111; public const int WM_SYSCOMMAND = 0x112; public const int WM_TIMER = 0x113; public const int WM_HSCROLL = 0x114; public const int WM_VSCROLL = 0x115; public const int WM_INITMENU = 0x116; public const int WM_INITMENUPOPUP = 0x117; public const int WM_MENUSELECT = 0x11F; public const int WM_MENUCHAR = 0x120; public const int WM_ENTERIDLE = 0x121; public const int WM_MENURBUTTONUP = 0x122; public const int WM_MENUDRAG = 0x123; public const int WM_MENUGETOBJECT = 0x124; public const int WM_UNINITMENUPOPUP = 0x125; public const int WM_MENUCOMMAND = 0x126; public const int WM_CHANGEUISTATE = 0x127; public const int WM_UPDATEUISTATE = 0x128; public const int WM_QUERYUISTATE = 0x129; public const int WM_CTLCOLORMSGBOX = 0x132; public const int WM_CTLCOLOREDIT = 0x133; public const int WM_CTLCOLORLISTBOX = 0x134; public const int WM_CTLCOLORBTN = 0x135; public const int WM_CTLCOLORDLG = 0x136; public const int WM_CTLCOLORSCROLLBAR = 0x137; public const int WM_CTLCOLORSTATIC = 0x138; public const int MN_GETHMENU = 0x1E1; public const int WM_MOUSEFIRST = 0x200; public const int WM_MOUSEMOVE = 0x200; public const int WM_LBUTTONDOWN = 0x201; public const int WM_LBUTTONUP = 0x202; public const int WM_LBUTTONDBLCLK = 0x203; public const int WM_RBUTTONDOWN = 0x204; public const int WM_RBUTTONUP = 0x205; public const int WM_RBUTTONDBLCLK = 0x206; public const int WM_MBUTTONDOWN = 0x207; public const int WM_MBUTTONUP = 0x208; public const int WM_MBUTTONDBLCLK = 0x209; public const int WM_MOUSEWHEEL = 0x20A; public const int WM_XBUTTONDOWN = 0x20B; public const int WM_XBUTTONUP = 0x20C; public const int WM_XBUTTONDBLCLK = 0x20D; public const int WM_MOUSELAST = 0x20A; public const int WM_PARENTNOTIFY = 0x210; public const int WM_ENTERMENULOOP = 0x211; public const int WM_EXITMENULOOP = 0x212; public const int WM_NEXTMENU = 0x213; public const int WM_SIZING = 0x214; public const int WM_CAPTURECHANGED = 0x215; public const int WM_MOVING = 0x216; public const int WM_POWERBROADCAST = 0x218; public const int WM_DEVICECHANGE = 0x219; public const int WM_MDICREATE = 0x220; public const int WM_MDIDESTROY = 0x221; public const int WM_MDIACTIVATE = 0x222; public const int WM_MDIRESTORE = 0x223; public const int WM_MDINEXT = 0x224; public const int WM_MDIMAXIMIZE = 0x225; public const int WM_MDITILE = 0x226; public const int WM_MDICASCADE = 0x227; public const int WM_MDIICONARRANGE = 0x228; public const int WM_MDIGETACTIVE = 0x229; public const int WM_MDISETMENU = 0x230; public const int WM_ENTERSIZEMOVE = 0x231; public const int WM_EXITSIZEMOVE = 0x232; public const int WM_DROPFILES = 0x233; public const int WM_MDIREFRESHMENU = 0x234; public const int WM_IME_SETCONTEXT = 0x281; public const int WM_IME_NOTIFY = 0x282; public const int WM_IME_CONTROL = 0x283; public const int WM_IME_COMPOSITIONFULL = 0x284; public const int WM_IME_SELECT = 0x285; public const int WM_IME_CHAR = 0x286; public const int WM_IME_REQUEST = 0x288; public const int WM_IME_KEYDOWN = 0x290; public const int WM_IME_KEYUP = 0x291; public const int WM_MOUSEHOVER = 0x2A1; public const int WM_MOUSELEAVE = 0x2A3; public const int WM_NCMOUSEHOVER = 0x2A0; public const int WM_NCMOUSELEAVE = 0x2A2; public const int WM_WTSSESSION_CHANGE = 0x2B1; public const int WM_TABLET_FIRST = 0x2C0; public const int WM_TABLET_LAST = 0x2DF; public const int WM_CUT = 0x300; public const int WM_COPY = 0x301; public const int WM_PASTE = 0x302; public const int WM_CLEAR = 0x303; public const int WM_UNDO = 0x304; public const int WM_RENDERFORMAT = 0x305; public const int WM_RENDERALLFORMATS = 0x306; public const int WM_DESTROYCLIPBOARD = 0x307; public const int WM_DRAWCLIPBOARD = 0x308; public const int WM_PAINTCLIPBOARD = 0x309; public const int WM_VSCROLLCLIPBOARD = 0x30A; public const int WM_SIZECLIPBOARD = 0x30B; public const int WM_ASKCBFORMATNAME = 0x30C; public const int WM_CHANGECBCHAIN = 0x30D; public const int WM_HSCROLLCLIPBOARD = 0x30E; public const int WM_QUERYNEWPALETTE = 0x30F; public const int WM_PALETTEISCHANGING = 0x310; public const int WM_PALETTECHANGED = 0x311; public const int WM_HOTKEY = 0x312; public const int WM_PRINT = 0x317; public const int WM_PRINTCLIENT = 0x318; public const int WM_APPCOMMAND = 0x319; public const int WM_THEMECHANGED = 0x31A; public const int WM_HANDHELDFIRST = 0x358; public const int WM_HANDHELDLAST = 0x35F; public const int WM_AFXFIRST = 0x360; public const int WM_AFXLAST = 0x37F; public const int WM_PENWINFIRST = 0x380; public const int WM_PENWINLAST = 0x38F; #endregion #region Key Constants //Key-Board Constants public const int VK_0 = 0x30; public const int VK_1 = 0x31; public const int VK_2 = 0x32; public const int VK_3 = 0x33; public const int VK_4 = 0x34; public const int VK_5 = 0x35; public const int VK_6 = 0x36; public const int VK_7 = 0x37; public const int VK_8 = 0x38; public const int VK_9 = 0x39; public const int VK_A = 0x41; public const int VK_B = 0x42; public const int VK_C = 0x43; public const int VK_D = 0x44; public const int VK_E = 0x45; public const int VK_F = 0x46; public const int VK_G = 0x47; public const int VK_H = 0x48; public const int VK_I = 0x49; public const int VK_J = 0x4A; public const int VK_K = 0x4B; public const int VK_L = 0x4C; public const int VK_M = 0x4D; public const int VK_N = 0x4E; public const int VK_O = 0x4F; public const int VK_P = 0x50; public const int VK_Q = 0x51; public const int VK_R = 0x52; public const int VK_S = 0x53; public const int VK_T = 0x54; public const int VK_U = 0x55; public const int VK_V = 0x56; public const int VK_W = 0x57; public const int VK_X = 0x58; public const int VK_Y = 0x59; public const int VK_Z = 0x5A; public const int VK_ADD = 0x6B; public const int VK_ATTN = 0xF6; public const int VK_BACK = 0x8; public const int VK_CANCEL = 0x3; public const int VK_CAPITAL = 0x14; public const int VK_CLEAR = 0xC; public const int VK_CONTROL = 0x11; public const int VK_CRSEL = 0xF7; public const int VK_DECIMAL = 0x6E; public const int VK_DELETE = 0x2E; public const int VK_DIVIDE = 0x6F; public const int VK_DOWN = 0x28; public const int VK_END = 0x23; public const int VK_EREOF = 0xF9; public const int VK_ESCAPE = 0x1B; public const int VK_EXECUTE = 0x2B; public const int VK_EXSEL = 0xF8; public const int VK_F1 = 0x70; public const int VK_F10 = 0x79; public const int VK_F11 = 0x7A; public const int VK_F12 = 0x7B; public const int VK_F13 = 0x7C; public const int VK_F14 = 0x7D; public const int VK_F15 = 0x7E; public const int VK_F16 = 0x7F; public const int VK_F17 = 0x80; public const int VK_F18 = 0x81; public const int VK_F19 = 0x82; public const int VK_F2 = 0x71; public const int VK_F20 = 0x83; public const int VK_F21 = 0x84; public const int VK_F22 = 0x85; public const int VK_F23 = 0x86; public const int VK_F24 = 0x87; public const int VK_F3 = 0x72; public const int VK_F4 = 0x73; public const int VK_F5 = 0x74; public const int VK_F6 = 0x75; public const int VK_F7 = 0x76; public const int VK_F8 = 0x77; public const int VK_F9 = 0x78; public const int VK_HELP = 0x2F; public const int VK_HOME = 0x24; public const int VK_INSERT = 0x2D; public const int VK_LBUTTON = 0x1; public const int VK_LCONTROL = 0xA2; public const int VK_LEFT = 0x25; public const int VK_LMENU = 0xA4; public const int VK_LSHIFT = 0xA0; public const int VK_MBUTTON = 0x4; public const int VK_MENU = 0x12; public const int VK_MULTIPLY = 0x6A; public const int VK_NEXT = 0x22; public const int VK_ENTER = 0xA; public const int VK_NONAME = 0xFC; public const int VK_NUMLOCK = 0x90; public const int VK_NUMPAD0 = 0x60; public const int VK_NUMPAD1 = 0x61; public const int VK_NUMPAD2 = 0x62; public const int VK_NUMPAD3 = 0x63; public const int VK_NUMPAD4 = 0x64; public const int VK_NUMPAD5 = 0x65; public const int VK_NUMPAD6 = 0x66; public const int VK_NUMPAD7 = 0x67; public const int VK_NUMPAD8 = 0x68; public const int VK_NUMPAD9 = 0x69; public const int VK_OEM_CLEAR = 0xFE; public const int VK_PA1 = 0xFD; public const int VK_PAUSE = 0x13; public const int VK_PLAY = 0xFA; public const int VK_PRINT = 0x2A; public const int VK_PRIOR = 0x21; public const int VK_PROCESSKEY = 0xE5; public const int VK_RBUTTON = 0x2; public const int VK_RCONTROL = 0xA3; public const int VK_RETURN = 0xD; public const int VK_RIGHT = 0x27; public const int VK_RMENU = 0xA5; public const int VK_RSHIFT = 0xA1; public const int VK_SCROLL = 0x91; public const int VK_SELECT = 0x29; public const int VK_SEPARATOR = 0x6C; public const int VK_SHIFT = 0x10; public const int VK_SNAPSHOT = 0x2C; public const int VK_SPACE = 0x20; public const int VK_SUBTRACT = 0x6D; public const int VK_TAB = 0x9; public const int VK_UP = 0x26; public const int VK_ZOOM = 0xFB; #endregion #region Window constants //Window States public const int SW_HIDE = 0; public const int SW_SHOWNORMAL = 1; public const int SW_SHOWMINIMIZED = 2; public const int SW_MAXIMIZE = 3; public const int SW_SHOWMAXIMIZED = 3; public const int SW_SHOWNOACTIVATE = 4; public const int SW_SHOW = 5; public const int SW_MINIMIZE = 6; public const int SW_SHOWMINNOACTIVE = 7; public const int SW_SHOWNA = 8; public const int SW_RESTORE = 9; public const int SW_SHOWDEFAULT = 10; public const int SW_MAX = 10; //Window constants public const string Desktop = "Progman"; public const string TaskBar = "Shell_TrayWnd"; public const int GW_HWNDFIRST = 0; public const int GW_HWNDLAST = 1; public const int GW_HWNDNEXT = 2; public const int GW_HWNDPREV = 3; public const int GW_OWNER = 4; public const int GW_CHILD = 5; #endregion #region Keyboard Events Constants //Keyboard events public const int KEYEVENTF_KEYUP = 0x2; public const int KEYEVENTF_EXTENDEDKEY = 0x1; public const int KEYEVENTF_KEYDOWN = 0; #endregion #region Enumerates /// <summary> /// Hooking type /// </summary> public enum HookType : int { WH_JOURNALRECORD = 0, WH_JOURNALPLAYBACK = 1, WH_KEYBOARD = 2, WH_GETMESSAGE = 3, WH_CALLWNDPROC = 4, WH_CBT = 5, WH_SYSMSGFILTER = 6, WH_MOUSE = 7, WH_HARDWARE = 8, WH_DEBUG = 9, WH_SHELL = 10, WH_FOREGROUNDIDLE = 11, WH_CALLWNDPROCRET = 12, WH_KEYBOARD_LL = 13, WH_MOUSE_LL = 14 } #endregion #region Structures /// <summary> /// System Type Enumerate /// </summary> public struct SYSTEMTIME { public short wYear; public short wMonth; public short wDayOfWeek; public short wDay; public short wHour; public short wMinute; public short wSecond; public short wMilliseconds; } /// <summary> /// Keyboard Hooking Structure /// </summary> public struct KBDLLHOOKSTRUCT { public UInt32 vkCode; public UInt32 scanCode; public UInt32 flags; public UInt32 time; public IntPtr extraInfo; } /// <summary> /// /// </summary> [StructLayout(LayoutKind.Sequential)] public struct SYSTEM_INFO { public uint dwOemId; public uint dwPageSize; public uint lpMinimumApplicationAddress; public uint lpMaximumApplicationAddress; public uint dwActiveProcessorMask; public uint dwNumberOfProcessors; public uint dwProcessorType; public uint dwAllocationGranularity; public uint dwProcessorLevel; public uint dwProcessorRevision; } /// <summary> /// /// </summary> [StructLayout(LayoutKind.Sequential)] struct RECT { public int left; public int top; public int right; public int bottom; } /// <summary> /// /// </summary> public enum APIParam { API_FAILED = 0, API_NULL = 0, API_FALSE = 0, API_TRUE = 1 }; //[StructLayout(LayoutKind.Sequential)] public struct POINTAPI { public int X; public int Y; }; #endregion #region Windows API functions [DllImport("user32.dll")] public static extern Boolean EnumChildWindows(IntPtr hWndParent,Delegate lpEnumFunc,int lParam); [DllImport("user32.dll")] public static extern Int32 FindWindow(String lpClassName,String lpWindowName); [DllImport("user32.dll", EntryPoint="GetDesktopWindow")] public static extern int GetDesktopWindow(); [DllImport("user32.dll")] static extern int GetForegroundWindow(); [DllImport("kernel32")] static extern void GetSystemInfo(ref SYSTEM_INFO pSI); [DllImport("user32.dll")] public static extern Int32 GetWindowText(IntPtr hWnd,StringBuilder s,int nMaxCount); [DllImport("user32.dll")] public static extern Int32 GetWindowTextLength(IntPtr hWnd); [DllImport("winmm.dll")] public static extern long PlaySound(string lpszName, IntPtr hModule, long dwFlags); [DllImport("user32.dll")] public static extern Int32 SetForegroundWindow(IntPtr hWnd); [DllImport("user32.dll")] public static extern int SendMessage(IntPtr hWnd, int msg , int wParam , int[] lParam); [DllImport("user32.dll")] public static extern int SendMessage(IntPtr hWnd, int msg , int wParam , object lParam); [DllImport("user32.dll")] public static extern bool PostMessageA(IntPtr hWnd, int wMsg , int wParam , int lParam); [DllImport("user32.dll", CharSet=CharSet.Auto)] public static extern IntPtr FindWindowEx(IntPtr parent , IntPtr next , string sClassName, IntPtr sWindowTitle); [DllImport("user32.dll")] public static extern IntPtr WindowFromPoint(int xPoint, int yPoint); [DllImport("user32.dll")] public static extern int GetCursorPos (ref POINTAPI lpPoint); [DllImport("kernel32.dll", SetLastError = true)] public static extern bool SetSystemTime([In] ref SYSTEMTIME st); [DllImport("user32.dll")] public static extern int UnhookWindowsHookEx(IntPtr hook); [DllImport("user32.dll")] public static extern int CallNextHookEx( IntPtr hook, int code, IntPtr wParam, ref KBDLLHOOKSTRUCT lParam); #endregion } /// <summary> /// Helper class containing Gdi32 API functions /// </summary> public sealed class GDI32 { public const int SRCCOPY = 0x00CC0020; // BitBlt dwRop parameter [DllImport("gdi32.dll")] public static extern bool BitBlt(IntPtr hObject, int nXDest, int nYDest, int nWidth, int nHeight, IntPtr hObjectSource, int nXSrc, int nYSrc, int dwRop); [DllImport("gdi32.dll")] public static extern IntPtr CreateCompatibleBitmap(IntPtr hDC, int nWidth, int nHeight); [DllImport("gdi32.dll")] public static extern IntPtr CreateCompatibleDC(IntPtr hDC); [DllImport("gdi32.dll")] public static extern bool DeleteDC(IntPtr hDC); [DllImport("gdi32.dll")] public static extern bool DeleteObject(IntPtr hObject); [DllImport("gdi32.dll")] public static extern IntPtr SelectObject(IntPtr hDC, IntPtr hObject); } /// <summary> /// Helper class containing User32 API functions /// </summary> public sealed class User32 { [StructLayout(LayoutKind.Sequential)] public struct RECT { public int left; public int top; public int right; public int bottom; } [DllImport("user32.dll")] public static extern IntPtr GetDesktopWindow(); [DllImport("user32.dll")] public static extern IntPtr GetWindowDC(IntPtr hWnd); [DllImport("user32.dll")] public static extern IntPtr ReleaseDC(IntPtr hWnd, IntPtr hDC); [DllImport("user32.dll")] public static extern IntPtr GetWindowRect(IntPtr hWnd, ref RECT rect); } }