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

dedndave

when things settle down around here, i might take another look at this
it seems interesting and may be a good way to learn more about windows
i would think there is a way to do it without the DDK - perhaps by using some undocumented calls

UtillMasm

 :U
oh my fo!
you must be my master!

dedndave

nahhhhhhhhh - just super-dave, is all

PBrennick

If possible, it would be nice if you shared the sources onless this is a job. It is very useful stuff to know. What I am trying to do is to create a software light switch for the USB light I use to see the keyboard. I would not have to keep pulling it out every morning.

Dave, Using a dial-up can be a pain if it times out. What I used to do is compose long replies whilst offline (in a text editor). Then I would sign in and paste it.

Paul


The GeneSys Project is available from:
The Repository or My crappy website

dedndave

yah - i dunno what happened there - i thought i was logged in "forever" - lol - oh well
but, i need to start posting in a text file like you said so that i can insure it is correct
this is especially true for posts that have code - wouldn't want someone trying out crummy code

i think what azdps wound up doing was to translate the code at this link from C into ASM....
http://www.codeproject.com/KB/system/RemoveDriveByLetter.aspx

azdps

here is the code. it will shutdown all usb drive (thumb drive, flash drive, usb hard drive) devices connected to your system.

.386
.model flat, stdcall
option casemap: none

   include         /masm32/include/windows.inc
   include         /masm32/include/kernel32.inc
   include         /masm32/include/user32.inc
   include         /masm32/include/comctl32.inc
   include         /masm32/include/masm32.inc
   include         /masm32/include/debug.inc
   include         /masm32/include/Setupapi.inc
   
   includelib      /masm32/lib/kernel32.lib
   includelib      /masm32/lib/user32.lib
   includelib      /masm32/lib/comctl32.lib
   includelib      /masm32/lib/masm32.lib
   includelib      /masm32/lib/debug.lib
   includelib      /masm32/lib/Setupapi.lib

SP_DEVINFO_DATA STRUCT
     cbSize              dd ?
     ClassGuid           GUID <>
     DevInst             dd ?
     Reserved            dd ?
SP_DEVINFO_DATA ENDS

.const
     DIGCF_PRESENT           equ 02h
     DIGCF_DEVICEINTERFACE   equ 10h
     SPDRP_FRIENDLYNAME      equ 0000000Ch
     SPDRP_REMOVAL_POLICY    equ 0000001Fh
     GUID_DEVINTERFACE_DISK TEXTEQU <{053f56307h,0b6bfh,011d0h,{094h,0f2h,000h,0a0h,0c9h,01eh,0fbh,08bh}}>
     xGUID_DEVINTERFACE_DISK GUID GUID_DEVINTERFACE_DISK

.data
  txtShutdown         db "Shutting down",0
 
.data?
  hDrive              HANDLE ?
  hDeviceInfoSet      HANDLE ?
  cbReturned          dd ?
  hDeviceInstance     dd ?
  szVetoName          dd ?
  szRemovable         db 256 dup (?)
  szBuff1             db 128 dup (?)
  devinfo             SP_DEVINFO_DATA  <?>


.code
start:

    invoke SetupDiGetClassDevs,addr xGUID_DEVINTERFACE_DISK,0,0,DIGCF_PRESENT + DIGCF_DEVICEINTERFACE
    mov hDeviceInfoSet,eax
     
    xor ecx,ecx
    mov devinfo.cbSize,sizeof SP_DEVINFO_DATA
@@:
    push ecx
    invoke SetupDiEnumDeviceInfo,hDeviceInfoSet,ecx,addr devinfo
        .if eax == TRUE
            invoke SetupDiGetDeviceRegistryProperty,hDeviceInfoSet,addr devinfo,SPDRP_REMOVAL_POLICY,NULL,addr szRemovable,4,0
                .if szRemovable == 3
                    invoke SetupDiGetDeviceRegistryProperty,hDeviceInfoSet,addr devinfo,SPDRP_FRIENDLYNAME,NULL,addr szRemovable,256,0
                    invoke CM_Get_Parent,addr hDeviceInstance,devinfo.DevInst,0
                    invoke CM_Request_Device_Eject,hDeviceInstance,0,addr szVetoName,MAX_PATH,0                 
                    invoke MessageBox,0,addr szRemovable,addr txtShutdown,0
                    invoke RtlZeroMemory,addr szRemovable,sizeof szRemovable
                    pop ecx
                    inc ecx
                    jmp @B
                .else ; its not removable
                    pop ecx
                    inc ecx
                    jmp @B
                .endif
         .endif

invoke SetupDiDestroyDeviceInfoList,hDeviceInfoSet
invoke CloseHandle,hDrive


; #################################################################################################

end start

[attachment deleted by admin]

azdps

#21
now i'm working on handling the notifications of when a usb disk device is connected and disconnected. i've run into a problem though. when a device is connected im able to obtain the drive letter of the new device after receiving a WM_DEVICECHANGE message. i need to find out how to obtain the "friendly name" of a device using the drive letter.

Edit:
i think i will try a different approach. this doesnt seem to be working.

dedndave

i saw a script
it was in VB - lol
only us good guys write in asm   :dance: