News:

MASM32 SDK Description, downloads and other helpful links
MASM32.com New Forum Link
masmforum WebSite

safely shutdown usb hard drive

Started by azdps, May 26, 2009, 03:05:02 AM

Previous topic - Next topic

azdps

i'm able to shutdown a usb thumb drive for safe removal, but using the same command i'm not able to shutdown a usb hard drive. i use IOCTL_STORAGE_EJECT_MEDIA to shutdown usb flashdrive. any ideas for usb hard drive?

redskull

Perhaps IOCTL_STORAGE_MEDIA_REMOVAL?  There seems to be differing opinions on the semantics between what is 'removable' and what is 'ejectable'.
Strange women, lying in ponds, distributing swords, is no basis for a system of government

azdps

tried that. IOCTL_STORAGE_MEDIA_REMOVAL for example will lock or unlock a cd drive from being able to eject the cd tray. i won't actually eject (stop) a usb drive.


azdps

ya dedndave i like zara's picture better too. anyways i actually found that link before you had posted it and had been looking it over. i've attempted to use some of the fuctions etc but many of them are not defined in the masm32 windows.inc or separate file.

how do you define GUID_DEVINTERFACE_DISK?

in the windows sdk WinIoCtl.h file it's defined as:

DEFINE_GUID(GUID_DEVINTERFACE_DISK, 0x53f56307L, 0xb6bf, 0x11d0, 0x94, 0xf2, 0x00, 0xa0, 0xc9, 0x1e, 0xfb, 0x8b);

i need it to work with masm but it doesnt.

dedndave

#5
according to MS docs, the class guid is defined in Ntddstor.h
however, it is defined just as you said - it should be ok
0x53f56307L, 0xb6bf, 0x11d0, 0x94, 0xf2, 0x00, 0xa0, 0xc9, 0x1e, 0xfb, 0x8b
which corresponds to {53F56307-B6BF-11D0-94F2-00A0C91EFB8B}

a listing of Ntddstor.h
http://www.koders.com/c/fid365E9BBCE97F34081FE24CE89CAEFCCCE2CA014B.aspx

let me see if i can make it work in asm

EDIT i found a much clearer description here - again, the code is in c, but the text is more straightforward
let me try a masm version
http://support.microsoft.com/kb/165721

azdps

i've been to the microsoft link already. that works for usb flash drive but i was unable to get it to work for usb hard drive. maybe im doing something wrong. also i was able to find a powerbasic include file to see how it was included. and looks like you have posted what i had just found out.

dedndave

#7
ok
here is what i understand from what i've read

EDITED....
if you follow the procedure in the KB165721 article, it fails because usb hard drives are partitionable
it works with usb flash drives because they are not partitionable
even if you have only one partition, the drive will have more than one device handle, as the OS uses at
least one handle to do housekeeping on the partitionable drive - at least that is how it appears

that means, in order to lock the device completely, you have to enumerate the handles on the device,
first by using the DeviceIoControl IOCTL_STORAGE_GET_DEVICE_NUMBER function
you can then enumerate the handles on that device (somehow - lol)

from looking at that guys code (codeproject) he uses some DDK functions, looks
through the structure, locking each device handle belonging to that device

he uses some functions that are in the device driver development kit
(DDK) in order to enumerate them - i am not sure that is neccessary

there must be a way to do this without using the DDK, because the OS does it
whether these functions are available through the API is hard to say
i think they are because programs like EaseUs can lock and dismount drives
i tried to locate some similar functions under win/com controls, but it is somewhat confusing

but, the DDK is a freebie....
http://www.microsoft.com/whdc/devtools/ddk/default.mspx


azdps

i've been working my way through the code at http://www.codeproject.com/KB/system/RemoveDriveByLetter.aspx. now i can't figure out the following c++ code:

CM_Get_Parent(&DevInstParent, DevInst, 0);

I don't understand what values i'm suppose to put in DevInstParent or DevInst spots.




dedndave

dang - i had a bunch of stuff written and my log-in session timed out  :(
let me see if i can recap.....

dedndave

CM_Get_Parent is another DDK function call...
http://msdn.microsoft.com/en-us/library/ms791198.aspx

in C, "&" means "address of the variable", so &DevInstParent is "offset DevInstParent" to us
that is an output of the CMAPI function call

without tearing into it again today (lol), i think the DevInst is a pointer into an array
of structures that has been returned by one of the other DDK function calls
it is the Device Instance Handle

now, this is a big learning experience for me, so my take on it may be, and probably is, skewed as hekk
from what i gather, the OS has handles for devices (like drives) that it uses to perform housekeeping
(keeping track of write-cache status, indexing files - all that good stuff)

dedndave

i found some more info for you...

USB related
http://www.usb.org/developers/docs/

here is a site that has a forum, dedicated to driver development
you are sure to find people far more knowledgable on this subject than me
http://www.osronline.com/

locally, we have a guy named Zooba that comes in now and then
you might want to send him a PM

azdps

thanks for the help dedndave. got it all working now. shuts down usb hard drive and thumb drives now.

dedndave

lol - great !  :8)

i take it you used the DDK ?