AutoHotkey Community

It is currently May 27th, 2012, 1:38 pm

All times are UTC [ DST ]




Post new topic Reply to topic  [ 37 posts ]  Go to page Previous  1, 2, 3
Author Message
 Post subject:
PostPosted: June 2nd, 2011, 4:28 pm 
Hi thanks for answering..

Ive just replaced my ID info in your example from 1st post

Code:
DeviceEject( "USB\VID_058F&PID_6387\GDLL4HW4" )


Report this post
Top
  
Reply with quote  
 Post subject:
PostPosted: June 3rd, 2011, 8:18 am 
Offline

Joined: April 23rd, 2010, 11:22 am
Posts: 64
SKAN wrote:
Open notepad / type something / save it to USB drive / Do not close notepad / try to eject the drive.


notepad does not lock up the USB drive (well not on my box in any case) MS Word/Excel/Powerpoint does or opening an explorer window on the USB drive


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: June 3rd, 2011, 8:26 am 
Offline
User avatar

Joined: December 26th, 2005, 4:40 pm
Posts: 8776
capitalH wrote:
notepad does not lock up the USB drive


I came to know that after I posted :)

Quote:
opening an explorer window on the USB drive


Explorer does not lock up the drive for me in XP. The explorer window mostly closes or redirects to 'Control Panel' depending on its ahk_class ( I think ).


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: June 3rd, 2011, 8:49 am 
how do I get a usb device VID and PID? It means product ID and vendor ID, right?


Report this post
Top
  
Reply with quote  
 Post subject:
PostPosted: June 3rd, 2011, 9:09 am 
Offline
User avatar

Joined: December 26th, 2005, 4:40 pm
Posts: 8776
Anonymous wrote:
how do I get a usb device VID and PID?


You can view it from Windows 'Device Manager' or use command-line tool like DevCon

If you need to find Device Instance ID for your USB Flash Drive, then you may insert
'Clipboard :=' in the following line
Code:
Clipboard := DeviceID := USBD_GetDeviceID( Serial )

in USBD_SafelyRemove()


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: June 3rd, 2011, 9:29 am 
Quote:
USBD_SafelyRemove() would work with a drive letter, but not ID.

You can get the ID and assign the driveletter and then use USBD_SafelyRemove() to assure its the right device


Report this post
Top
  
Reply with quote  
 Post subject:
PostPosted: June 4th, 2011, 10:33 pm 
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 )
}


Report this post
Top
  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 37 posts ]  Go to page Previous  1, 2, 3

All times are UTC [ DST ]


Who is online

Users browsing this forum: No registered users and 20 guests


You can post new topics in this forum
You can reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Powered by phpBB® Forum Software © phpBB Group