Page 2 of 2

Posted: Thu Jan 22, 2009 2:38 pm
by Seymour Clufley
graves wrote:
Joakim Christiansen wrote:Damn, I was looking for some code to eject a USB stick physically :lol:
:shock:
¿Launching it as a cannonball? :?:
It'd be like that weapon the killer uses in No Country For Old Men!

Posted: Thu Jan 22, 2009 6:56 pm
by SFSxOI
you mean the high pressure air gun thing?

Posted: Thu Jan 22, 2009 7:03 pm
by graves
SFSxOI wrote:you mean the high pressure air gun thing?
For me, it sounds like the torpedo of a submarine... OR a land-air missile.

Posted: Thu Jan 22, 2009 8:54 pm
by Seymour Clufley
SFSxOI wrote:you mean the high pressure air gun thing?
Yes!

Posted: Sat Jan 24, 2009 12:08 am
by Le Soldat Inconnu
No idea ?

I my computer first code works only this cd-rom

Second code works but he remove all USB Key.

Me i need to ject just one usb Key from his drive lettre, like M:\

I search way on internet but for this moment, nothing

Posted: Sun Jan 25, 2009 4:18 pm
by Le Soldat Inconnu
For this moment, i'm here :

I arrive to knom if device is removeable, the name and description of device
But i don't find yet drive letter in relation with my device

And i correct error in last code, if you don't reset *MemoryID before use, and function back nothing, you keep in *MemoryId the last result
Finnally you can eject what you don't want

Code: Select all

Procedure RemoveDevice(DriveLetter.s)
  #DIGCF_PRESENT = $2
  #DIGCF_ALLCLASSES = $4
  
  ; Constantes pour SetupDiGetDeviceRegistryProperty
  #SPDRP_ADDRESS = $1C
  #SPDRP_BUSNUMBER = $15
  #SPDRP_BUSTYPEGUID = $13
  #SPDRP_CAPABILITIES = $F
  #SPDRP_CHARACTERISTICS = $1B
  #SPDRP_CLASS = $7
  #SPDRP_CLASSGUID = $8
  #SPDRP_COMPATIBLEIDS = $2
  #SPDRP_CONFIGFLAGS = $A
  #SPDRP_DEVICEDESC = $0
  #SPDRP_DEVTYPE = $19
  #SPDRP_DRIVER = $9
  #SPDRP_ENUMERATOR_NAME = $16
  #SPDRP_EXCLUSIVE = $1A
  #SPDRP_FRIENDLYNAME = $C
  #SPDRP_HARDWAREID = $1
  #SPDRP_LEGACYBUSTYPE = $14
  #SPDRP_LOCATION_INFORMATION = $D
  #SPDRP_LOWERFILTERS = $12
  #SPDRP_MAXIMUM_PROPERTY = $1C
  #SPDRP_MFG = $B
  #SPDRP_PHYSICAL_DEVICE_OBJECT_NAME = $E
  #SPDRP_SECURITY = $17
  #SPDRP_SECURITY_SDS = $18
  #SPDRP_SERVICE = $4
  #SPDRP_UI_NUMBER = $10
  #SPDRP_UI_NUMBER_DESC_FORMAT = $1E
  #SPDRP_UNUSED0 = $3
  #SPDRP_UNUSED1 = $5
  #SPDRP_UNUSED2 = $6
  #SPDRP_UPPERFILTERS = $11
  
  ; Caractéristiques du device
  #CM_DEVCAP_LOCKSUPPORTED  = $1
  #CM_DEVCAP_EJECTSUPPORTED  = $2
  #CM_DEVCAP_REMOVABLE  = $4
  #CM_DEVCAP_DOCKDEVICE  = $8
  #CM_DEVCAP_UNIQUEID  = $10
  #CM_DEVCAP_SILENTINSTALL  = $20
  #CM_DEVCAP_RAWDEVICEOK  = $40
  #CM_DEVCAP_SURPRISEREMOVALOK  = $80
  #CM_DEVCAP_HARDWAREDISABLED  = $100
  #CM_DEVCAP_NONDYNAMIC  = $200
  
  Structure SP_DEVINFO_DATA
    cbSize.l
    ClassGuid.GUID
    DevInst.l
    reserved.l
  EndStructure
  
  #Memoire_Taille = 200
  
  Protected DeviceInfoData.SP_DEVINFO_DATA, DeviceCapabilities.l
  Protected *Memoire, Index
  
  DeviceInfoData\cbSize = SizeOf(DeviceInfoData)
  
  hDeviceInfoSet = SetupDiGetClassDevs_(0, 0, 0, #DIGCF_PRESENT | #DIGCF_ALLCLASSES)
  If hDeviceInfoSet <>#INVALID_HANDLE_VALUE
    Library = OpenLibrary(#PB_Any, "setupapi.dll")
    If Library
      
      *SetupDiGetDeviceInstanceId = GetFunction(Library, "SetupDiGetDeviceInstanceIdA")
      *SetupDiGetDeviceRegistryProperty = GetFunction(Library, "SetupDiGetDeviceRegistryPropertyA")
      *CM_Locate_DevNode = GetFunction(Library, "CM_Locate_DevNodeA")
      *CM_Request_Device_Eject = GetFunction(Library, "CM_Request_Device_Eject_ExA")
      *CM_Get_DevNode_Registry_Property = GetFunction(Library, "CM_Get_DevNode_Registry_PropertyA")
      
      While SetupDiEnumDeviceInfo_(hDeviceInfoSet, Index, @DeviceInfoData)
        
        *DeviceService = AllocateMemory(#Memoire_Taille)
        *DevicePhysicalName = AllocateMemory(#Memoire_Taille)
        *DeviceName = AllocateMemory(#Memoire_Taille)
        *DeviceDescription = AllocateMemory(#Memoire_Taille)
        
        CallFunctionFast(*SetupDiGetDeviceRegistryProperty, hDeviceInfoSet, DeviceInfoData, #SPDRP_SERVICE, 0, *DeviceService, #Memoire_Taille, 0)
        Debug PeekS(*DeviceService)
        CallFunctionFast(*SetupDiGetDeviceRegistryProperty, hDeviceInfoSet, DeviceInfoData, #SPDRP_LOCATION_INFORMATION, 0, *DevicePhysicalName, #Memoire_Taille, 0)
        Debug PeekS(*DevicePhysicalName)
        CallFunctionFast(*SetupDiGetDeviceRegistryProperty, hDeviceInfoSet, DeviceInfoData, #SPDRP_FRIENDLYNAME, 0, *DeviceName, #Memoire_Taille, 0)
        Debug PeekS(*DeviceName)
        CallFunctionFast(*SetupDiGetDeviceRegistryProperty, hDeviceInfoSet, DeviceInfoData, #SPDRP_DEVICEDESC, 0, *DeviceDescription, #Memoire_Taille, 0)
        Debug PeekS(*DeviceDescription)
        CallFunctionFast(*SetupDiGetDeviceRegistryProperty, hDeviceInfoSet, DeviceInfoData, #SPDRP_CAPABILITIES, 0, @DeviceCapabilities, 4, 0)
        Debug DeviceCapabilities
        ; If DeviceCapabilities & #CM_DEVCAP_LOCKSUPPORTED
          ; Debug "CM_DEVCAP_LOCKSUPPORTED"
        ; EndIf
        ; If DeviceCapabilities & #CM_DEVCAP_EJECTSUPPORTED
          ; Debug "CM_DEVCAP_EJECTSUPPORTED"
        ; EndIf
        If DeviceCapabilities & #CM_DEVCAP_REMOVABLE
          Debug "CM_DEVCAP_REMOVABLE"
        EndIf
        ; If DeviceCapabilities & #CM_DEVCAP_DOCKDEVICE
          ; Debug "CM_DEVCAP_DOCKDEVICE"
        ; EndIf
        ; If DeviceCapabilities & #CM_DEVCAP_UNIQUEID
          ; Debug "CM_DEVCAP_UNIQUEID"
        ; EndIf
        ; If DeviceCapabilities & #CM_DEVCAP_SILENTINSTALL
          ; Debug "CM_DEVCAP_SILENTINSTALL"
        ; EndIf
        ; If DeviceCapabilities & #CM_DEVCAP_RAWDEVICEOK
          ; Debug "CM_DEVCAP_RAWDEVICEOK"
        ; EndIf
        ; If DeviceCapabilities & #CM_DEVCAP_SURPRISEREMOVALOK
          ; Debug "CM_DEVCAP_RAWDEVICEOK"
        ; EndIf
        ; If DeviceCapabilities & #CM_DEVCAP_HARDWAREDISABLED
          ; Debug "CM_DEVCAP_HARDWAREDISABLED"
        ; EndIf
        ; If DeviceCapabilities & #CM_DEVCAP_NONDYNAMIC
          ; Debug "CM_DEVCAP_NONDYNAMIC"
        ; EndIf
        
        ;- Pour éjecter toutes les clés USB
        If UCase(PeekS(*DeviceService)) = "USBSTOR"
          *DeviceId = AllocateMemory(#Memoire_Taille)
          CallFunctionFast(*SetupDiGetDeviceInstanceId, hDeviceInfoSet, DeviceInfoData, *DeviceId, #Memoire_Taille, 0)
          Debug PeekS(*DeviceId)
          If CallFunctionFast(*CM_Locate_DevNode, @DevInst.l, PeekS(*DeviceId), 0) = 0
            CallFunctionFast(*CM_Request_Device_Eject, DevInst, 0, 0, 0, 0, 0)
          EndIf
          FreeMemory(*DeviceId)
        EndIf
        
        FreeMemory(*DeviceService)
        FreeMemory(*DevicePhysicalName)
        FreeMemory(*DeviceName)
        FreeMemory(*DeviceDescription)
        
        
        Debug "_____________________"
        Index + 1
      Wend
      
      CloseLibrary(Librairie)
    EndIf
    SetupDiDestroyDeviceInfoList_(hDeviceInfoSet)
  EndIf
  
  
  Debug "Fin"
  
EndProcedure

RemoveDevice("M:\")

Posted: Sun Jan 25, 2009 5:45 pm
by Le Soldat Inconnu
i find exemple in VB, but for this moment, i don't arrive to translate second code

http://www.codeproject.com/KB/system/Re ... etter.aspx

first code is

Code: Select all

hVolume = CreateFile_("\\.\L:", #GENERIC_READ|#GENERIC_WRITE, #FILE_SHARE_READ | #FILE_SHARE_WRITE, 0, #OPEN_EXISTING, 0, 0)
If hVolume <> #INVALID_HANDLE_VALUE
  Debug hVolume
  sdn.STORAGE_DEVICE_NUMBER
  dwBytesReturned.l
  res = DeviceIoControl_(hVolume, #IOCTL_STORAGE_GET_DEVICE_NUMBER, 0, 0, @sdn, SizeOf(STORAGE_DEVICE_NUMBER), @dwBytesReturned, 0)
  If res
    Debug sdn\DeviceNumber
    
  EndIf
  CloseHandle_(hVolume)
    
EndIf

Posted: Tue Feb 03, 2009 5:41 pm
by Le Soldat Inconnu
So

I do new code

I can remove all device from drive letter
And it's work with all USB device : CDROM, HardDisk, USB KEY, USB U3, Memroy Card, ...

Code is here (french forum)
http://www.purebasic.fr/french/viewtopic.php?t=8955

Library here (code, example and PB library)
http://www.lsi-dev.com/index.php?mod=ar ... voir&id=44