USB video device button message processing and common USB connection event processing

Source: Internet
Author: User

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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.