private void btc_action_Click(object sender, EventArgs e) { if (hWinEventHook == IntPtr.Zero) { if (listener == null) { listener = new WinEventProc(EventCallback); } List <Process> processes_find = findProcessesByName(txt_process_name.Text); if (processes_find.Count != 1) { MessageBox.Show("Processes found: " + processes_find.Count); return; } hWinEventHook = SetWinEventHook(EVENT_SYSTEM_FOREGROUND, EVENT_SYSTEM_CONTEXTHELPEND, IntPtr.Zero, listener, processes_find[0].Id, 0, SetWinEventHookFlags.WINEVENT_OUTOFCONTEXT | SetWinEventHookFlags.WINEVENT_SKIPOWNPROCESS); txt_log.Text = ""; btn_action.Text = "Stop"; } else { UnhookWinEvent(hWinEventHook); hWinEventHook = IntPtr.Zero; btn_action.Text = "Start"; } }
internal static extern IntPtr SetWinEventHook( AccessibleEvents eventMin, //Specifies the event constant for the //lowest event value in the range of events that are //handled by the hook function. This parameter can //be set to EVENT_MIN to indicate the //lowest possible event value. AccessibleEvents eventMax, //Specifies the event constant for the highest event //value in the range of events that are handled //by the hook function. This parameter can be set //to EVENT_MAX to indicate the highest possible //event value. IntPtr eventHookAssemblyHandle, //Handle to the DLL that contains the hook //function at lpfnWinEventProc, if the //WINEVENT_INCONTEXT flag is specified in the //dwFlags parameter. If the hook function is not //located in a DLL, or if the WINEVENT_OUTOFCONTEXT //flag is specified, this parameter is NULL. WinEventProc eventHookHandle, //Pointer to the event hook function. //For more information about this function uint processId, //Specifies the ID of the process from which the //hook function receives events. Specify zero (0) //to receive events from all processes on the //current desktop. uint threadId, //Specifies the ID of the thread from which the //hook function receives events. //If this parameter is zero, the hook function is //associated with all existing threads on the //current desktop. SetWinEventHookParameter parameterFlags //Flag values that specify the location //of the hook function and of the events to be //skipped. The following flags are valid: );
public static void SubscribeToWindowEvents() { procHandler = new WinEventProc(WindowEventCallback); //logger.Info("Subscribe - Before if"); if (windowEventHook == IntPtr.Zero) { windowEventHook = SetWinEventHook( EVENT_OBJECT_FOCUS, // eventMin EVENT_OBJECT_VALUECHANGE, // eventMax IntPtr.Zero, // hmodWinEventProc procHandler, // lpfnWinEventProc 0, // idProcess 0, // idThread WINEVENT_SKIPOWNPROCESS); //logger.Info("Subscribe - Inside if"); if (windowEventHook == IntPtr.Zero) { throw new Win32Exception(Marshal.GetLastWin32Error()); } } //logger.Info("Subscribe - after if"); }
private void StartListeningForWindowResize() { // Resize event resize_listener = new WinEventProc(resize_EventCallback); //setting the window hook resize_winHook = SetWinEventHook(EVENT_SYSTEM_MOVESIZEEND, EVENT_SYSTEM_MOVESIZEEND, IntPtr.Zero, resize_listener, 0, 0, WINEVENT_OUTOFCONTEXT); }
public MainWindow() { // Used to detect when the user swaps window to stop music if they request it foregroundWindowChangedListener = new WinEventProc(onForegroundWindowChanged); SetWinEventHook(3, 3, IntPtr.Zero, foregroundWindowChangedListener, 0, 0, 0); InitializeComponent(); string version = "4.5"; UpdateChecker.CheckForUpdate(version); if (Properties.Settings.Default.StartMinimized) // check if we should minimize { WindowState = WindowState.Minimized; } // Load the saved values into the UI LaunchMinimized.IsChecked = Properties.Settings.Default.StartMinimized; // set the LaunchMinimized checkbox LaunchOnStartup.IsChecked = Properties.Settings.Default.Startup; // set the LaunchOnStarup checkbox PlayInBackground.IsChecked = Properties.Settings.Default.PlayInBackground; // set the Obscure Volume.Value = Properties.Settings.Default.Volume; SetTextOfRichTextBox(MainMenuPathBox, Path.GetFileName(Properties.Settings.Default.MainMenu)); // Set the text of MainMenuPathBox to the path of the Menu music SetTextOfRichTextBox(VictoryPathBox, Path.GetFileName(Properties.Settings.Default.VictoryMusic)); // Set the text of VictoryPathBox to the path of the Victory music SetTextOfRichTextBox(InGamePathBox, Path.GetFileName(Properties.Settings.Default.InGameMusic)); // Set the text of InGamePathBox to the path of the In game music }
private void StartListeningForWindowSwitch() { // Switch event switch_listener = new WinEventProc(switch_EventCallback); //setting the window hook switch_winHook = SetWinEventHook(EVENT_SYSTEM_FOREGROUND, EVENT_SYSTEM_FOREGROUND, IntPtr.Zero, switch_listener, 0, 0, WINEVENT_OUTOFCONTEXT); }
private bool HookInternal(uint processId, uint threadId) { if (Hooked) { throw new InvalidOperationException("Hook is already hooked."); } var eventProc = new WinEventProc(OnWinEventProc); _eventProcHandle = GCHandle.Alloc(eventProc); _hookHandle = SetWinEventHook( eventMin: EventMin, eventMax: EventMax, hmodWinEventProc: IntPtr.Zero, lpfnWinEventProc: eventProc, idProcess: processId, idThread: threadId, dwFlags: WinEventHookFlags.WINEVENT_OUTOFCONTEXT | WinEventHookFlags.WINEVENT_SKIPOWNPROCESS ); Hooked = _hookHandle != IntPtr.Zero; if (!Hooked) { throw new Win32Exception(); } return(Hooked); }
public MainWindow() { logFileReader = new LogFileReader(getLogFilePath()); // windows event hooking for getting when the foreground window changes foregroundWindowChangedListener = new WinEventProc(onForegroundWindowChanged); foregroundWindowChangeHook = SetWinEventHook(3, 3, IntPtr.Zero, foregroundWindowChangedListener, 0, 0, 0); InitializeComponent(); string version = "4.3"; UpdateChecker.CheckForUpdate(version); if (Properties.Settings.Default.StartMinimized) // check if we should minimize { WindowState = WindowState.Minimized; } LaunchMinimized.IsChecked = Properties.Settings.Default.StartMinimized; // set the LaunchMinimized checkbox LaunchOnStartup.IsChecked = Properties.Settings.Default.Startup; // set the LaunchOnStarup checkbox PlayInBackground.IsChecked = Properties.Settings.Default.PlayInBackground; // set the Obscure Volume.Value = Properties.Settings.Default.Volume; SetTextOfRichTextBox(TitleMenuPathBox, Path.GetFileName(Properties.Settings.Default.TitleMenu)); // Set the text of TitleMenuPathBox to the path of the Title music SetTextOfRichTextBox(MainMenuPathBox, Path.GetFileName(Properties.Settings.Default.MainMenu)); // Set the text of MainMenuPathBox to the path of the Menu music SetTextOfRichTextBox(VictoryPathBox, Path.GetFileName(Properties.Settings.Default.VictoryMusic)); // Set the text of VictoryPathBox to the path of the Victory music }
internal void Initialize() { if (IsInitialized) { return; } try { CairoLogger.Instance.Debug("Starting WindowsTasksService"); // create window to receive task events _HookWin = new NativeWindowEx(); _HookWin.CreateHandle(new CreateParams()); // prevent other shells from working properly SetTaskmanWindow(_HookWin.Handle); // register to receive task events RegisterShellHookWindow(_HookWin.Handle); WM_SHELLHOOKMESSAGE = RegisterWindowMessage("SHELLHOOK"); WM_TASKBARCREATEDMESSAGE = RegisterWindowMessage("TaskbarCreated"); TASKBARBUTTONCREATEDMESSAGE = RegisterWindowMessage("TaskbarButtonCreated"); _HookWin.MessageReceived += ShellWinProc; if (Shell.IsWindows8OrBetter) { // set event hook for uncloak events uncloakEventProc = UncloakEventCallback; if (uncloakEventHook == IntPtr.Zero) { uncloakEventHook = SetWinEventHook( EVENT_OBJECT_UNCLOAKED, EVENT_OBJECT_UNCLOAKED, IntPtr.Zero, uncloakEventProc, 0, 0, WINEVENT_OUTOFCONTEXT | WINEVENT_SKIPOWNPROCESS); } } // set window for ITaskbarList setTaskbarListHwnd(); // adjust minimize animation SetMinimizedMetrics(); // enumerate windows already opened and set active window getInitialWindows(); IsInitialized = true; } catch (Exception ex) { CairoLogger.Instance.Info("Unable to start WindowsTasksService: " + ex.Message); } }
public void StartListening() { _listener = new WinEventProc(EventCallback); _winHook = SetWinEventHook(EVENT_SYSTEM_FOREGROUND, EVENT_SYSTEM_MINIMIZEEND, IntPtr.Zero, _listener, 0, 0, WINEVENT_OUTOFCONTEXT); Console.WriteLine("--- Tracking Started!"); }
//private HookProc myCallbackDelegate = null; public void StartListeningForWindowChanges() { listener = new WinEventProc(EventCallback); //setting the window hook winHook = SetWinEventHook(EVENT_SYSTEM_FOREGROUND, EVENT_SYSTEM_FOREGROUND, IntPtr.Zero, listener, Process.Id, 0, WINEVENT_OUTOFCONTEXT | WINEVENT_SKIPOWNPROCESS | WINEVENT_SKIPOWNTHREAD); //this.myCallbackDelegate = new HookProc(this.MyCallbackFunction); //winHookAcived = SetWindowsHookEx(HookType.WH_SHELL, myCallbackDelegate, IntPtr.Zero, IntPtr.Zero); }
public ForegroundWindowMonitor(ISetForegroundWindow foregroundWindowImpl) { _foregroundWindowImpl = foregroundWindowImpl; _listener = EventCallback; _handle = SetWinEventHook(3, 3, IntPtr.Zero, _listener, 0, 0, SetWinEventHookFlags.WINEVENT_SKIPOWNPROCESS); _foregroundTimer = new Timer(); _foregroundTimer.AutoReset = false; _foregroundTimer.Interval = 10.0; _foregroundTimer.Elapsed += _foregroundTimer_Elapsed; }
public WindowObserver(IntPtr windowHandle, Action <int> callback) { _windowHandle = windowHandle; _callback = callback; _eventListener = new WinEventProc(WindowEventCallback); int processId; var threadId = GetWindowThreadProcessId(windowHandle, out processId); _eventHook = SetWinEventHook(1, 0x7fffffff, IntPtr.Zero, _eventListener, processId, threadId, SetWinEventHookFlags.WINEVENT_OUTOFCONTEXT); Debug.WriteLine("hooked to window: " + _eventHook); }
internal IntPtr SetWinHook(WinEventProc callBack) { using (Process currentProcess = Process.GetCurrentProcess()) using (ProcessModule currentModule = currentProcess.MainModule) { return(SetWinEventHook( WinEventTypes.EVENT_SYSTEM_FOREGROUND, WinEventTypes.EVENT_SYSTEM_FOREGROUND, GetModuleHandle(currentModule.ModuleName), callBack, 0, 0, WinEventTypes.WINEVENT_OUTOFCONTEXT)); } }
public void StartListeningForWindowChanges() { _foregroundListener = HandleForegroundEvent; _moveSizeStartListener = HandleMoveSizeStartEvent; _moveSizeEndListener = HandleMoveSizeEndEvent; SetWinEventHook(EVENT_SYSTEM_FOREGROUND, EVENT_SYSTEM_FOREGROUND, IntPtr.Zero, _foregroundListener, 0, 0, WINEVENT_OUTOFCONTEXT | WINEVENT_SKIPOWNPROCESS); SetWinEventHook(EVENT_SYSTEM_MOVESIZESTART, EVENT_SYSTEM_MOVESIZESTART, IntPtr.Zero, _moveSizeStartListener, 0, 0, WINEVENT_OUTOFCONTEXT | WINEVENT_SKIPOWNPROCESS); SetWinEventHook(EVENT_SYSTEM_MOVESIZEEND, EVENT_SYSTEM_MOVESIZEEND, IntPtr.Zero, _moveSizeEndListener, 0, 0, WINEVENT_OUTOFCONTEXT | WINEVENT_SKIPOWNPROCESS); }
public void StartListeningForWindowChanges() { try { listener = new WinEventProc(EventCallback); //setting the window hook winHook = SetWinEventHook(EVENT_SYSTEM_FOREGROUND, EVENT_SYSTEM_FOREGROUND, IntPtr.Zero, listener, 0, 0, WINEVENT_OUTOFCONTEXT); } catch (Exception) { //Ignore } }
protected void CreateWinEventHook(int listenEvent, WinEventProc callback) { const int flags = WINEVENT_OUTOFCONTEXT | WINEVENT_SKIPOWNPROCESS; //To prevent garbage collection on children before this base class UnHooks this._hookCallback = callback; this._windowEventHook = SetWinEventHook(listenEvent, listenEvent, IntPtr.Zero, this._hookCallback, 0, 0, flags); if (this._windowEventHook == IntPtr.Zero) { throw new Exception(Marshal.GetLastWin32Error().ToString()); } }
public void SetHook(string windowName) { if (hHook != null && !hHook.IsInvalid && !hHook.IsClosed) { hHook.Close(); callback = null; } callback = WinEventProc(windowName); hHook = SetWinEventHook(WindowsEventHookType.EVENT_SYSTEM_FOREGROUND, WindowsEventHookType.EVENT_OBJECT_LOCATIONCHANGE, IntPtr.Zero, callback, 0, 0, WindowsEventHookFlags.WINEVENT_OUTOFCONTEXT | WindowsEventHookFlags.WINEVENT_SKIPOWNPROCESS); var err = Marshal.GetLastWin32Error(); if (hHook == null || hHook.IsInvalid) { System.Console.WriteLine("Hook set failed..."); System.Console.WriteLine($"error code: {err}"); } }
public SystemMenuUtilities(SpotifyInstance instance) { _proc = new WinEventProc(HookHandler); _mainWindowHandle = instance.MainWindowHandle; User32.GetSystemMenu(_mainWindowHandle, bRevert: true); _systemMenuHandle = User32.GetSystemMenu(_mainWindowHandle, bRevert: false); int processId; var threadId = User32.GetWindowThreadProcessId(_mainWindowHandle, out processId); var handle = SetWinEventHook(User32.WindowsEventHookType.EVENT_OBJECT_INVOKED, User32.WindowsEventHookType.EVENT_OBJECT_INVOKED, IntPtr.Zero, _proc, processId, threadId, User32.WindowsEventHookFlags.WINEVENT_OUTOFCONTEXT); _windowHook = new User32.SafeEventHookHandle(handle); AddMenuItem("Hello!", () => MessageBox.Show("HI!"), precedeWithSeparator: true); }
public void Hook() { if (hookId == IntPtr.Zero) { proc = HookProcedure; hookId = SetWinEventHook( EventMapConverter.NameToCode(EventName.EVENT_MIN), // eventMin EventMapConverter.NameToCode(EventName.EVENT_MAX), // eventMax IntPtr.Zero, // hmodWinEventProc proc, // lpfnWinEventProc 0, // idProcess 0, // idThread WINEVENT_OUTOFCONTEXT | WINEVENT_SKIPOWNPROCESS); if (hookId == IntPtr.Zero) { throw new Win32Exception(Marshal.GetLastWin32Error()); } } }
private static void SubscribeToWindowEvents() { eventProc = new WinEventProc(PTManager.WindowEventCallback); if (windowEventHook == IntPtr.Zero) { windowEventHook = SetWinEventHook( EVENT_SYSTEM_FOREGROUND, // eventMin EVENT_SYSTEM_FOREGROUND, // eventMax IntPtr.Zero, // hmodWinEventProc eventProc, // lpfnWinEventProc 0, // idProcess 0, // idThread WINEVENT_OUTOFCONTEXT | WINEVENT_SKIPOWNPROCESS); if (windowEventHook == IntPtr.Zero) { throw new Win32Exception(Marshal.GetLastWin32Error()); } } }
public static void Start() { if (Interlocked.Increment(ref _count) == 1) { if (_winEventHook == IntPtr.Zero) { _winEventHookCallback = WinEventHookCallback; _winEventHook = SetWinEventHook( EVENT_SYSTEM_FOREGROUND, // eventMin EVENT_SYSTEM_FOREGROUND, // eventMax IntPtr.Zero, // winEventProc _winEventHookCallback, // winEventCallback 0, // idProcess 0, // idThread WINEVENT_OUTOFCONTEXT); if (_winEventHook == IntPtr.Zero) { throw new Win32Exception(Marshal.GetLastWin32Error()); } } } }
private static extern IntPtr SetWinEventHook(UInt32 eventMin, UInt32 eventMax, IntPtr hmodWinEventProc, WinEventProc lpfnWinEventProc, Int32 idProcess, Int32 idThread, UInt32 dwflags);
internal static extern IntPtr SetWinEventHook(int eventMin, int eventMax, IntPtr hmodWinEventProc, WinEventProc lpfnWinEventProc, int idProcess, int idThread, SetWinEventHookFlags dwflags);
private static extern IntPtr SetWinEventHook( int eventMin, int eventMax, IntPtr callbackProcess, WinEventProc callback, int idProcess, int idThread, int flags );
public static extern System.IntPtr SetWinEventHook(uint eventMin, uint eventMax, [In] IntPtr hmodWinEventProc, WinEventProc pfnWinEventProc, uint idProcess, uint idThread, uint dwFlags);
internal static extern IntPtr SetWinEventHook(uint eventMin, uint eventMax, IntPtr hmodWinEventProc, WinEventProc lpfnWinEventProc, int idProcess, int idThread, uint dwflags); // Used to see when the currently foreground window changes
public static extern IntPtr SetWinEventHook(Enums.AccessibleEvents eventMin, Enums.AccessibleEvents eventMax, IntPtr eventHookAssemblyHandle, WinEventProc eventHookHandle, uint processId, uint threadId, Enums.SetWinEventHookParameter parameterFlags);
internal static extern IntPtr SetWinEventHook(AccessibleEvents eventMin, AccessibleEvents eventMax, IntPtr eventHookAssemblyHandle, WinEventProc eventHookHandle, uint processId, uint threadId, int parameterFlags);
internal static extern IntPtr SetWinEventHook(AccessibleEvents eventMin, //Specifies the event constant for the lowest event value in the range of events that are handled by the hook function. This parameter can be set to EVENT_MIN to indicate the lowest possible event value. AccessibleEvents eventMax, //Specifies the event constant for the highest event value in the range of events that are handled by the hook function. This parameter can be set to EVENT_MAX to indicate the highest possible event value. IntPtr eventHookAssemblyHandle, //Handle to the DLL that contains the hook function at lpfnWinEventProc, if the WINEVENT_INCONTEXT flag is specified in the dwFlags parameter. If the hook function is not located in a DLL, or if the WINEVENT_OUTOFCONTEXT flag is specified, this parameter is NULL. WinEventProc eventHookHandle, //Pointer to the event hook function. For more information about this function uint processId, //Specifies the ID of the process from which the hook function receives events. Specify zero (0) to receive events from all processes on the current desktop. uint threadId, //Specifies the ID of the thread from which the hook function receives events. If this parameter is zero, the hook function is associated with all existing threads on the current desktop. SetWinEventHookParameter parameterFlags //Flag values that specify the location of the hook function and of the events to be skipped. The following flags are valid: );
private static extern IntPtr SetWinEventHook(int eventMin, int eventMax, IntPtr hmodWinEventProc, WinEventProc lpfnWinEventProc, int idProcess, int idThread, int dwflags);
private static extern IntPtr SetWinEventHook(int eventMin, int eventMax, IntPtr winEventProc, WinEventProc winEventCallback, int idProcess, int idThread, int dwflags);
public WindowHook(string _targetProcessName) { TargetProcessName = _targetProcessName; WinEventListener = new WinEventProc(WinEventProcCallback); ForegroundEventHook = User32.SetWinEventHook(User32.SWEH_Events.EVENT_SYSTEM_FOREGROUND, User32.SWEH_Events.EVENT_SYSTEM_FOREGROUND, IntPtr.Zero, WinEventListener, 0, 0, User32.SWEH_dwFlags.WINEVENT_OUTOFCONTEXT | User32.SWEH_dwFlags.WINEVENT_SKIPOWNPROCESS); }
public static extern SafeEventHookHandle SetWinEventHook( WindowsEventHookType eventMin, WindowsEventHookType eventMax, IntPtr hmodWinEventProc, WinEventProc lpfnWinEventProc, int idProcess, int idThread, WindowsEventHookFlags dwflags);
public static extern IntPtr SetWinEventHook( EVENT_SYSTEM eventMin, EVENT_SYSTEM eventMax, IntPtr hmodWinEventProc, WinEventProc lpfnWinEventProc, uint idProcess, uint idThread, WINEVENT dwFlags);
private void initialize() { try { CairoLogger.Instance.Debug("Starting WindowsTasksService"); // create window to receive task events _HookWin = new NativeWindowEx(); _HookWin.CreateHandle(new CreateParams()); // prevent other shells from working properly SetTaskmanWindow(_HookWin.Handle); // register to receive task events RegisterShellHookWindow(_HookWin.Handle); WM_SHELLHOOKMESSAGE = RegisterWindowMessage("SHELLHOOK"); WM_TASKBARCREATEDMESSAGE = RegisterWindowMessage("TaskbarCreated"); TASKBARBUTTONCREATEDMESSAGE = RegisterWindowMessage("TaskbarButtonCreated"); _HookWin.MessageReceived += ShellWinProc; if (Interop.Shell.IsWindows8OrBetter) { // set event hook for uncloak events uncloakEventProc = UncloakEventCallback; if (uncloakEventHook == IntPtr.Zero) { uncloakEventHook = SetWinEventHook( EVENT_OBJECT_UNCLOAKED, EVENT_OBJECT_UNCLOAKED, IntPtr.Zero, uncloakEventProc, 0, 0, WINEVENT_OUTOFCONTEXT | WINEVENT_SKIPOWNPROCESS); } } // set window for ITaskbarList setTaskbarListHwnd(); // adjust minimize animation SetMinimizedMetrics(); // prepare collections groupedWindows = CollectionViewSource.GetDefaultView(Windows); groupedWindows.GroupDescriptions.Add(new PropertyGroupDescription("Category")); groupedWindows.CollectionChanged += groupedWindows_Changed; groupedWindows.Filter = groupedWindows_Filter; if (groupedWindows is ICollectionViewLiveShaping taskbarItemsView) { taskbarItemsView.IsLiveFiltering = true; taskbarItemsView.LiveFilteringProperties.Add("ShowInTaskbar"); taskbarItemsView.IsLiveGrouping = true; taskbarItemsView.LiveGroupingProperties.Add("Category"); } // enumerate windows already opened EnumWindows(new CallBackPtr((hwnd, lParam) => { ApplicationWindow win = new ApplicationWindow(hwnd, this); if (win.CanAddToTaskbar && win.ShowInTaskbar && !Windows.Contains(win)) { Windows.Add(win); } return(true); }), 0); // register for app grabber changes so that our app association is accurate AppGrabber.AppGrabber.Instance.CategoryList.CategoryChanged += CategoryList_CategoryChanged; } catch (Exception ex) { CairoLogger.Instance.Info("Unable to start WindowsTasksService: " + ex.Message); } }
public static extern IntPtr SetWinEventHook(uint eventMin, uint eventMax, IntPtr hmodWinEventProc, WinEventProc lpfnWinEventProc, uint idProcess, uint idThread, WinEventFlags dwFlags);
static extern IntPtr SetWinEventHook(uint eventMin, uint eventMax, IntPtr hmodWinEventProc, WinEventProc lpfnWinEventProc, uint idProcess, uint idThread, uint dwFlags);
public static void Init() { lastTime = DateTime.Now; listener = new WinEventProc(EventCallback); int threadId = 0x000306D4; int processId = 0x000306BC; //setting the window hook and writing the result to the console var i = SetWinEventHook(0x0003, 0x0003, IntPtr.Zero, listener, 0, 0, SetWinEventHookFlags.WINEVENT_OUTOFCONTEXT); }