Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8751425
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T13:02:44+00:00 2026-06-13T13:02:44+00:00

I’m trying to get alerted as to when an external application is closed. For

  • 0

I’m trying to get alerted as to when an external application is closed. For my test i’ve retrieved Notepad’s hwnd and set it into my monitor. Strangely, i get the system event whenever i click from notepad onto some other application even though notepad hasn’t closed. Does anyone have any idea why?

some quick example calling code:

uint EVENT_OBJECT_DESTROY = 0x8001;
IntPtr notepadHwnd = <your pointer>
var mon = new WindowMonitor(notepadHwnd, EVENT_OBJECT_DESTROY);
mon.EventOccurred += (sender, args) => Console.WriteLine("closed");

The monitor:

  public class WindowMonitor : IDisposable
  {
    //store delegate to prevent GC
    private User32.WinEventDelegate dEvent;

    private IntPtr _hook;
    private readonly IntPtr _window;
    private readonly List<uint> _watchedEvents = new List<uint>();

    public event EventHandler<AccessibleEventTypeEventArgs> EventOccurred;

    public WindowMonitor(IntPtr windowToMonitor, params User32.AccessibleEventType[] eventsToMonitor)
    {
      //prevent junk
      if (eventsToMonitor == null || eventsToMonitor.Length == 0)
        throw new ArgumentNullException("eventsToMonitor", "Must specify events to monitor");
      if (windowToMonitor == IntPtr.Zero)
        throw new ArgumentNullException("windowToMonitor", "Must specify a valid window handle to monitor");

      _window = windowToMonitor;

      //cast them now so we dont have to cast each one when evaluting events
      uint lowest = (uint) User32.AccessibleEventType.EVENT_MAX;
      uint highest = (uint) User32.AccessibleEventType.EVENT_MIN;
      foreach (User32.AccessibleEventType eventType in eventsToMonitor)
      {
        var castType = (uint) eventType;
        _watchedEvents.Add(castType);

        //need the range of events to subscribe to
        if (castType > highest)
          highest = castType;
        if (castType < lowest)
          lowest = castType;
      }

      //sign up for the event
      dEvent = this.winEvent;
      _hook = User32.SetWinEventHook(lowest, highest, IntPtr.Zero, dEvent, 0, 0, User32.WINEVENT_OUTOFCONTEXT);

      //ensure it worked
      if (IntPtr.Zero.Equals(_hook)) throw new Win32Exception();

      //no kill callback
      GC.KeepAlive(dEvent);
    }

    private void winEvent(IntPtr hWinEventHook, uint eventType, IntPtr hWnd, int idObject, int idChild, uint dwEventThread, uint dwmsEventTime)
    {
      //only care about the events for the specified window
      if (hWnd != _window)
        return;

      if (_watchedEvents.Contains(eventType))
      {
        if (EventOccurred != null)
          EventOccurred(this, new AccessibleEventTypeEventArgs((User32.AccessibleEventType) eventType));
      }
    }

    public void Dispose()
    {
      //unhook the listener
      if (!IntPtr.Zero.Equals(_hook))
        User32.UnhookWinEvent(_hook);

      //clear variables
      _hook = IntPtr.Zero;
      dEvent = null;

      //kill any event listeners
      EventOccurred = null;

      GC.SuppressFinalize(this);
    }
  }

and some winapi (i omitted the list of event consts):

public delegate void WinEventDelegate(IntPtr hWinEventHook, uint eventType, IntPtr hwnd,
  int idObject, int idChild, uint dwEventThread, uint dwmsEventTime);

[DllImport("user32.dll")]
public static extern IntPtr SetWinEventHook(uint eventMin, uint eventMax, IntPtr hmodWinEventProc,
  WinEventDelegate lpfnWinEventProc, uint idProcess, uint idThread, uint dwFlags);

[DllImport("user32.dll")]
public static extern bool UnhookWinEvent(IntPtr hWinEventHook);
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T13:02:45+00:00Added an answer on June 13, 2026 at 1:02 pm

    You’re probably seeing an event for the caret. When you get a winevent, you usually need to check the HWND and the idObject to make sure it’s actually an event about the part of the window you care about; whether the window itself (OBJID_WINDOW) or some other aspect of it, such as scrollbars, caret, or something else.

    For debugging winevents, check out the Accessible Event Watcher tool – configure it to listen to WinEvents via the Mode menu, then in Mode/Settings… select the events you want – eg. EVENT_OBJECT_DESTROY, and then the event information to display, usually the hwnd, idObject and idChild.

    —

    By the way, one comment on your code: if you are only looking for events from a specific window, it’s more efficient to get that window’s thread (GetWindowThreadProcessId), and then use SetWinEventHook with that idThread to only get events from that specific thread instead of passing 0 to listen to all threads; this allows Win32 will do the filtering for you at the event source, which is more efficient than getting events of that type from all threads on the entire desktop and then just ignoring the ones you didn’t need.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
Basically, what I'm trying to create is a page of div tags, each has
I am trying to understand how to use SyndicationItem to display feed which is
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
this is what i have right now Drawing an RSS feed into the php,
I am trying to render a haml file in a javascript response like so:
I have a French site that I want to parse, but am running into
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I'm trying to select an H1 element which is the second-child in its group
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.