Identify USB device insertion and unplugging messages in Driver

Source: Internet
Author: User
Identifying USB device insertion and unplugging messages in drivers

Look at my other nest. There may be unexpected surprises. ^_^ www.devres.info

Identify USB device insertion/removal messages in driver favorites
It is a little difficult to capture the insertion and unplugging messages of a USB device in the driver (of course for my half bottle of water ). On the Internet, wm_devicechange is mentioned everywhere, but this is a window event. What should I do if there is no window in the driver? There is no window to create it by yourself ......

First, we will introduce the wm_devicechange message and the application detection method, and only focus on what I use.

Windowproc (uint message, wparam, lparam); // Message Processing Function

Message = wm_devicechange;

Wparam = dbt_devicearrival; // insert a device

= Dbt_deviceremovecomplete; // unplug the device

Next, when I use the application to test successfully, a new problem occurs. First, let the application start with the PDA. Second, hide the application window. The first problem seems to be solved by creating a shortcut in the startup Item (no experiment ......); second, I thought it was very simple. I only needed to set the attribute visible. I didn't expect it to work. (I used a dialog box program and it was useless to call showwindow ). This can be difficult... (if anyone knows the method, please kindly advise)

Finally, I will introduce the method I used-the background program, which is a DLL. It is started and loaded by the system's service program, so it can be ensured that at each startup, can run with the system. Besides, the window problem can be controlled by myself. It turns out that it is not familiar with the MFC mechanism, and it is not known that it shows the window ......

Of course, in addition to wm_devicechange detection, there are other methods to obtain the USB insertion and unplugging messages. You can create a message queue by yourself to obtain the corresponding hardware messages. Here, we only focus on the discussion and do not conduct in-depth research.

First, use createmsgqueue () to create a message queue. A parameter in the msgqueueoptions structure is required.

Second, after a message queue is created successfully, you can use requestdevicenotifications () to register the message queue to the system, so that you can obtain the corresponding message. Here, the first parameter I use null, it is to detect messages from all hardware devices.

Again, after completing the preceding steps, wait for the message in the message queue to arrive at waitforsingleobject (hmsg ...), you can use readmsgqueue () to read the message. The parameter of this function is similar to readfile (). After reading the message, it is processed accordingly.

Finally, close the Message Queue registered to the system and release the Message Queue resources ......

Through the application test, you can print out all the hardware device names in the system, and receive the corresponding message when the USB device is inserted and pulled out. However, when we test the program in the project, we find a new problem. When the port of the USB device is occupied, the USB device is pulled out, the two methods do not receive any messages ......

In the end, we had to poll the port of the USB device in the driver to determine whether the USB was inserted or unplugged.

All of the above are personal understandings. Due to the limited level, there will inevitably be gaps and deficiencies. Please refer to Hai Han, the author of this article. In addition, it indicates that all technical articles in this blog are original, and the writing style is similar to work notes. In line with the principle of respecting individual labor achievements, if you need to repost or reference them, add the original link or indicate the source.

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.