For some reason I dont get the traytip notification.. If I replace with msgbox It works. It disconnect as expected, I am just wondering why I dont get the notification?
Code:
Kingston := FindDriveBySerial( "0018F30C9" )
USBD_SafelyRemove( Kingston )
return
FindDriveBySerial( DeviceSerial ) {
DriveGet,RD, List, REMOVABLE
StringReplace, RD, RD, A ; Remove Drive Letter 'A' from List
Loop, Parse, RD
If ( USBD_GetDeviceSerial( A_LoopField ":" ) = DeviceSerial )
Return A_LoopField ":"
}
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
Else, MsgBox, Test
}
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 )
}