Click here to Skip to main content
15,895,011 members
Please Sign up or sign in to vote.
1.12/5 (3 votes)
See more:
How can i programmatically eject USB Device even if it is in use?

Scenarios:

USB device attached to the system.

We get WM_DEVICECHANGE message by "RegisterDeviceNotification" through which we can handle events like DBT_DEVICEARRIVAL, DBT_DEVICEQUERYREMOVE etc.

Now if the device is in use, if we want to eject it from explorer, it prompts as "Problem Ejecting USB Mass Storage Device "This device is currently in use. Close any programs or windows that might be using the device, and then try again."
Question: 1: How can we eject USB without any prompt while it is in use?

Question: 2: How can we get notification when the user press eject from explorer for USB device?

Thanks in advance :)
Posted
Comments
PIEBALDconsult 23-Dec-14 8:18am    
Why? Does the user want you to do that?
[no name] 23-Dec-14 18:51pm    
So what you're saying is that the OS role is irrelevant - eject anyway with no warning. Nice. What about the application using the device? Who cares? It can probably be done but why would you.
@wasthi 26-Dec-14 1:07am    
I have an application in which i have registered for WM_DEVICECHANGE notification. I have opened a file from USB drive. now if i Try to eject USB from explorer it shows the prompt. My question is that Can i get the event that shows the prompt for explorer? I have to handle that event.
@wasthi 29-Dec-14 4:06am    
In addition to above comment:
After USB attachment i get DBT_DEVICEARRIVAL notification, now i have opened a file from USB and then try to eject the USB form explorer. How to close the file(which has opened the handle of USB) to avoid the system prompt "USB is in use........".
@wasthi 6-Jan-15 0:50am    
Consider the following Flow: Device Attached ->WM_DEVICECHANGE Arrived ->Device is in use(Opened by any app) ->Try to Eject(Close the file & Successful Eject) ->Default System Prompt(USB Device is in use)..............in this flow normally when we try to eject System prompt occurs. i have to close the file programmatically who has open the USB and eject USB successfully to avoid the System Prompt. Is there any event that does the above required task before the system prompt occurs?

1 solution

I would say that this is a bad idea, and it will end in tears. If you write code which could cause loss of data, the chances are that it will come back and bite you in the ass, to use the Americanism.

Obviously, this is just my opinion, but... well, it makes sense to me.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900