110,539
社区成员
发帖
与我相关
我的任务
分享
public void ParseMessages(ref Message m)
{
if (m.Msg == Win32Usb.WM_DEVICECHANGE) // we got a device change message! A USB device was inserted or removed
{
switch (m.WParam.ToInt32()) // Check the W parameter to see if a device was inserted or removed
{
case Win32Usb.DEVICE_ARRIVAL: // inserted
if (OnDeviceArrived != null)
{
OnDeviceArrived(this, new System.EventArgs());
CheckDevicePresent();
}
break;
case Win32Usb.DEVICE_REMOVECOMPLETE: // removed
if (OnDeviceRemoved != null)
{
OnDeviceRemoved(this, new System.EventArgs());
CheckDevicePresent();
}
break;
}
}
}
#region Constants
/// <summary>Windows message sent when a device is inserted or removed</summary>
public const int WM_DEVICECHANGE = 0x0219;
/// <summary>WParam for above : A device was inserted</summary>
public const int DEVICE_ARRIVAL = 0x8000;
/// <summary>WParam for above : A device was removed</summary>
public const int DEVICE_REMOVECOMPLETE = 0x8004;
/// <summary>Used in SetupDiClassDevs to get devices present in the system</summary>
protected const int DIGCF_PRESENT = 0x02;
/// <summary>Used in SetupDiClassDevs to get device interface details</summary>
protected const int DIGCF_DEVICEINTERFACE = 0x10;
/// <summary>Used when registering for device insert/remove messages : specifies the type of device</summary>
protected const int DEVTYP_DEVICEINTERFACE = 0x05;
/// <summary>Used when registering for device insert/remove messages : we're giving the API call a window handle</summary>
protected const int DEVICE_NOTIFY_WINDOW_HANDLE = 0;
/// <summary>Purges Win32 transmit buffer by aborting the current transmission.</summary>
protected const uint PURGE_TXABORT = 0x01;
/// <summary>Purges Win32 receive buffer by aborting the current receive.</summary>
protected const uint PURGE_RXABORT = 0x02;
/// <summary>Purges Win32 transmit buffer by clearing it.</summary>
protected const uint PURGE_TXCLEAR = 0x04;
/// <summary>Purges Win32 receive buffer by clearing it.</summary>
protected const uint PURGE_RXCLEAR = 0x08;
/// <summary>CreateFile : Open file for read</summary>
protected const uint GENERIC_READ = 0x80000000;
/// <summary>CreateFile : Open file for write</summary>
protected const uint GENERIC_WRITE = 0x40000000;
/// <summary>CreateFile : file share for write</summary>
protected const uint FILE_SHARE_WRITE = 0x2;
/// <summary>CreateFile : file share for read</summary>
protected const uint FILE_SHARE_READ = 0x1;
/// <summary>CreateFile : Open handle for overlapped operations</summary>
protected const uint FILE_FLAG_OVERLAPPED = 0x40000000;
/// <summary>CreateFile : Resource to be "created" must exist</summary>
protected const uint OPEN_EXISTING = 3;
/// <summary>CreateFile : Resource will be "created" or existing will be used</summary>
protected const uint OPEN_ALWAYS = 4;
/// <summary>ReadFile/WriteFile : Overlapped operation is incomplete.</summary>
protected const uint ERROR_IO_PENDING = 997;
/// <summary>Infinite timeout</summary>
protected const uint INFINITE = 0xFFFFFFFF;
/// <summary>Simple representation of a null handle : a closed stream will get this handle. Note it is public for comparison by higher level classes.</summary>
public static IntPtr NullHandle = IntPtr.Zero;
/// <summary>Simple representation of the handle returned when CreateFile fails.</summary>
protected static IntPtr InvalidHandleValue = new IntPtr(-1);
#endregion