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
<PackageReference Include="Sibber.WindowMessageMonitor" Version="5.0.0" />
<PackageVersion Include="Sibber.WindowMessageMonitor" Version="5.0.0" />
<PackageReference Include="Sibber.WindowMessageMonitor" />
paket add Sibber.WindowMessageMonitor --version 5.0.0
#r "nuget: Sibber.WindowMessageMonitor, 5.0.0"
#:package Sibber.WindowMessageMonitor@5.0.0
#addin nuget:?package=Sibber.WindowMessageMonitor&version=5.0.0
#tool nuget:?package=Sibber.WindowMessageMonitor&version=5.0.0
Sibber.WindowMessageMonitor
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 | Versions 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. |
-
.NETStandard 2.0
- Microsoft.Bcl.HashCode (>= 6.0.0)
- Sibber.Common.Native.Windows (>= 3.0.1 && < 4.0.0)
- System.Memory (>= 4.6.3)
-
net8.0-windows7.0
- Sibber.Common.Native.Windows (>= 3.0.1 && < 4.0.0)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.
### 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`.