我在工作中需要挂钩 WPF 应用程序的 Windows 事件。我需要监听 USB 事件。我发现了分散且不完整的答案,因此我想在一个统一的位置记录我的方法。
我最初的问题发生在尝试复制此处的代码示例时:
/sf/answers/1360502111/
我能够挂钩 Windows 事件并接收设备通知,但它们非常通用,没有为我提供太多可用于我的应用程序的信息。
对该页面的进一步阅读使我在同一页面上得到了不同的答案,该答案直接挂接到窗口句柄中以监视事件:
https ://stackoverflow.com/a/620179/1683999
这个答案提供了链接: https:
//www.codeproject.com/Articles/3946/Trapping-windows-messages
通过按照 codeproject 教程进行一些修改以挂钩 WPF 的窗口句柄,我能够获取 WM_DEVICECHANGE 消息,但在解码 wParam 时,我只收到 DBT_DEVNODES_CHANGED,因为我没有注册监听 USB 事件。通过 Google 快速搜索,我找到了一篇旧的 MSDN 论坛帖子: https:
//social.msdn.microsoft.com/Forums/vstudio/en-US/983dc1ee-6208-4036-903f-3fd5674a1efb/registerdevicenotification-in-wpf ?forum =wpf
在这个帖子中,我找到了我正在寻找的答案。我没有注册 Window 来专门查找 USB 事件,因此我从 Windows 获取了通用事件代码。进一步的研究让我回到了 StackOverflow:
/sf/answers/1137213101/
最后的答案为我解决了这个难题。我提供了一些代码片段,概述了通过挂钩到 WPF 窗口然后创建一个注册该窗口以侦听 USB 事件的侦听器来侦听 Windows 事件所需的内容。
要访问 USB 事件,我们需要做一些事情。
首先我们需要链接到 user32.dll 中的一些方法。
namespace Example
{
// https://www.pinvoke.net/default.aspx/Structures.DEV_BROADCAST_DEVICEINTERFACE
[StructLayout(LayoutKind.Sequential)]
public struct DEV_BROADCAST_DEVICE_INTERFACE
{
public int Size;
public int DeviceType;
public int Reserved;
public Guid ClassGuid;
public short Name;
}
public class Win32Native
{
[DllImport("user32.dll", CharSet = CharSet.Auto)]
public static extern IntPtr RegisterDeviceNotification(
IntPtr hRecipient,
IntPtr notificationFilter,
uint flags);
[DllImport("user32.dll", CharSet = CharSet.Auto)]
public static extern uint UnregisterDeviceNotification(IntPtr hHandle);
}
}
Run Code Online (Sandbox Code Playgroud)
我们需要它来注册我们的 WPF 窗口来监听 USB 事件。
namespace Example
{
public class UsbEventRegistration : IDisposable
{
private const int DBT_DEVTYP_DEVICEINTERFACE = 5;
private static readonly s_guidDevInterfaceUsbDevice =
new Guid("A5DCBF10-6530-11D2-901F-00C04FB951ED");
private readonly IntPtr _windowHandle;
private IntPtr _notificationHandle = IntPtr.Zero;
public bool IsRegistered => _notificationHandle != IntPtr.Zero;
public UsbEventRegistration(IntPtr windowHandle)
{
_windowHandle = windowHandle;
}
public void Register()
{
var dbdi = new DEV_BROADCAST_DEVICE_INTERFACE
{
DeviceType = DBT_DEVTYP_DEVICEINTERFACE,
Reserved = 0,
ClassGuid = s_guidDevInterfaceUsbDevice,
Name = 0,
};
dbdi.Size = Marshal.SizeOf(dbdi);
IntPtr buffer = Marshal.AllocHGlobal(dbdi.Size);
Marshal.StructureToPtr(dbdi, buffer, true);
_notificationHandle = Win32Native.RegisterDeviceNotification(
_windowHandle,
buffer,
0);
}
// Call on window unload.
public void Dispose()
{
Win32Native.UnregisterDeviceNotification(_notificationHandle);
}
}
}
Run Code Online (Sandbox Code Playgroud)
最后,准备好 WPF 窗口代码隐藏。
namespace Example
{
public partial class WPFWindow : Window
{
private UsbEventRegistration _usbEventRegistration;
public WPFWindow()
{
InitializeComponent();
}
protected override void OnSourceInitialized(EventArgs e)
{
base.OnSourceInitialized(e);
// IMO this should be abstracted away from the code-behind.
var windowSource = (HwndSource)PresentationSource.FromVisual(this);
_usbEventRegistration = new UsbEventRegistration(windowSource.Handle);
// This will allow your window to receive USB events.
_usbEventRegistration.Register();
// This hook is what we were aiming for. All Windows events are listened to here. We can inject our own listeners.
windowSource.AddHook(WndProc);
}
private IntPtr WndProc(IntPtr hwnd, int msg, IntPtr wParam, IntPtr lParam, ref bool handled)
{
// Here's where the help ends. Do what you need here.
// Get additional info from http://www.pinvoke.net/
// USB event message is msg == 0x0219 (WM_DEVICECHANGE).
// USB event plugin is wParam == 0x8000 (DBT_DEVICEARRIVAL).
// USB event unplug is wParam == 0x8004 (DBT_DEVICEREMOVECOMPLETE).
// Your device info is in lParam. Filter that.
// You need to convert wParam/lParam to Int32 using Marshal.
return IntPtr.Zero;
}
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1539 次 |
| 最近记录: |