I had a need at work to hook into Windows events for a WPF application. I needed to listen to USB events. I've found scattered and incomplete answers, so I wanted to document my approach in a consolidated location.
My original problem occurred when trying to replicate the code example here:
https://stackoverflow.com/a/19435744/1683999
I was able to hook into Windows events and receive device notifications, however they were very generic and didn't give me much information that I could use for my application.
Further reading on that page led me to a different answer on the same page that hooked directly into the window handle to monitor events:
https://stackoverflow.com/a/620179/1683999
This answer gave a link to:
https://www.codeproject.com/Articles/3946/Trapping-windows-messages
By following the codeproject tutorial with a bit of modification to hook into WPF's window handle I was able to get WM_DEVICECHANGE messages, but when decoding wParam, I was only receiving DBT_DEVNODES_CHANGED since I wasn't registered to listen to USB events. A quick Google search led me to an old MSDN forums post:
https://social.msdn.microsoft.com/Forums/vstudio/en-US/983dc1ee-6208-4036-903f-3fd5674a1efb/registerdevicenotification-in-wpf?forum=wpf
In this thread, I found the answer I was looking for. I wasn't registering the Window to look specifically for USB events, so I was getting a general event code from Windows. Further research led me back to StackOverflow:
https://stackoverflow.com/a/16245901/1683999
This last answer completed the puzzle for me. I have provided code snippets outlining what was needed to listen for Windows events by hooking into the WPF window and then creating a listener that registers the window to listen for USB events.