• 0

[C#] attach event to window exit


Question

I launch an application through another application. I can get the window handle of the application, and I want to add an event to it when it is closed.

When it is closed, I want to do something in my applcation... so I'm looking to attach to a window I guess, and when its Exited, throw an event and do something.

Any ideas?

Link to comment
https://www.neowin.net/forum/topic/616922-c-attach-event-to-window-exit/
Share on other sites

7 answers to this question

Recommended Posts

  • 0

Something along the lines of this?

using System.Windows.Forms;

class ClassName : Form
{
	//some class variables, methods, delegates, etc. ?
	ClassName ()
	{
		//some other things?
		this.FormClosing += new FormClosingEventHandler(formClosing);
	}

	void formClosing (object sender, FormClosingEventArgs)
	{
		//If you are using multithreaded programming, make sure to notify the threads that the app is
		//	closing somehow. Otherwise, you'll get exceptions (in the console if compiled as a console app)

		// THROW YOUR EVENT SOMEWHERE IN HERE
	}

	public static void Main (string[] args)
	{
		Application.Run(new ClassName());
	}
}

  • 0

My title and post are unclear...

I basically only have the Window handle, an IntPtr of the application of which I want to attach events to. Whenever this external window is closed, I want an event to be called in my application.

I understand events and the simplicity... .Exited or whatever, but I ONLY have the IntPtr handle to the window.

  • 0

You'll want to hook into Windows and monitor the WM_Close (WM_CLOSE = 0x0010) event and check the handle you have against the handle of the window being closed. You do this by overriding the WndProc event. Or you can set up a system wide hook The following is what I use to set this up. Just copy this into a .cs file (ex. Hook.cs) and create a reference to this in your form.

using System;
using System.ComponentModel;
using System.Runtime.InteropServices;

namespace Hook
{
	public delegate void OnForegroundWindowChangedDelegate(IntPtr hWnd);
	public delegate void OnWindowMinimizeStartDelegate(IntPtr hWnd);
	public delegate void OnWindowMinimizeEndDelegate(IntPtr hWnd);
	public delegate void OnWindowDestroyDelegate(IntPtr hWnd);

	public sealed class Hooks
	{
		#region Windows API

		private enum SystemEvents : uint
		{
			EVENT_SYSTEM_DESTROY = 0x8001,
			EVENT_SYSTEM_MINIMIZESTART = 0x0016,
			EVENT_SYSTEM_MINIMIZEEND = 0x0017,
			EVENT_SYSTEM_FOREGROUND = 0x0003
		}

		private const uint WINEVENT_OUTOFCONTEXT = 0x0000;

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

		[DllImport("User32.dll", SetLastError = true)]
		private static extern IntPtr SetWinEventHook(
			uint eventMin,
			uint eventMax,
			IntPtr hmodWinEventProc,
			WinEventDelegate lpfnWinEventProc,
			uint idProcess,
			uint idThread,
			uint dwFlags);

		[DllImport("user32.dll")]
		private static extern bool UnhookWinEvent(
			IntPtr hWinEventHook
			);

		#endregion

		private WinEventDelegate dEvent;
		private IntPtr pHook;
		public OnForegroundWindowChangedDelegate OnForegroundWindowChanged;
		public OnWindowMinimizeStartDelegate OnWindowMinimizeStart;
		public OnWindowMinimizeEndDelegate OnWindowMinimizeEnd;
		public OnWindowDestroyDelegate OnWindowDestroy;

		public Hooks()
		{
			dEvent = this.WinEvent;
			pHook = SetWinEventHook(
				(uint)SystemEvents.EVENT_SYSTEM_DESTROY ,
				(uint)SystemEvents.EVENT_SYSTEM_DESTROY ,
				IntPtr.Zero,
				dEvent,
				0,
				0,
				WINEVENT_OUTOFCONTEXT
				);

			if (IntPtr.Zero.Equals(pHook)) throw new Win32Exception();

			GC.KeepAlive(dEvent);
		}

		private void WinEvent(IntPtr hWinEventHook, uint eventType, IntPtr hWnd, int idObject, int idChild, uint dwEventThread, uint dwmsEventTime)
		{
			switch (eventType)
			{
				case (uint)SystemEvents.EVENT_SYSTEM_DESTROY:
					if (OnWindowDestroy != null) OnWindowDestroy(hWnd);
					break;

				//extend here when required
			}
		}

		~Hooks()
		{
			if (!IntPtr.Zero.Equals(pHook)) UnhookWinEvent(pHook);
			pHook = IntPtr.Zero;
			dEvent = null;

			OnWindowDestroy = null;
		}
	}
}

Then to use this, insert this into your form:

Hook hook = new Hook();

And then in your form's loading event insert this to set up an event for the hook:

hook.OnWindowDestroy += OnWindowDestroyDelegate(window_Destroyed);

private void window_Destroyed(IntPtr hWnd)
{
	 if (hWnd == {the handle you're looking for})
	 {
		  //Do whatever you need here!
	 }
}

Hope it helps.

  • 0

This is great... one problem... (I'm awful with delegates, I really need a book).

The hook setup gives me an error:

hook.OnWindowDestroy += OnWindowDestroyDelegate(window_Destroyed);

project.OnWindowDestroyDelegate' is a 'type' but is used like a 'variable'

I copied your code exactly, but just changed the namespace to my project.. I think that's correct...

  • 0
  zackiv31 said:
This is great... one problem... (I'm awful with delegates, I really need a book).

The hook setup gives me an error:

hook.OnWindowDestroy += OnWindowDestroyDelegate(window_Destroyed);

project.OnWindowDestroyDelegate' is a 'type' but is used like a 'variable'

I copied your code exactly, but just changed the namespace to my project.. I think that's correct...

Sorry, I forgot to type 'new' in there. The correct line should be:

hook.OnWindowDestroy += new OnWindowDestroyDelegate(window_Destroyed);

  • 0
  zackiv31 said:
This is great... one problem... (I'm awful with delegates, I really need a book).

The hook setup gives me an error:

hook.OnWindowDestroy += OnWindowDestroyDelegate(window_Destroyed);

project.OnWindowDestroyDelegate' is a 'type' but is used like a 'variable'

I copied your code exactly, but just changed the namespace to my project.. I think that's correct...

This is really very helpful post.

One question: I want to handle other message, like WM_PAINT, WM_MOVE, etc. in this case how can I get the message codes?

I tried declaring EVENT_SYSTEM_PAINT = 0x000F but this is not working.

Kindly suggest.

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.
  • Posts

    • Sounds creepy to say the least. Don't need nor want AI having access to my history. They're claiming it to be an "offline" model now, but how can we guarantee they don't go behind our backs and change that?
    • Exactly! Without those fundamentals you've mentioned, Democracy is literally just Demonstration of Crazy, nothing to be proud of in such system.
    • Still I see almost no ads in mobile Edge unlike Chrome. So their browser is much better at blocking ads than Chrome and it is a fact. It even blocks ads on YouTube and you can add simple custom block filters. Also, Edge still support manifest v2 on desktop, so I'll look for another browser when I start seeing ads again.
    • Considering they consistently release a new version every year, I think it makes perfect sense. There is a minor pain point at the time of the change, so as long as they only change it once, we get over it and end up in a better place. The issue with MS' naming scheme is that they change their mind so frequently. 3.1, 95, 98, ME, 2000, XP, Vista, 7, 8, 8.1 10, there is no consistency. That was the issue, not the choice of naming the OS after the year. Minor correction: Windows 7 was 6.1. Even Windows 10 was version 6.4 at launch, but they retroactively changed it to 10.0 early on, along with an announcement that they would no longer track the kerel version and OS version separately...then proceeded to call Windows 11 version 10.0.2, so yeah, MS sucks at naming.
    • I don't think there is any problem with the two-digit number. Even if someone didn't understand the meaning, 26 > 18, so they will still understand at a glance that it is a newer version. The only downside I see is someone assuming 18 is a MUCH older than 26, but IMO, that confusion really isn't a big issue. The truth is that the majority of software companies use the 2-digit number; Microsoft is the outlier, and even they used 2-digit numbers in the past.
  • Recent Achievements

    • First Post
      viraltui earned a badge
      First Post
    • Reacting Well
      viraltui earned a badge
      Reacting Well
    • Week One Done
      LunaFerret earned a badge
      Week One Done
    • Week One Done
      Ricky Chan earned a badge
      Week One Done
    • Week One Done
      maimutza earned a badge
      Week One Done
  • Popular Contributors

    1. 1
      +primortal
      481
    2. 2
      +FloatingFatMan
      264
    3. 3
      snowy owl
      238
    4. 4
      ATLien_0
      230
    5. 5
      Edouard
      176
  • Tell a friend

    Love Neowin? Tell a friend!