WinUSB: Merge upstream changes

This commit is contained in:
Gus
2021-02-08 17:08:46 +01:00
parent f438d0dd71
commit 8630a89553
8 changed files with 203 additions and 139 deletions
+39 -18
View File
@@ -1,6 +1,6 @@
/* WinUSBNet library
/* WinUSBNet library
* (C) 2010 Thomas Bleeker (www.madwizard.org)
*
*
* Licensed under the MIT license, see license.txt or:
* http://www.opensource.org/licenses/mit-license.php
*/
@@ -47,20 +47,13 @@ namespace MadWizard.WinUSBNet
/*
// Listen for the control's window creation and then hook into it.
internal void OnHandleCreated(object sender, EventArgs e)
private void OnHandleCreated(object sender, EventArgs e)
{
try
{
// Window is now created, assign handle to NativeWindow.
IntPtr handle = ((Control)sender).Handle;
AssignHandle(handle);
if (_notifyHandle != IntPtr.Zero)
{
API.DeviceManagement.StopDeviceDeviceNotifications(_notifyHandle);
_notifyHandle = IntPtr.Zero;
}
API.DeviceManagement.RegisterForDeviceNotifications(handle, _guid, ref _notifyHandle);
RegisterNotify(handle);
}
catch (API.APIException ex)
{
@@ -68,17 +61,12 @@ namespace MadWizard.WinUSBNet
}
}
internal void OnHandleDestroyed(object sender, EventArgs e)
private void OnHandleDestroyed(object sender, EventArgs e)
{
try
{
// Window was destroyed, release hook.
ReleaseHandle();
if (_notifyHandle != null)
{
API.DeviceManagement.StopDeviceDeviceNotifications(_notifyHandle);
_notifyHandle = IntPtr.Zero;
}
StopNotify();
}
catch (API.APIException ex)
{
@@ -86,6 +74,28 @@ namespace MadWizard.WinUSBNet
}
}
private void RegisterNotify(IntPtr handle)
{
AssignHandle(handle);
if (_notifyHandle != IntPtr.Zero)
{
API.DeviceManagement.StopDeviceDeviceNotifications(_notifyHandle);
_notifyHandle = IntPtr.Zero;
}
API.DeviceManagement.RegisterForDeviceNotifications(handle, _guid, ref _notifyHandle);
}
private void StopNotify()
{
//ReleaseHandle();
if (_notifyHandle != IntPtr.Zero)
{
API.DeviceManagement.StopDeviceDeviceNotifications(_notifyHandle);
_notifyHandle = IntPtr.Zero;
}
}
protected override void WndProc(ref Message m)
{
// Listen for operating system messages
@@ -95,6 +105,17 @@ namespace MadWizard.WinUSBNet
case API.DeviceManagement.WM_DEVICECHANGE:
_notifier.HandleDeviceChange(m);
break;
case WM_NCDESTROY:
// Note: when a control is used, OnHandleDestroyed will be called and the
// handle is already released from NativeWindow. In that case, this
// WM_NCDESTROY message will not be caught here. This is no problem since
// StopNotify is already called. Even if it does, calling it twice does not cause
// problems.
// When a window handle is used instead of a Control the OnHandle events will not
// fire and this handler is necessary to release the handle and stop notifications
// when the window is destroyed.
StopNotify();
break;
}
base.WndProc(ref m);
}