AutoHotkey Homepage AutoHotkey Community
Let's help each other out
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Crazy Scripting : Safely Remove USB Flash Drive - 45L
Goto page 1, 2, 3  Next
 
Reply to topic    AutoHotkey Community Forum Index -> Scripts & Functions
View previous topic :: View next topic  
Author Message
SKAN



Joined: 26 Dec 2005
Posts: 8688

PostPosted: Tue Jun 02, 2009 3:15 pm    Post subject: Crazy Scripting : Safely Remove USB Flash Drive - 45L Reply with quote

    Usage Example:

    You may use a drive letter
    USBD_SafelyRemove( "H:" )
    or call DeviceEject() directly with a DeviceID
    DeviceEject( "USB\VID_058F&PID_6387\GDLL4HW4" )



Code:
USBD_SafelyRemove( Drv ) {
 If A_OSVersion not in  WIN_VISTA,WIN_XP,WIN_2000
   Return
 If ! ( Serial := USBD_GetDeviceSerial( Drv ) )
   Return 
 DeviceID := USBD_GetDeviceID( Serial )
 DeviceEject( DeviceID )
 IfExist, %Drv%\, TrayTip, %DeviceID%, Drive %Drv% was not Ejected!, 10, 3
 Else, TrayTip, %DeviceID%, Drive %Drv% was safely Removed, 10, 1
}

USBD_GetDeviceSerial( Drv="" ) {
 DriveGet, DriveType, Type, %Drv%
 IfNotEqual,DriveType,Removable, Return
 RegRead, Hex, HKLM, SYSTEM\MountedDevices, \DosDevices\%Drv%
 VarSetCapacity(U,(Sz:=StrLen(Hex)//2)),  VarSetCapacity(A,Sz+1)
 Loop % Sz
  NumPut( "0x" . SubStr(hex,2*A_Index-1,2), U, A_Index-1, "Char" )
 DllCall( "WideCharToMultiByte", Int,0,Int,0, UInt,&U,UInt,Sz, Str,A,UInt,Sz, Int,0,Int,0)
 StringSplit, Part, A, #
 ParentIdPrefixCheck := SubStr( Part3,1,InStr(Part3,"&",0,0)-1 )
 IfEqual,A_OSVersion,WIN_VISTA, Return,ParentIdPrefixCheck
 Loop, HKLM, SYSTEM\CurrentControlSet\Enum\USBSTOR,1,0
  { Device := A_LoopRegName
    Loop, HKLM, SYSTEM\CurrentControlSet\Enum\USBSTOR\%Device%,1,0
     { Serial := A_LoopRegName
       RegRead, PIPrefix, HKLM, SYSTEM\CurrentControlSet\Enum\USBSTOR\%Device%\%Serial%
              , ParentIdPrefix
       If ( PIPrefix = ParentIdPrefixCheck )
         Return, SubStr( Serial,1,InStr(Serial,"&",0,0)-1 )
     }
}}

USBD_GetDeviceID( Serial ) {
 Loop, HKLM, SYSTEM\CurrentControlSet\Enum\USB\,1,0
  { Device := A_LoopRegName
    Loop, HKLM, SYSTEM\CurrentControlSet\Enum\USB\%Device%,1,0
    If ( A_LoopRegName=Serial )
      Return DllCall( "CharUpperA", Str, "USB\" Device "\" Serial, Str )
}}

DeviceEject( DeviceID ) {
 hMod := DllCall( "LoadLibrary", Str,"SetupAPI.dll" ), VarSetCapacity(VE,255,0)
 If ! DllCall( "SetupAPI\CM_Locate_DevNodeA", UIntP,DI, Str,DeviceID, Int,0 )
 If ! DllCall( "SetupAPI\CM_Get_DevNode_Status", UIntP,STS, UIntP,PR, UInt,DI, Int,0)
 DllCall( "SetupAPI\CM_Request_Device_EjectA", UInt,DI, UIntP,VT, Str,VE, UInt,255, Int,0)
 DllCall( "FreeLibrary", UInt,hMod )
}


Smile
_________________
URLGet - Internet Explorer based Downloader
Back to top
View user's profile Send private message Send e-mail
MasterFocus



Joined: 08 Apr 2009
Posts: 3035
Location: Rio de Janeiro - RJ - Brasil

PostPosted: Tue Jun 02, 2009 3:24 pm    Post subject: Reply with quote

Amazing! Very Happy
Always wanted to safely remove quicker than usual. Thanks!
_________________
"Read the manual. Read it again. Search the forum.
Try something before asking. Show what you've tried.
"

Antonio França
My stuff: Google Profile
Back to top
View user's profile Send private message Visit poster's website
HotKeyIt



Joined: 18 Jun 2008
Posts: 4652
Location: AHK Forum

PostPosted: Tue Jun 02, 2009 8:02 pm    Post subject: Reply with quote

Many thanks Wink
_________________
AHK_H (2alpha) AHF TT _Struct WatchDir Yaml _Input ObjTree RapidHotkey DynaRun Wink
Back to top
View user's profile Send private message
el
Guest





PostPosted: Sat Oct 10, 2009 6:40 am    Post subject: Reply with quote

found it useful for quick hotkey to eject usb(s)
thanks
Back to top
Obi-Wahn



Joined: 20 Apr 2006
Posts: 75
Location: Vienna

PostPosted: Sat Oct 10, 2009 10:43 am    Post subject: Reply with quote

Hmm, great script, but when I disconnect my thumb drive with you script, it'll eject it, so I don't see it in the 'safely removal' dialog-box from windows, but I hear the sound when I disconnect the drive from the computer.

Also, I can't eject USB-HDDs from my Computer with your script.
Any suggestions?
Back to top
View user's profile Send private message Visit poster's website
Pip
Guest





PostPosted: Sat Oct 10, 2009 6:06 pm    Post subject: I love this Reply with quote

Hi

This is great...but...is it possible for it to eject the USB - if it is running from there itself?

Pip
Back to top
Raccoon



Joined: 02 Jan 2008
Posts: 150
Location: Freenode IRC

PostPosted: Tue Sep 14, 2010 2:29 am    Post subject: Reply with quote

Code:
IfNotEqual,DriveType,Removable, Return

FYI, not all USB flash drives have the 'removable' flag. I believe the same applies with eSATA drives as well certain USB harddrive enclosures.
_________________


Need help right away? Get live support on IRC.
Already have an IRC client installed? /join #ahk
Back to top
View user's profile Send private message
SKAN



Joined: 26 Dec 2005
Posts: 8688

PostPosted: Tue Sep 14, 2010 2:43 am    Post subject: Reply with quote

Raccoon wrote:
FYI, not all USB flash drives have the 'removable' flag.


Thanks for the info.. I did guess that when I wrote the wrapper.
1) It just did not make sense to me to 'remove' something that is not marked removable
2) I did not have the hardware to test it.
3) I had mentioned the option of using DeviceEject() directly.
Back to top
View user's profile Send private message Send e-mail
TomXIII



Joined: 14 Apr 2009
Posts: 182

PostPosted: Tue Sep 14, 2010 8:10 am    Post subject: Reply with quote

@SKAN : I like this script but it seems that doesn't work with AHK_L
Back to top
View user's profile Send private message
SKAN



Joined: 26 Dec 2005
Posts: 8688

PostPosted: Tue Sep 14, 2010 8:15 am    Post subject: Reply with quote

TomXIII wrote:
it seems that doesn't work with AHK_L


I will check it.. BTW, what OS?.. I can test only in XP!
Back to top
View user's profile Send private message Send e-mail
TomXIII



Joined: 14 Apr 2009
Posts: 182

PostPosted: Tue Sep 14, 2010 8:22 am    Post subject: Reply with quote

I'm on Win7. I did a test with both AutoHotkey & AutoHotkey_L and it works fine only with AutoHotkey
Back to top
View user's profile Send private message
SKAN



Joined: 26 Dec 2005
Posts: 8688

PostPosted: Thu Sep 16, 2010 6:46 pm    Post subject: USBD_SafelyRemoveW() for AutoHotkey_LW Reply with quote

I will be glad if this can be tested in XP/Vista/Win7 in AHK_LW

Code:
; USBD_SafelyRemoveW() for AutoHotkey_LW
; www.autohotkey.com/forum/viewtopic.php?p=384003#384003

USBD_SafelyRemoveW( Drv ) {
 If A_OSVersion not in  WIN_VISTA,WIN_XP,WIN_2000
   Return
 If ! ( Serial := USBD_GetDeviceSerialW( Drv ) )
   Return
 DeviceID := USBD_GetDeviceIDW( Serial )
 DeviceEjectW( DeviceID )
 IfExist, %Drv%\, TrayTip, %DeviceID%, Drive %Drv% was not Ejected!, 10, 3
 Else, TrayTip, %DeviceID%, Drive %Drv% was safely Removed, 10, 1
}

USBD_GetDeviceSerialW( Drv="" ) {
 DriveGet, DriveType, Type, %Drv%
 IfNotEqual,DriveType,Removable, Return
 RegRead, Hex, HKLM, SYSTEM\MountedDevices, \DosDevices\%Drv%
 VarSetCapacity(U,(Sz:=StrLen(Hex)//2)+2, 0 )
 Loop % Sz
  NumPut( "0x" . SubStr(hex,2*A_Index-1,2), U, A_Index-1, "Char" )
 VarSetCapacity( U, -1 )
 StringSplit, Part, U, #
 ParentIdPrefixCheck := SubStr( Part3,1,InStr(Part3,"&",0,0)-1 )
  IfNotEqual,A_OSVersion,WIN_XP, Return,ParentIdPrefixCheck ; this line needs testing in Win7
 Loop, HKLM, SYSTEM\CurrentControlSet\Enum\USBSTOR,1,0
  { Device := A_LoopRegName
    Loop, HKLM, SYSTEM\CurrentControlSet\Enum\USBSTOR\%Device%,1,0
     { Serial := A_LoopRegName
       RegRead, PIPrefix, HKLM, SYSTEM\CurrentControlSet\Enum\USBSTOR\%Device%\%Serial%
              , ParentIdPrefix
       If ( PIPrefix = ParentIdPrefixCheck )
         Return, SubStr( Serial,1,InStr(Serial,"&",0,0)-1 )
     }
}}

USBD_GetDeviceIDW( Serial ) {
 Loop, HKLM, SYSTEM\CurrentControlSet\Enum\USB\,1,0
  { Device := A_LoopRegName
    Loop, HKLM, SYSTEM\CurrentControlSet\Enum\USB\%Device%,1,0
    If ( A_LoopRegName=Serial )
      Return DllCall( "CharUpperW", Str, "USB\" Device "\" Serial, Str )
}}

DeviceEjectW( DeviceID ) {
 hMod := DllCall( "LoadLibrary", Str,"SetupAPI.dll" ), VarSetCapacity(VE,255,0)
 If ! DllCall( "SetupAPI\CM_Locate_DevNodeW", UIntP,DI, Str,DeviceID, Int,0 )
 If ! DllCall( "SetupAPI\CM_Get_DevNode_Status", UIntP,STS, UIntP,PR, UInt,DI, Int,0)
 DllCall( "SetupAPI\CM_Request_Device_EjectW", UInt,DI, UIntP,VT, Str,VE, UInt,255, Int,0)
 DllCall( "FreeLibrary", UInt,hMod )
}
Back to top
View user's profile Send private message Send e-mail
TomXIII



Joined: 14 Apr 2009
Posts: 182

PostPosted: Fri Sep 17, 2010 9:02 am    Post subject: Reply with quote

Previous code tested with AHK_L on Win7:

It works with a little modification:
Code:
USBD_SafelyRemoveW( Drv ) {
 If A_OSVersion not in  WIN_VISTA,WIN_XP,WIN_2000,WIN_7
   Return


Last edited by TomXIII on Fri Sep 17, 2010 9:07 am; edited 1 time in total
Back to top
View user's profile Send private message
SKAN



Joined: 26 Dec 2005
Posts: 8688

PostPosted: Fri Sep 17, 2010 9:06 am    Post subject: Reply with quote

Alter the following line

Code:
IfNotEqual,A_OSVersion,WIN_XP, Return,ParentIdPrefixCheck


to

Code:
IfEqual,A_OSVersion,WIN_VISTA, Return,ParentIdPrefixCheck


and try again, please.

Code:
USBD_SafelyRemoveW( "H:" )
Back to top
View user's profile Send private message Send e-mail
TomXIII



Joined: 14 Apr 2009
Posts: 182

PostPosted: Fri Sep 17, 2010 9:09 am    Post subject: Reply with quote

Sorry for the previous post.
I edited it after you last post! OUPS

But... It's works for me!
Back to top
View user's profile Send private message
Display posts from previous:   
Reply to topic    AutoHotkey Community Forum Index -> Scripts & Functions All times are GMT
Goto page 1, 2, 3  Next
Page 1 of 3

 
Jump to:  
You can post new topics in this forum
You can reply to topics in this forum


Powered by phpBB © 2001, 2005 phpBB Group