USB Key

Share your advanced PureBasic knowledge/code with the community.
User avatar
Droopy
Enthusiast
Enthusiast
Posts: 658
Joined: Thu Sep 16, 2004 9:50 pm
Location: France
Contact:

USB Key

Post 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
rsts
Addict
Addict
Posts: 2736
Joined: Wed Aug 24, 2005 8:39 am
Location: Southwest OH - USA

Post by rsts »

absent without
present with
:)

cheers
User avatar
utopiomania
Addict
Addict
Posts: 1655
Joined: Tue May 10, 2005 10:00 pm
Location: Norway

Post 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
User avatar
Droopy
Enthusiast
Enthusiast
Posts: 658
Joined: Thu Sep 16, 2004 9:50 pm
Location: France
Contact:

Post 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 ?
User avatar
utopiomania
Addict
Addict
Posts: 1655
Joined: Tue May 10, 2005 10:00 pm
Location: Norway

Post 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.
Last edited by utopiomania on Wed Nov 30, 2005 10:50 am, edited 1 time in total.
User avatar
Droopy
Enthusiast
Enthusiast
Posts: 658
Joined: Thu Sep 16, 2004 9:50 pm
Location: France
Contact:

Post by Droopy »

thanks
Seldon
Enthusiast
Enthusiast
Posts: 405
Joined: Fri Aug 22, 2003 7:12 am
Location: Italia

Post by Seldon »

Just for curiosity... I can't see the code for FindCamera() ... am I blind ?? :)
Blade
Enthusiast
Enthusiast
Posts: 362
Joined: Wed Aug 06, 2003 2:49 pm
Location: Venice - Italy, Japan when possible.
Contact:

Post by Blade »

@Droopy
You code works great with my USB card reader.
Intrigued
Enthusiast
Enthusiast
Posts: 501
Joined: Thu Jun 02, 2005 3:55 am
Location: U.S.A.

Post 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)
Intrigued - Registered PureBasic, lifetime updates user
User avatar
Droopy
Enthusiast
Enthusiast
Posts: 658
Joined: Thu Sep 16, 2004 9:50 pm
Location: France
Contact:

Post by Droopy »

Thanks @All
josku_x
Addict
Addict
Posts: 997
Joined: Sat Sep 24, 2005 2:08 pm

Post 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
Bonne_den_kule
Addict
Addict
Posts: 841
Joined: Mon Jun 07, 2004 7:10 pm

Post 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?
User avatar
Droopy
Enthusiast
Enthusiast
Posts: 658
Joined: Thu Sep 16, 2004 9:50 pm
Location: France
Contact:

Post 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 
josku_x
Addict
Addict
Posts: 997
Joined: Sat Sep 24, 2005 2:08 pm

Post 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 :?
User avatar
blueznl
PureBasic Expert
PureBasic Expert
Posts: 6166
Joined: Sat May 17, 2003 11:31 am
Contact:

Post by blueznl »

printers with cardslots etc. will also report as a removable drive even when no cards are inserted
( PB6.00 LTS Win11 x64 Asrock AB350 Pro4 Ryzen 5 3600 32GB GTX1060 6GB)
( The path to enlightenment and the PureBasic Survival Guide right here... )
Post Reply