|
|
Comments and Discussions
|
|
 |
|

|
I am trying to use the same sample code for HID GUID interface, but it not working.
I am trying to map drive letter to device number and then trying to get Driverkey Name of USB mass storage HID device, Do I need to do make any code changes to the above sample to make it work for USB HID Devices.
Thanks
|
|
|
|

|
So, you have an USB device which is mass storage and HID device?
Then the partent/child relations probably look like this:
USB device
-> DISK
-> HID
Use the existing code for getting the USB device. Then do an additional enumertion for HID devices, get the DevInst of each, call CM_Get_Parent to get their parents's DevInst. Match this with the USB device's DevInst.
|
|
|
|

|
Thanks for the reply,
In my case parent is a rack with number of slots, So parent is common to all the devices I plugin. I can plug in a USB device which is a mass storage and HID device,
and also additionally other HID/USB devices.
The only input parameter I am getting is drive letter, using this I need to get driverkey name of the plugin device.
|
|
|
|

|
I am trying to get the USB driver keyname, but why I am getting differerent
driverkey names when change for following guid types, why?
guid = &GUID_DEVINTERFACE_VOLUME;
guid = &GUID_DEVINTERFACE_DISK;
HidD_GetHidGuid(&hidGuid);
My purpose is to get DriverKey name of the USB plugged in
I am getting drive letter as a parameter,
using that I get device number using IOCTL_STORAGE_GET_DEVICE_NUMBER.
it then enumerates all disks, using the setup APIs.
The device numbers of the drives are matched with the device number mentioned above in order to get the device instance
of the correct drive.
Using SP_DEVINFO_DATA and hDevInfo I call SetupDiGetDeviceRegistryProperty to get DriverKey name.
I getting different driver key names for each of these guids
guid = &GUID_DEVINTERFACE_VOLUME;
guid = &GUID_DEVINTERFACE_DISK;
HidD_GetHidGuid(&hidGuid);
Application is using HidD_GetHidGuid(&hidGuid) when they are adding device object to the currentDevice object list.
When I use one of these guids, the driver key name I am getting does not match with one in currentDevice object list
guid = &GUID_DEVINTERFACE_VOLUME;
guid = &GUID_DEVINTERFACE_DISK;
|
|
|
|

|
I've dealed with existing devices only.
Maybe enumeating for all devices it the right choice here, like so:
int res;
CONFIGRET cret;
char szProperty[512];
SP_DEVINFO_DATA devInfoData;
HDEVINFO hDevInfo = SetupDiGetClassDevs(NULL, 0, 0, DIGCF_ALLCLASSES | DIGCF_PRESENT);
devInfoData.cbSize = sizeof(devInfoData);
for ( int idx=0; true; idx++ ) {
res = SetupDiEnumDeviceInfo(hDevInfo, idx, &devInfoData);
if ( !res ) {
break; }
res = CM_Get_Device_ID(devInfoData.DevInst, szProperty, sizeof(szProperty), 0);
if ( res == CR_SUCCESS ) {
printf("Device ID = %s\n", szDeviceIdString);
}
res = SetupDiGetDeviceRegistryProperty(hDevInfo, &devInfoData,
SPDRP_DRIVER, &dwType,
(PBYTE)szProperty, sizeof(szProperty),
NULL);
if ( res ) {
printf("SPDRP_DRIVER = %s\n", szProperty);
}
}
SetupDiDestroyDeviceInfoList(hDevInfo);
|
|
|
|

|
I am trying to get the USB driver keyname, but why I am getting differerent
driverkey names when change for following guid types, why?
guid = &GUID_DEVINTERFACE_VOLUME;
guid = &GUID_DEVINTERFACE_DISK;
HidD_GetHidGuid(&hidGuid);
|
|
|
|

|
Each device has a unique drive key name. If you enumerate for a different device interface then you get differenct devices which of course have their own driver key names.
|
|
|
|

|
Nicely written and useful article.
Just because the code works, it doesn't mean that it is good code.
|
|
|
|
|

|
n2012 comes, in order to thank everyone, characteristic, novel style, varieties, low price and good quality, and the low sale price. Thank everyone
==== ( http://www.fullmalls.com ) =====
==== ( http://www.fullmalls.com ) =====
$33 True Religion jeans, Ed Hardy jeans,LV,Coogi jeans,Affliction jeans
$30 Air Jordan shoes,Shox shoes,Gucci,LV shoes
50%Discount winter fashion :Sandle,t-shirt,caps,jerseys,handbag and brand watches!!!
$15 Ed Hardy ,LV ,Gucci Bikini
$15 Polo, Ed Hardy, Gucci, LV, Lacoste T-shirts
$25 Coach,Gucci,LV,Prada,Juicy,Chanel handbag,
$10 Gucci,Ed Hardy sunglasses
$9 New Era caps.
give you the unexpected harvest
==== ( http://www.fullmalls.com ) =====
==== ( http://www.fullmalls.com ) =====
==== ( http://www.fullmalls.com ) =====
==== ( http://www.fullmalls.com ) =====
==== ( http://www.fullmalls.com ) =====
==== ( http://www.fullmalls.com ) =====
|
|
|
|

|
Very useful code dealing with poorly documented Windows functions.
|
|
|
|

|
Hello
How can reconnect the usb drive based on the given DeviceId;
modified on Friday, August 19, 2011 2:41 AM
|
|
|
|

|
Check out how the Microsoft devcon tool performs its restart command.
|
|
|
|

|
I ejected usb using CM_Query_And_Remove_SubTree but the MessageBox is not displaying("Safe to remove"). I am using this method for later i want to re-enable the device. How can i use CM_Query_And_Remove_SubTree method with MessageBox
-- Modified Friday, August 19, 2011 1:25 AM
|
|
|
|

|
Works for me as described. What is your OS and your code?
|
|
|
|

|
My operating system is XP.
My code as follows.
for(INT nCount = 0; nCount < 1; nCount++)
{
cr = CM_Query_And_Remove_SubTree(objDevInfoData.DevInst, NULL, NULL, 0, 0);
if(cr == CR_SUCCESS)
{
bUSBEjected = TRUE;
break;
}
Sleep(SLEEP);
}
modified on Thursday, August 18, 2011 12:04 AM
|
|
|
|

|
I had tried it with my tool RemoveDrive and here it worked. I've checked the code and there is indeed a trick involved. First perform the silent removal, then call CM_Request_Device_Eject to get the balloon. The device's problem code remains 21, so reactivating still works.
res = CM_Query_And_Remove_SubTreeW(DevInstRemovable, &VetoType, VetoNameW, MAX_PATH, ulFlags);
if ( bShowBalloon && res==CR_SUCCESS && VetoType==PNP_VetoTypeUnknown ) {
res = CM_Request_Device_EjectW(DevInstRemovable, NULL, NULL, 0, 0); }
|
|
|
|

|
Hello,
My code as follows. i applied like this but still Problem 47 is coming............
PNP_VETO_TYPE VetoType;
CHAR VetoName[MAX_PATH];
cr = CM_Query_And_Remove_SubTree(objDevInfoData.DevInst,&VetoType,VetoName,sizeof VetoName, 0);
if(cr == CR_SUCCESS && VetoType==PNP_VetoTypeUnknown)
{
cr = CM_Request_Device_EjectW(objDevInfoData.DevInst, NULL, NULL, 0, 0);
bUSBEjected = TRUE;
break;
}
|
|
|
|

|
hi Uwe: I used the codes you posted and made some necessary changes,but i found out that under WIN7 X64 is not working ,the error message i got is:the "drect memory access controller" device is not removable and can not be ejected or unplugged.why?can you give me a basic direction about why is message appeared?thanks ~ FYI: the device i want to eject is a calculaor.
|
|
|
|

|
Finally it's the device's driver which grants or rejects the request, so the driver of your device just does not support safe removal. I think it is not specific to Win7 x64, it is specific to the driver of the device.
How do you get this error string?
Uwe Sieber
|
|
|
|

|
i change the parameters in the cm_request_device_eject like this: cr = CM_Request_Device_EjectW(DeviceInfoData.DevInst,&pnpvietotype,NULL,0,0); wheni start to do eject the device ,it pop a messagebox include the message i said before .so that is not a peoblem of system, it's the driver ,i need to make the driver can suppot safe removal .but how to ?can you give me a example ?I'm a new learner of usb field.thank very very much~
|
|
|
|

|
It would be very convenient to tell user which processes block safely removal. Sometimes let say Firefox opens some file and disable safely removal.
Btw. I had problems translating your code in Delphi cause lots of functions in setupapi.pas (interface) file are not good mapped. I thought that my code is not working, but actually both code worked excellent and thanks to your sample I found out problems!!
If someone knows if it's possible to map processes that locked files or entire drive I would be very thankful.
Ivan
|
|
|
|

|
I've tried this in my tools RemoveDrive and EjectMedia using some undocumented ntdll.dll calls.
Google for NtQuerySystemInformation, NtQueryObject.
The main problem is that you don't know the type of handle before calling NtQueryObject. But NtQueryObject does not return if you hit a handle of a waiting sync object e.g. a mutex. If this happens then the calling thread is stuck in the kernel, TermitateThread cannot kill it, even killing the process does not work.
Tools as the Sysinternal ProcessExplorer or Cedrick Collomb's Unlocker use a kernel driver to get around this problem.
Uwe
|
|
|
|

|
On MSND stands this:
[NtQuerySystemInformation may be altered or unavailable in future versions of Windows. Applications should use the alternate functions listed in this topic.]
But that is not the problem, but idea, cause I don't know where to start and what to do. How to get handle list of all processes that effect on safely remove etc.
>> If this happens then the calling thread is stuck in the kernel, TermitateThread cannot kill it, even killing the process does not work.
This could be a problem but I will try to solve this somehow.
Sincerely,
Ivan
|
|
|
|
|

|
Nice explanation with details and analysis
|
|
|
|

|
Very good explanation with references. Several updates to cover gotchas. Working code sample with build instructions.
|
|
|
|

|
Hi Uwe,
thank you for this great article. Digging into the problem of determining if a removable drive is a floppy, I found this thread:
http://www.techtalkz.com/microsoft-device-drivers/244257-how-determine-removable-drive-floppy-drive.html[^]
I'm going to test the IOCTL_DISK_GET_DRIVE_GEOMETRY DeviceIoControl call --
cheers from Italy - Lorenzo -
-- EDIT --
well, seems it works... if you were looking for a better way to detect a floppy, here it is.
works even if floppy drive is empty or media is badly formatted or corrupted --
bool VolumeIsFloppy(HANDLE hVolume)
{
DISK_GEOMETRY geom;
DWORD dwBytesReturned = 0;
long res = DeviceIoControl(hVolume, IOCTL_DISK_GET_DRIVE_GEOMETRY, NULL, 0, &geom, sizeof(geom), &dwBytesReturned, NULL);
if ( res )
return ( geom.MediaType != RemovableMedia ) && ( geom.MediaType != FixedMedia );
else
return GetLastError() == ERROR_NOT_READY;
}
modified on Friday, April 1, 2011 6:03 AM
|
|
|
|

|
Interesting idea. Have you tried it? My concern is that the drive is really accessed, so a floppy drive would "rattle"...
Uwe
|
|
|
|

|
Yes, I tried it, and it works fine for me.
The drive does not rattle, but I don't know if some sort of "cache" is involved in this...
I'm not exactly a "Windows internals" expert... but my opinion is that the call is actually querying the geometry of the *drive* rather than the geometry of the *disk*.
I'm looking for some old 720k single sided floppy, I'm sure I have some in my loft. Let's see what the OS reports: this should tell us if the disk is phisically accesed or not, do you agree?
ciao - Lorenzo -
|
|
|
|

|
ok I made some tests with a 720K diskette.
1) the call to IOCTL_DISK_GET_DRIVE_GEOMETRY reports MediaType = F3_720_512 so it is reporting the geometry of the disk, not that of the drive (what on earth is "drive geometry", afterall? maybe I drank too much whisky );
2) the drive is phisically accessed only if it wasn't accessed previously; I think there is some sensor in the drive that detects media change, so phisical access is unnecessary unless disk geometry wasn't read yet since the last media change;
3) ok, the drive actually "rattles" in the described circumstances;
4) anyway, I observe that Windows itself has the same behaviour when asked to remove the USB floppy through the tray icon.
in conclusion, I think we're on the right track.
by the way: I'm (also) a Delphi fan, so I ported your code to Delphi. are you interested in publishing it alongside the C++ one?
ciao - Lorenzo -
|
|
|
|

|
I have used the above code to remove the PCI drive and found the following errors.
1. If CM_Query_And_Remove_SubTreeW function is used , it returns PNP_VetoOutstandingOpen when tried to remove the device.
2. If CM_Request_Device_EjectW function is used , it returns PNP_VetoIllegalDeviceRequest when tried to remove the device.
Please tell me how to solve these issues
|
|
|
|

|
What is a "PCI drive"?
PNP_VetoOutstandingOpen means there is at least one open handle, and
PNP_VetoIllegalDeviceRequest "The device does not support the specified operation".
http://msdn.microsoft.com/en-gb/library/ff549728.aspx
So, whatever it is, I suspect that the driver of the drive's parent device just does not support safe removal.
You can try to prepare the drive itself for safe removal (don't call CM_Get_Parent, just use the drive's DevInst).
Uwe Sieber
|
|
|
|

|
1. I tried CM_Query_And_Remove_SubTreeW with drive's DevInst and observed the following state.
The drive has been disappeared from the Disk Management. But in DeviceManager the drive is displayed with yellow icon(the properties of the drive displays that "Windows is uninstalling this device. (Code 21)"). What to do to completely remove the drive from DeviceManager.
2. I tried CM_Request_Device_EjectW with drive's DevInst, it displays the PNP_VetoIllegalDeviceRequest "The device does not support the specified operation". What has to be done from driver to support safe removal ?
|
|
|
|

|
Very complete; information difficult to derive from MSDN documentation [sic]
|
|
|
|

|
You use CM_Request_Device_Eject to eject the drive, which should logically and physically eject the drive. It physically ejects the drive in XP, but not in Windows 7. The drive cable must be manually removed for the USB device to register a disconnect. How can I send a physical disconnect in Windows 7 to the USB drive?
|
|
|
|

|
Sorry, seems I missed the notification about your message...
I think you refer to the new behaviour since Vista, which leaves USB devices powered after safe removal.
This can be changed to the old behaviour either system wide or device specific:
http://support.microsoft.com/kb/2401954
Uwe Sieber
|
|
|
|

|
Thanks alot for this nice code!!!
But I have a question. Very often something gets corrupted in the system or in one of the programs that uses this USB device and no matter what you do it won't be ejected, CM_Request_Device_Eject returns CR_REMOVE_VETOED.
I'm curious, is there any way to forcefully remove a USB Flash Drive (well, aside from physically yanking it out of the slot), via some kernel API call? Maybe some undocumented one? And if no, is there any way to know what process is holding a reference to this device that it preventing it from being ejected?
|
|
|
|

|
The returned veto name gives a hint if it is the volume or the disk which prevents the removal.
Mapping this to its "dos device name" you can use the SysInternals ProcessExplorer to search for an open handle.
To deal with foreign processes and their handles properly you have to go into the kernel mode as the ProcessExplorer or the UnLocker tool do.
I have no experience with that...
Uwe
|
|
|
|
|

|
FSCTL_DISMOUNT_VOLUME dismounts the file system, open handles are invalidated but they are not closed, so, safe removal will still fail.
Uwe Sieber
|
|
|
|

|
The link given above gives the contents of the cfg.h file on the screen with line numbers in it. Why not just include it along with the .lib file in this download???
|
|
|
|

|
An SDK is required anyway and the newer SDK (since Build 6000) come with the cfg.h.
Using it from ReactOS is the final fallback only for developers who are using VS6 and never have downloaded a newer SDK, which everyone should have at least to have a look on it...
The VS6 editor supports column blocks. So just paste it with the line numbers, place the cursor at the begin, press and hold the Alt key, click and drag over the numbers and the space column, release mouse and Alt key, press delete, save as cfg.h. Easy enough
Uwe Sieber
|
|
|
|

|
Hey,
Thanks, this is a good article =)
Anthony
|
|
|
|

|
Hello Uwe
You have released some excellent code and it is helping me a lot. I found a difference in your two programs, where RemoveDrive can remove a SATA device, but RemoveDriveByLetter cannot.
Could you please tell me what the difference is between the two programs?
Thank you! I have included the logs below to show what is going on.
D:\RemoveDriveByLetter\Debug>RemoveDriveByLetter.exe f:
failed
Result=0x17
VetoName=PCIIDE\IDEChannel\4&3adfead3&0&0)
C:\RemoveDrive\x64>removedrive f:
RemoveDrive V1.9 (x64) - prepares drives for safe removal
Freeware by Uwe Sieber - www.uwe-sieber.de
Removing Drive 'F:\' (WDC WD7500AACS-65D6B0 ATA Device)
success
|
|
|
|

|
RemoveDrive checks if the drive's parent device is removable. If not then it removes the drive itself instead of its parent device.
The check for beeing removable:
CM_Get_DevNode_Status(&Status, &ProblemNumber, DevInst, 0);
IsRemovable = ((Status & DN_REMOVABLE) != 0);
Uwe
|
|
|
|

|
Hi Uwe
This smal sample is a powerful help for my private work. Thanks for sharing it. Checking the other discussions and your home page I found a additional very helpfull tool called DriveCleanup. I my special case it make sense to use both, so do you also share the code of this DriveCleanup tool. Than I'm able to combine it in one software I'm just building for my private use. Will be great.
Greetings from Germany
Raimund
|
|
|
|
|

|
Hi Uwe,
While discussing an issue related to drive *insertion* sometimes requiring a reboot with a Microsoft support engineer, I was supplied with the following information:
QUOTE
I think the DriveCleanup tool (http://www.uwe-sieber.de/drivetools_e.html ) may have the defect that it doesn't call co-installer to uninstall driver, hence sometimes when re-installing USB storage device, device vetoes removal failed, in the end the installation finished with marking DI_NEEDREBOOT flag.
UNQUOTE
QUOTE
DriverCleanup calls CM_xxx API's to remove device, such as CM_Query_And_Remove_SubTree. This is bad, because CM_XXX API's bypass class/co-installers.
Co-installer sets Flags member of the SP_DEVINSTALL_PARAMS structure for the device with the DI_NEEDREBOOT flag, and the installer also notifies the user that a system restart is required.
The correct method is to invoke DIF_REMOVE, which is shown in Devcon. You may check the codes in WDK(\WinDDK\6001.18002\src\setup\devcon -> cmds.cpp->
RemoveCallback())
UNQUOTE
What do you think?
Grüße;
P.S. I'll share the final word on "why does XP sometimes prompt for a reboot" and how to avoid it.
|
|
|
|
 |
|
|
General News Suggestion Question Bug Answer Joke Rant Admin
|
Shows the link between a drive letter, its disk number, and the disk's device instance
| Type | Article |
| Licence | CPOL |
| First Posted | 18 Apr 2006 |
| Views | 422,607 |
| Downloads | 5,819 |
| Bookmarked | 266 times |
|
|