Page 1 of 2

USB Key

Posted: Tue Nov 29, 2005 9:19 pm
by Droopy
Someone can test this code with XP ?

Code: Select all

;/ Droopy 28/11/05
; Return 1 if a Removable Media is detected ( Probably a USB Key ) / 0 instead

Procedure DetectUSBKey()
  For n=2 To 26
    If Realdrivetype_(n,0)=  #DRIVE_REMOVABLE
      ProcedureReturn 1
    EndIf
  Next
EndProcedure

;/ Test
If DetectUSBKey()
  MessageRequester("Removable Drive","Present",#MB_ICONINFORMATION)
Else
  MessageRequester("Removable Drive","Absent",#MB_ICONERROR )
EndIf

Posted: Tue Nov 29, 2005 9:24 pm
by rsts
absent without
present with
:)

cheers

Posted: Tue Nov 29, 2005 9:38 pm
by utopiomania
Thanks, here's some similar snippets, used to detect a removable disk type digital camera. FindCamera()
checks the drives for a #DRIVE_REMOVABLE type disk.

Code: Select all

;Used by GetDeviceEvent()
#WM_DEVICECHANGE = $219
#DBT_DEVICEARRIVAL = $8000 ;A device has been inserted
#DBT_DEVICEREMOVECOMPLETE = $8004 ;A device has been removed.
.
.
.
;Set a callback to handle removable device events
SetWindowCallback(@GetDeviceEvent())
.
.
.
Procedure GetDeviceEvent(WindowID, Message, wParam, lParam)
  ;Callback procedure to handle camera connect/disconnect events
  Result = #PB_ProcessPureBasicEvents
  If Message = #WM_DEVICECHANGE
    Select wParam
      Case #DBT_DEVICEARRIVAL
        ;Removable device inserted
        ;Detect a camera
        If FindCamera()
          ;Camera detected
          ;
        Else
          ;Camera not connected
        EndIf
      Case #DBT_DEVICEREMOVECOMPLETE
        ;Removable device removed
        If FindCamera() = 0
          ;Camera not found
        EndIf
    EndSelect
  EndIf
  ProcedureReturn Result  
EndProcedure

Posted: Tue Nov 29, 2005 10:17 pm
by Droopy
Thanks a lot :D

Just a question, what is the code for :

Code: Select all

If FindCamera()
and how to know if the device is a camera ?

Posted: Tue Nov 29, 2005 10:44 pm
by utopiomania
FindCamera() returns true if the device is a #DRIVE_REMOVABLE , And a recursive search find a DCIM folder on it, where the pics are.

Posted: Wed Nov 30, 2005 9:10 am
by Droopy
thanks

Posted: Wed Nov 30, 2005 9:39 am
by Seldon
Just for curiosity... I can't see the code for FindCamera() ... am I blind ?? :)

Posted: Wed Nov 30, 2005 11:29 pm
by Blade
@Droopy
You code works great with my USB card reader.

Posted: Fri Dec 02, 2005 1:18 am
by Intrigued
This saw my ("Present" dialog returned) 128MB Jumpdrive - thumb drive - USB unit.

Coo'!

(Windows XP Pro, all updates, single removable drive unit attached)

Posted: Fri Dec 02, 2005 9:01 am
by Droopy
Thanks @All

Posted: Fri Dec 02, 2005 3:59 pm
by josku_x
Sorry I am having a problem, the program returns "Present" even if I have OR don't have a USB Key plugged.

I checked all my USB ports and none of them has a USB Key plugged, when I ran the code it returned "Present". Then I tested to insert a USB Key in one of my USB ports and the program still outputs "Present" :cry:

Using Windows XP Pro 32-bit

Posted: Fri Dec 02, 2005 4:11 pm
by Bonne_den_kule
josku_x wrote:Sorry I am having a problem, the program returns "Present" even if I have OR don't have a USB Key plugged.

I checked all my USB ports and none of them has a USB Key plugged, when I ran the code it returned "Present". Then I tested to insert a USB Key in one of my USB ports and the program still outputs "Present" :cry:

Using Windows XP Pro 32-bit
Do you have an external/USB HD?

Posted: Fri Dec 02, 2005 4:37 pm
by Droopy
Try this and run it in Debug Mode to find wich device is removable.

Code: Select all

;/ Droopy 28/11/05 
; Return 1 if a Removable Media is detected ( Probably a USB Key ) / 0 instead 

Procedure DetectUSBKey() 
  For n=2 To 26 
    If Realdrivetype_(n,0)=  #DRIVE_REMOVABLE
      Debug Chr(n+65)+": Drive is a Removable Drive" 
      ProcedureReturn 1 
    EndIf 
  Next 
EndProcedure 

;/ Test 
If DetectUSBKey() 
  MessageRequester("Removable Drive","Present",#MB_ICONINFORMATION) 
Else 
  MessageRequester("Removable Drive","Absent",#MB_ICONERROR ) 
EndIf 

Posted: Fri Dec 02, 2005 11:39 pm
by josku_x
Thanks for your answer Droopy, but I'm now at a friend so I'll check the code later...
Do you have an external/USB HD?
No, I don't have anything there, that's why I am confused about :?

Posted: Sat Dec 03, 2005 12:30 am
by blueznl
printers with cardslots etc. will also report as a removable drive even when no cards are inserted