Sibber.WindowMessageMonitor 5.0.0

Prefix Reserved
dotnet add package Sibber.WindowMessageMonitor --version 5.0.0
                    
NuGet\Install-Package Sibber.WindowMessageMonitor -Version 5.0.0
                    
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="Sibber.WindowMessageMonitor" Version="5.0.0" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="Sibber.WindowMessageMonitor" Version="5.0.0" />
                    
Directory.Packages.props
<PackageReference Include="Sibber.WindowMessageMonitor" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add Sibber.WindowMessageMonitor --version 5.0.0
                    
#r "nuget: Sibber.WindowMessageMonitor, 5.0.0"
                    
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package Sibber.WindowMessageMonitor@5.0.0
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=Sibber.WindowMessageMonitor&version=5.0.0
                    
Install as a Cake Addin
#tool nuget:?package=Sibber.WindowMessageMonitor&version=5.0.0
                    
Install as a Cake Tool

Sibber.WindowMessageMonitor

NuGet Version License

A class that lets you listen to window messages for a given window, forked from dotMorten/WinUIEx.Messaging.WindowMessageMonitor.

Documentation

See full documentation and API reference at sibber5.github.io/Sibber.WindowMessageMonitor.

Usage

You must create the WindowMessageMonitor on the same thread as the window it would monitor was created on (for CreateWithMessageOnlyWindow, it must be created on a thread with a message pump).
Disposal, however, is thread-safe and can be done from any thread. Make sure to dispose of it when you are done using it, not doing so may cause problems such as memory leaks.

On .NET 8+, this library is compatible with trimming and Native AOT.

If you have an existing window:

var monitor = new WindowMessageMonitor(windowHandle);
monitor.WindowMessageReceived += (object sender, ref WindowMessageEventArgs e) =>
{
    Debug.WriteLine($"Received message: {e.MessageType} with wParam: {e.Message.WParam} and LParam: {e.Message.LParam}");

    // note that `e` is a mutable struct that is passed by reference.
    e.Handled = true;
    e.Result = 1;
}

Or you can create a message-only window:

var monitor = WindowMessageMonitor.CreateWithMessageOnlyWindow();

Make sure to dispose when you're done using the instance:

monitor.Dispose();

Or use monitor.Free() which does the exact same thing as Dispose() but throws if an error occurs, so you can log it.

Subscribers to WindowMessageReceived should catch and handle all exceptions. If that is not possible, you can subscribe to WindowMessageException to handle the thrown exceptions, which would otherwise be swallowed.

The interface IWindowMessageMonitor is also provided in case you want to provide custom implementations, useful for things like other libraries that use this library.

License

The file src/WindowMessageMonitor.cs was taken from dotMorten/WinUIEx, MIT License - Copyright (c) 2021 Morten Nielsen. See the license notice at the top of the file for more info.

The rest of the library/repository is licensed under the MIT License - see LICENSE - unless otherwise stated in specific files or sections. See individual files for exceptions.

Product Compatible and additional computed target framework versions.
.NET net5.0 was computed.  net5.0-windows was computed.  net6.0 was computed.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 was computed.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 was computed.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed.  net8.0-windows7.0 is compatible.  net9.0 was computed.  net9.0-android was computed.  net9.0-browser was computed.  net9.0-ios was computed.  net9.0-maccatalyst was computed.  net9.0-macos was computed.  net9.0-tvos was computed.  net9.0-windows was computed.  net10.0 was computed.  net10.0-android was computed.  net10.0-browser was computed.  net10.0-ios was computed.  net10.0-maccatalyst was computed.  net10.0-macos was computed.  net10.0-tvos was computed.  net10.0-windows was computed. 
.NET Core netcoreapp2.0 was computed.  netcoreapp2.1 was computed.  netcoreapp2.2 was computed.  netcoreapp3.0 was computed.  netcoreapp3.1 was computed. 
.NET Standard netstandard2.0 is compatible.  netstandard2.1 was computed. 
.NET Framework net461 was computed.  net462 was computed.  net463 was computed.  net47 was computed.  net471 was computed.  net472 was computed.  net48 was computed.  net481 was computed. 
MonoAndroid monoandroid was computed. 
MonoMac monomac was computed. 
MonoTouch monotouch was computed. 
Tizen tizen40 was computed.  tizen60 was computed. 
Xamarin.iOS xamarinios was computed. 
Xamarin.Mac xamarinmac was computed. 
Xamarin.TVOS xamarintvos was computed. 
Xamarin.WatchOS xamarinwatchos was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
5.0.0 114 6/1/2026
4.0.0 194 10/12/2025
3.0.0 267 6/8/2025
2.0.5 175 6/7/2025
2.0.4 199 5/8/2025
2.0.3 202 5/8/2025
2.0.2 219 5/7/2025
2.0.1 215 5/4/2025
2.0.0 211 5/4/2025
1.2.1 175 5/3/2025
1.2.0 165 5/3/2025
1.1.0 138 5/3/2025
1.0.1 214 5/1/2025
1.0.0 220 5/1/2025

### API Enhancements & Breaking Changes
* **Breaking**: Changed `WindowMessageEventArgs` from a readonly struct to a standard mutable record struct. You can now set `Handled` and `Result` directly by reference.
* **Breaking**: The `WindowMessageMonitor` constructor now throws an `InvalidOperationException` (instead of `Win32Exception`) if called on a different thread than the one that created the target window.
* Added the `WindowMessageException` event. This catches unhandled exceptions thrown by your `WindowMessageReceived` subscribers and exposes them via `WindowMessageExceptionEventArgs` to prevent native process crashes. This should not be normally used, as `WindowMessageReceived` should not throw.
* `Dispose()` and `Free()` can now be safely called from any thread. Cross-thread calls defer teardown to the UI thread via `WM_NCDESTROY` or a custom `WM_UNHOOKSUBCLASS` message.

### Bug Fixes
* Fixed an edge case causing `GCHandle` and native hook leaks during window teardown by unconditionally intercepting `WM_NCDESTROY` to clean up, even if previous messages were marked as handled.
* Fixed a rare `CallbackOnCollectedDelegate` crash for message-only windows by pinning the subclass delegate and tearing it down via `WM_CLOSE`.
* Fixed a `Win32Exception` in `Free()` that could occur when trying to unhook from a window that was already naturally closed.
* Fixed a possible race condition in the disposal loop.
* Added immediate `DestroyWindow` cleanup if subclass initialization fails during monitor setup.
* Fixed crashes under some NativeAOT and JIT environments by using `GetModuleHandle(default)` instead of dynamic method pointers to resolve the `HINSTANCE`.