USB video device button message processing and common USB connection event processing
(C # version, C ++/C can be modified in a similar way)
I have tried many methods to create a USB camera example recently. The general method of USB connection is as follows:
(1) define message variables in Form class:
Public Const Int Wm_devicechange = 0x219;
Public Const Int Dbt_devicearrival = 0x8000;
Public Const Int Dbt_configchangecanceled = 0x0019;
Public Const Int Dbt_configchanged = 0x0018;
Public Const Int Dbt_customevent = 0x8006;
Public Const Int Dbt_devicequeryremove = 0x8001;
Public Const Int Dbt_devicequeryremovefaileds = 0x8002;
Public Const Int Dbt_deviceremovecomplete = 0x8004;
Public Const Int Dbt_deviceremovepending = 0x8003;
Public Const Int Dbt_devicetypespecific = 0x8005;
Public Const Int Dbt_devnodes_changed = 0x0007;
Public Const Int Dbt_querychangeconfig = 0x0017;
Public Const Int Dbt_userdefined = 0 xFFFF;
(2) override form class wndproc method:
Protected Override Void Wndproc ( Ref Message m)
{
Try
{
If (M. MSG = wm_devicechange)
{
Switch (M. wparam. toint32 ())
{
Case Wm_devicechange:
Break ;
Case Dbt_devicearrival: // USB flash drive insertion
Driveinfo [] S = driveinfo. getdrives ();
Foreach (Driveinfo drive In S)
{
If (Drive. drivetype = drivetype. removable)
{
Listbox1.items. Add (" The USB flash drive has been inserted. The drive letter is: "+ Drive. Name. tostring ());
Break ;
}
}
Break ;
Case Dbt_configchangecanceled:
Break ;
Case Dbt_configchanged:
Break ;
Case Dbt_customevent:
Break ;
Case Dbt_devicequeryremove:
Break ;
Case Dbt_devicequeryremovefailed:
Break ;
Case Dbt_deviceremovecomplete: // Unmount the USB flash drive
Break ;
Case Dbt_deviceremovepending:
Break ;
Case Dbt_devicetypespecific:
Break ;
Case Dbt_devnodes_changed:
Break ;
Case Dbt_querychangeconfig:
Break ;
Case Dbt_userdefined:
Break ;
Default :
Break ;
}
}
}
Catch (Exception ex)
{
MessageBox. Show (ex. Message );
}
Base . Wndproc ( Ref M );
}
}
Limitations of this method:
For general USB disks (U disks/storage), you can process the insert/remove event of the USB flash disk, but cannot process non-stored USB devices such as camera.