Jimi2000

WindowHooks_NativeMethods

Feb 14th, 2018
448
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 11.27 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Runtime.InteropServices;
  5. using System.Security;
  6. using System.Security.Permissions;
  7.  
  8. namespace WindowHooks
  9. {
  10.     public class WinApi
  11.     {
  12.         public enum WinMessage : int
  13.         {
  14.             WM_SETTEXT = 0X000C,
  15.             WM_CLOSE = 0x0010,
  16.             WM_QUIT = 0x12
  17.         }
  18.        
  19.        
  20.         [StructLayout(LayoutKind.Sequential)]
  21.         public struct RECT
  22.         {
  23.             public int Left;
  24.             public int Top;
  25.             public int Right;
  26.             public int Bottom;
  27.         }
  28.  
  29.         public static IntPtr FindWindow(IntPtr hwndParent, IntPtr hwndChildAfter, string lpszClass, string lpszWindow)
  30.         {
  31.             return SafeNativeMethods.FindWindowEx(hwndParent, hwndChildAfter, lpszClass, lpszWindow);
  32.         }
  33.  
  34.         public static int SendMessage(IntPtr hwnd, WinMessage Message, int wParam, int lParam)
  35.         {
  36.             return SafeNativeMethods.SendMessage(hwnd, Message, wParam, lParam);
  37.         }
  38.  
  39.         public static int SendTextMessage(IntPtr hwnd, WinMessage Message, int wParam, string lParam)
  40.         {
  41.             return SafeNativeMethods.SendMessage(hwnd, Message, wParam, lParam);
  42.         }
  43.  
  44.     }
  45.  
  46.     public class Hook
  47.     {
  48.         public static long SWEH_CHILDID_SELF = 0;
  49.  
  50.         public enum SWEH_dwFlags : uint
  51.         {
  52.             WINEVENT_OUTOFCONTEXT = 0x0000,     // Events are ASYNC
  53.             WINEVENT_SKIPOWNTHREAD = 0x0001,    // Don't call back for events on installer's thread
  54.             WINEVENT_SKIPOWNPROCESS = 0x0002,   // Don't call back for events on installer's process
  55.             WINEVENT_INCONTEXT = 0x0004         // Events are SYNC, this causes your dll to be injected into every process
  56.         }
  57.  
  58.         public enum SWEH_Events : uint
  59.         {
  60.             EVENT_MIN = 0x00000001,
  61.             EVENT_MAX = 0x7FFFFFFF,
  62.             EVENT_SYSTEM_SOUND = 0x0001,
  63.             EVENT_SYSTEM_ALERT = 0x0002,
  64.             EVENT_SYSTEM_FOREGROUND = 0x0003,
  65.             EVENT_SYSTEM_MENUSTART = 0x0004,
  66.             EVENT_SYSTEM_MENUEND = 0x0005,
  67.             EVENT_SYSTEM_MENUPOPUPSTART = 0x0006,
  68.             EVENT_SYSTEM_MENUPOPUPEND = 0x0007,
  69.             EVENT_SYSTEM_CAPTURESTART = 0x0008,
  70.             EVENT_SYSTEM_CAPTUREEND = 0x0009,
  71.             EVENT_SYSTEM_MOVESIZESTART = 0x000A,
  72.             EVENT_SYSTEM_MOVESIZEEND = 0x000B,
  73.             EVENT_SYSTEM_CONTEXTHELPSTART = 0x000C,
  74.             EVENT_SYSTEM_CONTEXTHELPEND = 0x000D,
  75.             EVENT_SYSTEM_DRAGDROPSTART = 0x000E,
  76.             EVENT_SYSTEM_DRAGDROPEND = 0x000F,
  77.             EVENT_SYSTEM_DIALOGSTART = 0x0010,
  78.             EVENT_SYSTEM_DIALOGEND = 0x0011,
  79.             EVENT_SYSTEM_SCROLLINGSTART = 0x0012,
  80.             EVENT_SYSTEM_SCROLLINGEND = 0x0013,
  81.             EVENT_SYSTEM_SWITCHSTART = 0x0014,
  82.             EVENT_SYSTEM_SWITCHEND = 0x0015,
  83.             EVENT_SYSTEM_MINIMIZESTART = 0x0016,
  84.             EVENT_SYSTEM_MINIMIZEEND = 0x0017,
  85.             EVENT_SYSTEM_DESKTOPSWITCH = 0x0020,
  86.             EVENT_SYSTEM_END = 0x00FF,
  87.             EVENT_OEM_DEFINED_START = 0x0101,
  88.             EVENT_OEM_DEFINED_END = 0x01FF,
  89.             EVENT_UIA_EVENTID_START = 0x4E00,
  90.             EVENT_UIA_EVENTID_END = 0x4EFF,
  91.             EVENT_UIA_PROPID_START = 0x7500,
  92.             EVENT_UIA_PROPID_END = 0x75FF,
  93.             EVENT_CONSOLE_CARET = 0x4001,
  94.             EVENT_CONSOLE_UPDATE_REGION = 0x4002,
  95.             EVENT_CONSOLE_UPDATE_SIMPLE = 0x4003,
  96.             EVENT_CONSOLE_UPDATE_SCROLL = 0x4004,
  97.             EVENT_CONSOLE_LAYOUT = 0x4005,
  98.             EVENT_CONSOLE_START_APPLICATION = 0x4006,
  99.             EVENT_CONSOLE_END_APPLICATION = 0x4007,
  100.             EVENT_CONSOLE_END = 0x40FF,
  101.             EVENT_OBJECT_CREATE = 0x8000,               // hwnd ID idChild is created item
  102.             EVENT_OBJECT_DESTROY = 0x8001,              // hwnd ID idChild is destroyed item
  103.             EVENT_OBJECT_SHOW = 0x8002,                 // hwnd ID idChild is shown item
  104.             EVENT_OBJECT_HIDE = 0x8003,                 // hwnd ID idChild is hidden item
  105.             EVENT_OBJECT_REORDER = 0x8004,              // hwnd ID idChild is parent of zordering children
  106.             EVENT_OBJECT_FOCUS = 0x8005,                // hwnd ID idChild is focused item
  107.             EVENT_OBJECT_SELECTION = 0x8006,            // hwnd ID idChild is selected item (if only one),
  108.                                                         //      or idChild is OBJID_WINDOW if complex
  109.             EVENT_OBJECT_SELECTIONADD = 0x8007,         // hwnd ID idChild is item added
  110.             EVENT_OBJECT_SELECTIONREMOVE = 0x8008,      // hwnd ID idChild is item removed
  111.             EVENT_OBJECT_SELECTIONWITHIN = 0x8009,      // hwnd ID idChild is parent of changed selected items
  112.             EVENT_OBJECT_STATECHANGE = 0x800A,          // hwnd ID idChild is item w/ state change
  113.             EVENT_OBJECT_LOCATIONCHANGE = 0x800B,       // hwnd ID idChild is moved/sized item
  114.             EVENT_OBJECT_NAMECHANGE = 0x800C,           // hwnd ID idChild is item w/ name change
  115.             EVENT_OBJECT_DESCRIPTIONCHANGE = 0x800D,    // hwnd ID idChild is item w/ desc change
  116.             EVENT_OBJECT_VALUECHANGE = 0x800E,          // hwnd ID idChild is item w/ value change
  117.             EVENT_OBJECT_PARENTCHANGE = 0x800F,         // hwnd ID idChild is item w/ new parent
  118.             EVENT_OBJECT_HELPCHANGE = 0x8010,           // hwnd ID idChild is item w/ help change
  119.             EVENT_OBJECT_DEFACTIONCHANGE = 0x8011,      // hwnd ID idChild is item w/ def action change
  120.             EVENT_OBJECT_ACCELERATORCHANGE = 0x8012,    // hwnd ID idChild is item w/ keybd accel change
  121.             EVENT_OBJECT_INVOKED = 0x8013,              // hwnd ID idChild is item invoked
  122.             EVENT_OBJECT_TEXTSELECTIONCHANGED = 0x8014, // hwnd ID idChild is item w? test selection change
  123.             EVENT_OBJECT_CONTENTSCROLLED = 0x8015,
  124.             EVENT_SYSTEM_ARRANGMENTPREVIEW = 0x8016,
  125.             EVENT_OBJECT_END = 0x80FF,
  126.             EVENT_AIA_START = 0xA000,
  127.             EVENT_AIA_END = 0xAFFF
  128.         }
  129.  
  130.         public enum SWEH_ObjectId : long
  131.         {
  132.              OBJID_WINDOW =             0x00000000,
  133.              OBJID_SYSMENU =            0xFFFFFFFF,
  134.              OBJID_TITLEBAR =           0xFFFFFFFE,
  135.              OBJID_MENU =               0xFFFFFFFD,
  136.              OBJID_CLIENT =             0xFFFFFFFC,
  137.              OBJID_VSCROLL =            0xFFFFFFFB,
  138.              OBJID_HSCROLL =            0xFFFFFFFA,
  139.              OBJID_SIZEGRIP =           0xFFFFFFF9,
  140.              OBJID_CARET =              0xFFFFFFF8,
  141.              OBJID_CURSOR =             0xFFFFFFF7,
  142.              OBJID_ALERT =              0xFFFFFFF6,
  143.              OBJID_SOUND =              0xFFFFFFF5,
  144.              OBJID_QUERYCLASSNAMEIDX =  0xFFFFFFF4,
  145.              OBJID_NATIVEOM =           0xFFFFFFF0
  146.         }
  147.  
  148.         private static SWEH_dwFlags WinEventHookInternalFlags = SWEH_dwFlags.WINEVENT_OUTOFCONTEXT |
  149.                                                                 SWEH_dwFlags.WINEVENT_SKIPOWNPROCESS |
  150.                                                                 SWEH_dwFlags.WINEVENT_SKIPOWNTHREAD;
  151.  
  152.         public delegate void WinEventDelegate(IntPtr hWinEventHook,
  153.                                               SWEH_Events eventType,
  154.                                               IntPtr hwnd,
  155.                                               SWEH_ObjectId idObject,
  156.                                               long idChild,
  157.                                               uint dwEventThread,
  158.                                               uint dwmsEventTime);
  159.  
  160.         public static IntPtr WinEventHookRange(SWEH_Events _eventFrom,
  161.                                                SWEH_Events _eventTo,
  162.                                                WinEventDelegate _delegate,
  163.                                                uint idProcess, uint idThread)
  164.         {
  165.             new UIPermission(UIPermissionWindow.AllWindows).Demand();
  166.             return UnsafeNativeMethods.SetWinEventHook(_eventFrom, _eventTo,
  167.                                                        IntPtr.Zero, _delegate,
  168.                                                        idProcess, idThread,
  169.                                                        WinEventHookInternalFlags);
  170.         }
  171.  
  172.         public static IntPtr WinEventHookOne(SWEH_Events _event, WinEventDelegate _delegate, uint idProcess, uint idThread)
  173.         {
  174.             new UIPermission(UIPermissionWindow.AllWindows).Demand();
  175.             return UnsafeNativeMethods.SetWinEventHook(_event, _event,
  176.                                                        IntPtr.Zero, _delegate,
  177.                                                        idProcess, idThread,
  178.                                                        WinEventHookInternalFlags);
  179.         }
  180.  
  181.         public static bool WinEventUnhook(IntPtr hWinEventHook)
  182.         {
  183.             return UnsafeNativeMethods.UnhookWinEvent(hWinEventHook);
  184.         }
  185.  
  186.         public static uint GetWindowThread(IntPtr hWnd)
  187.         {
  188.             new UIPermission(UIPermissionWindow.AllWindows).Demand();
  189.             return UnsafeNativeMethods.GetWindowThreadProcessId(hWnd, IntPtr.Zero);
  190.         }
  191.  
  192.  
  193.         public static WinApi.RECT GetWindowRect(IntPtr hWnd)
  194.         {
  195.             WinApi.RECT rect = new WinApi.RECT();
  196.             bool _result = SafeNativeMethods.GetWindowRect(hWnd, ref rect);
  197.             return rect;
  198.         }
  199.  
  200.     }
  201.  
  202.     [SuppressUnmanagedCodeSecurityAttribute]
  203.     internal static class SafeNativeMethods
  204.     {
  205.         [DllImport("user32.dll", EntryPoint = "FindWindowEx")]
  206.         public static extern IntPtr FindWindowEx(IntPtr hwndParent,
  207.                                                  IntPtr hwndChildAfter,
  208.                                                  string lpszClass, string lpszWindow);
  209.  
  210.         [DllImport("user32.dll", SetLastError = true)]
  211.         [return: MarshalAs(UnmanagedType.Bool)]
  212.         public static extern bool GetWindowRect(IntPtr hWnd, ref WinApi.RECT lpRect);
  213.  
  214.         [DllImport("User32.dll")]
  215.         public static extern int SendMessage(IntPtr hWnd, WinApi.WinMessage uMsg, int wParam, string lParam);
  216.  
  217.         [DllImport("User32.dll")]
  218.         public static extern int SendMessage(IntPtr hWnd, WinApi.WinMessage uMsg, int wParam, int lParam);
  219.  
  220.     }
  221.  
  222.  
  223.     [SuppressUnmanagedCodeSecurityAttribute]
  224.     internal static class UnsafeNativeMethods
  225.     {
  226.         [DllImport("user32.dll", SetLastError = true)]
  227.         public static extern uint GetWindowThreadProcessId(IntPtr hWnd, out uint lpdwProcessId);
  228.  
  229.         [DllImport("user32.dll")]
  230.         public static extern uint GetWindowThreadProcessId(IntPtr hWnd, IntPtr voidProcessId);
  231.  
  232.         [DllImport("user32.dll", SetLastError = false)]
  233.         public static extern IntPtr SetWinEventHook(Hook.SWEH_Events eventMin, Hook.SWEH_Events eventMax,
  234.                                                     IntPtr hmodWinEventProc, Hook.WinEventDelegate lpfnWinEventProc,
  235.                                                     uint idProcess, uint idThread, Hook.SWEH_dwFlags dwFlags);
  236.  
  237.         [DllImport("user32.dll", SetLastError = false)]
  238.         public static extern bool UnhookWinEvent(IntPtr hWinEventHook);
  239.     }
  240. }
Advertisement
Add Comment
Please, Sign In to add comment