Wm_devicechange USB device to achieve hot plug _wm_devicechange

Source: Internet
Author: User

When implementing certain functions, the USB device needs to be used to change the state of the program according to the plug of USB device, and the WM_DEVICECHANGE message is used at this time.

It is to be noted that:

1. Wm_devicechange messages can only be added manually, not in the Class wizard

2. This message will only be received by the top-level window (if you are developing OCX embedded in the Web page, you cannot receive this message in OCX ctrl)


Using Step Declaration messages

afx_msg BOOL Ondevicechange (UINT neventtype, DWORD dwdata);
Implementation message
BOOL Cmaindlg::ondevicechange (UINT neventtype, DWORD dwdata)
{
	Pdev_broadcast_hdr lpdb = (PDEV_BROADCAST_HDR) Dwdata;
	Switch (neventtype)      
	{case      
	dbt_devicearrival:  
		if (m_blistenusb && lpdb-> dbch_devicetype = DBT _devtyp_volume)
		{
			M_blistenusb = false;
			Pdev_broadcast_volume LPDBV = (pdev_broadcast_volume) lpdb;
			M_cdrivepath[0] = Firstdrivefrommask (LPDBV->dbcv_unitmask);//Get U disk letter
		} break
		;  
	Default: Break      
		;      
	}      
	return FALSE; 
}

TCHAR Firstdrivefrommask (ULONG unitmask)
{
	char i;
	for (i = 0; i < ++i)
	{
		if (Unitmask & 0x1)//See whether the state of the drive has changed the break
			;
		Unitmask = Unitmask >> 1;
	}
	return (i + ' A ');
}


Note: dbt_devnodes_changed need to add header files: #include <Dbt.h> Add the mapping of this message in the message map
Begin_message_map (Cthreaddlg, CDialog)
	on_wm_devicechange ()
End_message_map ()

Cwnd::ondevicechange afx_msg BOOL ondevicechange (UINT neventtype, DWORD dwdata);

Parameters:
Neventtype event type. See the Description section for a description of possible values.
Dwdata contains the address of the structure of the data associated with the event. Its meaning depends on the given event.

Description
The framework calls this function to notify the application or device driver that the hardware configuration of the device or computer has changed.
For devices that provide software control functions, such as pop-ups and locks, the operating system usually sends a dbt_deviceremovepending message to stop the application and device drivers from using the device.
If the operating system forcibly clears a device, it may not send a dbt_devicequeryremove message.
The nevent parameter can be one of the following values:
· Dbt_devicearrival has added a device that can now be used.
· Dbt_devicequeryremove allows the requested device to be purged. Any application can reject the request and cancel the cleanup operation.
· Dbt_devicequeryremovefailed the request to clear the device has been canceled.
· The dbt_deviceremovepending device will be cleared. Cannot refuse.
· The Dbt_deviceremovecomplete device has been cleared.
· Dbt_devicetypespecific the device-related events.
· Dbt_configchanged the current configuration has changed.
· The dbt_devnodes_changed device node has changed.

Attention
The framework calls this member function to allow your application to process a Windows message. The arguments passed to your member function reflect the parameters that the framework received when the message was received. If you call the base class implementation of this function, the implementation will use the arguments that were originally passed to the message (rather than the arguments you provided to the function).

Please refer to: Wm_devicechange

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.