VBA: Code pour déconnecter une clé USB

MJ13

XLDnaute Barbatruc
Bonjour à tous

Je cherche un code en VBA voire en .VBS ou .Bat pour déconnecter une clé USB.

Merci d'avance :).
 

MJ13

XLDnaute Barbatruc
Re : VBA: Code pour déconnecter une clé USB

Re

Bon, en fait cela ne sert pas à grand chose, vu que sous Win 7 et 8 voire 10, on peut retirer la clé sans cliquer sur l'icône.

Aller dans les propriétés du périphérique et vérifier qua la stratégie est bien configurer sur le choix 1.

C'est sûr qu'il vaut mieux attendre que la clé ai fini de copier les données.
 

Pièces jointes

  • USB_Statgégie.jpg
    USB_Statgégie.jpg
    72.7 KB · Affichages: 72

david84

XLDnaute Barbatruc
Re : VBA: Code pour déconnecter une clé USB

Bonjour,
à tester (entrer la lettre du périphérique à éjecter) :
Code:
Option Explicit

'Example by Howard Henry Schlunder
Private Declare Function GetVersion Lib "kernel32" () As Long
Private Declare Function CreateFile Lib "kernel32" Alias "CreateFileA" (ByVal lpFileName As String, ByVal dwDesiredAccess As Long, ByVal dwShareMode As Long, lpSecurityAttributes As Any, ByVal dwCreationDisposition As Long, ByVal dwFlagsAndAttributes As Long, ByVal hTemplateFile As Long) As Long
Private Declare Function DeviceIoControl Lib "kernel32" (ByVal hDevice As Long, ByVal dwIoControlCode As Long, lpInBuffer As Any, ByVal nInBufferSize As Long, lpOutBuffer As Any, ByVal nOutBufferSize As Long, lpBytesReturned As Long, lpOverlapped As Any) As Long
Private Declare Function CloseHandle Lib "kernel32" (ByVal hObject As Long) As Long


Private Const INVALID_HANDLE_VALUE = -1
Private Const OPEN_EXISTING = 3
Private Const FILE_FLAG_DELETE_ON_CLOSE = 67108864
Private Const GENERIC_READ = &H80000000
Private Const GENERIC_WRITE = &H40000000
Private Const IOCTL_STORAGE_EJECT_MEDIA = 2967560
Private Const VWIN32_DIOC_DOS_IOCTL = 1


Private Type DIOC_REGISTERS
  reg_EBX As Long
  reg_EDX As Long
  reg_ECX As Long
  reg_EAX As Long
  reg_EDI As Long
  reg_ESI As Long
  reg_Flags As Long
End Type


Sub EjecterDisque()
Dim hDrive As Long, DummyReturnedBytes As Long
Dim EjectDrive As String, DriveLetterAndColon As String
Dim RawStuff As DIOC_REGISTERS
  EjectDrive = InputBox("Which drive shall we try to eject the media from?", "Eject Media")
  If Len(EjectDrive) Then 'Confirm the user didn't cancel
    DriveLetterAndColon = UCase(Left$(EjectDrive & ":", 2)) 'Make it all caps for easy interpretation
    If GetVersion >= 0 Then 'We are running Windows NT/2000
      hDrive = CreateFile("\\.\" & DriveLetterAndColon, GENERIC_READ Or GENERIC_WRITE, 0, ByVal 0, OPEN_EXISTING, 0, 0)
      If hDrive <> INVALID_HANDLE_VALUE Then
        'Eject media!
        Call DeviceIoControl(hDrive, IOCTL_STORAGE_EJECT_MEDIA, 0, 0, 0, 0, DummyReturnedBytes, ByVal 0)
        Call CloseHandle(hDrive)  'Clean up after ourselves
      End If
    Else  'We are running Win9x/Me
      hDrive = CreateFile("\\.\VWIN32", 0, 0, ByVal 0, 0, FILE_FLAG_DELETE_ON_CLOSE, 0)
      If hDrive <> INVALID_HANDLE_VALUE Then
        'Setup our raw registers to use Interrupt 21h Function 440Dh Minor Code 49h
        RawStuff.reg_EAX = &H440D   'The function to use
        RawStuff.reg_EBX = Asc(DriveLetterAndColon) - Asc("A") + 1 'The drive to do it on
        RawStuff.reg_ECX = &H49 Or &H800     'The minor code of the function in the low byte of the low word and the device category of 8 in the high byte of the low word
        'Eject media!
        Call DeviceIoControl(hDrive, VWIN32_DIOC_DOS_IOCTL, RawStuff, LenB(RawStuff), RawStuff, LenB(RawStuff), DummyReturnedBytes, ByVal 0)
        Call CloseHandle(hDrive)  'Clean up after ourselves
      End If
    End If
  End If
End Sub
A+
 

Discussions similaires

Statistiques des forums

Discussions
312 147
Messages
2 085 768
Membres
102 969
dernier inscrit
pizza