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:\")