最近要做一段程序来自动卸载U盘 代码已找到 以下是关闭句柄部分
 但为什么载执行 lngReturn = CloseHandle(lngVolHandle) 一句后还有U盘的图标存在  那位大侠帮小弟解答一下万分感激  注:返回值 lngReturn  执行后不为0Private Function CloseVolume(lngVolHandle As Long) As Boolean
Dim lngReturn As Long
lngReturn = CloseHandle(lngVolHandle)
If lngReturn = 0 Then
CloseVolume = False
Else
CloseVolume = True
End If
End Function

解决方案 »

  1.   

    Option Explicit
    Dim boTimeOut As BooleanPrivate Const DRIVE_CDROM As Long = 5
    Private Const DRIVE_REMOVABLE As Long = 2Private Const GENERIC_READ As Long = &H80000000
    Private Const GENERIC_WRITE As Long = &H40000000Private Const OPEN_EXISTING As Long = 3
    Private Const FILE_DEVICE_FILE_SYSTEM As Long = 9
    Private Const FILE_DEVICE_MASS_STORAGE As Long = &H2D&
    Private Const METHOD_BUFFERED As Long = 0
    Private Const FILE_ANY_ACCESS As Long = 0
    Private Const FILE_READ_ACCESS As Long = 1
    Private Const LOCK_VOLUME As Long = 6
    Private Const DISMOUNT_VOLUME As Long = 8
    Private Const EJECT_MEDIA As Long = &H202
    Private Const MEDIA_REMOVAL As Long = &H201
    Private Const INVALID_HANDLE_VALUE As Long = -1Private Const LOCK_TIMEOUT As Long = 1000
    Private Const LOCK_RETRIES As Long = 20Private Declare Function GetDriveType Lib "kernel32.dll" _
    Alias "GetDriveTypeA" _
    (ByVal nDrive As String) As LongPrivate Declare Function CloseHandle Lib "kernel32.dll" _
    (ByVal hObject As Long) As LongPrivate Declare Function CreateFile Lib "kernel32.dll" Alias "CreateFileA" _
    (ByVal lpFileName As String, _
    ByVal dwDesiredAccess As Long, _
    ByVal dwShareMode As Long, _
    ByRef lpSecurityAttributes As Long, _
    ByVal dwCreationDisposition As Long, _
    ByVal dwFlagsAndAttributes As Long, _
    ByVal hTemplateFile As Long) As LongPrivate Declare Function DeviceIoControl Lib "kernel32.dll" _
    (ByVal hDevice As Long, _
    ByRef dwIoControlCode As Long, _
    ByRef lpInBuffer As Any, _
    ByVal nInBufferSize As Long, _
    ByRef lpOutBuffer As Any, _
    ByVal nOutBufferSize As Long, _
    ByRef lpBytesReturned As Long, _
    ByRef lpOverlapped As Long) As Long
    Private Function CTL_CODE(lngDevFileSys As Long, lngFunction As Long, _
    lngMethod As Long, lngAccess As Long) As Long
    CTL_CODE = (lngDevFileSys * (2 ^ 16)) Or (lngAccess * (2 ^ 14)) Or (lngFunction * (2 ^ 2)) Or lngMethod
    End FunctionPrivate Function OpenVolume(strLetter As String, lngVolHandle As Long) As Boolean
    Dim lngDriveType As Long
    Dim lngAccessFlags As Long
    Dim strVolume As String
    lngDriveType = GetDriveType(strLetter)
    Select Case lngDriveType
    Case DRIVE_REMOVABLE
    lngAccessFlags = GENERIC_READ Or GENERIC_WRITE
    Case DRIVE_CDROM
    lngAccessFlags = GENERIC_READ
    Case Else
    OpenVolume = False
    Exit Function
    End Select
    strVolume = "\\.\" & strLetter
    lngVolHandle = CreateFile(strVolume, lngAccessFlags, 0, _
    ByVal CLng(0), OPEN_EXISTING, ByVal CLng(0), ByVal CLng(0))
    If lngVolHandle = INVALID_HANDLE_VALUE Then
    OpenVolume = False
    Exit Function
    End If
    OpenVolume = True
    End FunctionPrivate Function CloseVolume(lngVolHandle As Long) As Boolean
    Dim lngReturn As Long
    lngReturn = CloseHandle(lngVolHandle)
    If lngReturn = 0 Then
    CloseVolume = False
    Else
    CloseVolume = True
    End If
    End FunctionPrivate Function LockVolume(ByRef lngVolHandle As Long) As Boolean
    Dim lngBytesReturned As Long
    Dim intCount As Integer
    Dim intI As Integer
    Dim boLocked As Boolean
    Dim lngFunction As Long
    lngFunction = CTL_CODE(FILE_DEVICE_FILE_SYSTEM, LOCK_VOLUME, METHOD_BUFFERED, FILE_ANY_ACCESS)
    intCount = LOCK_TIMEOUT / LOCK_RETRIES
    boLocked = False
    For intI = 0 To LOCK_RETRIES
    boTimeOut = False
    Timer1.Interval = intCount
    Timer1.Enabled = True
    Do Until boTimeOut = True Or boLocked = True
    boLocked = DeviceIoControl(lngVolHandle, ByVal lngFunction, _
    CLng(0), 0, CLng(0), 0, lngBytesReturned, ByVal CLng(0))
    DoEvents
    Loop
    If boLocked = True Then
    LockVolume = True
    Timer1.Enabled = False
    Exit Function
    End If
    Next intI
    LockVolume = False
    End Function
    Private Function DismountVolume(lngVolHandle As Long) As Boolean
    Dim lngBytesReturned As Long
    Dim lngFunction As Long
    lngFunction = CTL_CODE(FILE_DEVICE_FILE_SYSTEM, DISMOUNT_VOLUME, METHOD_BUFFERED, FILE_ANY_ACCESS)
    DismountVolume = DeviceIoControl(lngVolHandle, ByVal lngFunction, _
    0, 0, 0, 0, lngBytesReturned, ByVal 0)
    End Function
    Private Function PreventRemovalofVolume(lngVolHandle As Long) As Boolean
    Dim boPreventRemoval As Boolean
    Dim lngBytesReturned As Long
    Dim lngFunction As Long
    boPreventRemoval = False
    lngFunction = CTL_CODE(FILE_DEVICE_MASS_STORAGE, MEDIA_REMOVAL, METHOD_BUFFERED, FILE_READ_ACCESS)
    PreventRemovalofVolume = DeviceIoControl(lngVolHandle, ByVal lngFunction, _
    boPreventRemoval, Len(boPreventRemoval), 0, 0, lngBytesReturned, ByVal 0)
    End Function
    Private Function AutoEjectVolume(lngVolHandle As Long) As Boolean
    Dim lngFunction As Long
    Dim lngBytesReturned As Long
    lngFunction = CTL_CODE(FILE_DEVICE_MASS_STORAGE, EJECT_MEDIA, METHOD_BUFFERED, FILE_READ_ACCESS)
    AutoEjectVolume = DeviceIoControl(lngVolHandle, ByVal lngFunction, _
    0, 0, 0, 0, lngBytesReturned, ByVal 0)
    End FunctionPrivate Sub Eject(strVol As String)
    Dim lngVolHand As Long
    Dim boResult As Boolean
    Dim boSafe As Boolean
    strVol = strVol & ":"
    '
    ' Open and get a Handle for the Volume
    '
    boResult = OpenVolume(strVol, lngVolHand)
    If boResult = False Then
    MsgBox "Error Opening Volume " & Err.LastDllError
    Exit Sub
    End If
    '
    ' Lock the Volume
    '
    boResult = LockVolume(lngVolHand)
    If boResult = False Then
    MsgBox "Error Dismounting Volume " & Err.LastDllError
    CloseVolume (lngVolHand)
    Exit Sub
    End If
    '
    'Dismount the Volume
    '
    boResult = DismountVolume(lngVolHand)
    If boResult = False Then
    MsgBox "Error Dismounting Volume " & Err.LastDllError
    CloseVolume (lngVolHand)
    Exit Sub
    End If
    '
    ' Set to allow the Volume to be Removed
    '
    boResult = PreventRemovalofVolume(lngVolHand)
    If boResult = False Then
    MsgBox "Error Allowing Removal of Volume " & Err.LastDllError
    CloseVolume (lngVolHand)
    Exit Sub
    End If
    boSafe = True
    '
    ' Eject the Volume
    '
    boResult = AutoEjectVolume(lngVolHand)
    If boSafe = True Then
    MsgBox "Media may be Safely Removed from Drive " & UCase(strVol)
    End If
    '
    ' Close the Handle
    '
    boResult = CloseVolume(lngVolHand)
    If boResult = False Then
    MsgBox "Error Closing Volume " & Err.LastDllError
    Exit Sub
    End If
    Unload Me
    End Sub
    Private Sub Command1_Click()
    Eject "J"
    End SubPrivate Sub Timer1_Timer()
    boTimeOut = True
    End Sub
      

  2.   

    Eject "J"  ' 卸载 J 盘
      

  3.   

    CloseHandle 返回值不为零表示成功,0表示失败, 
    你已经成功弹出U盘(相当于右键盘符,在弹出菜单上点 弹出),并不是删除USB设备我认为应该只是 仅对USBFDD有效.
      

  4.   

    卸载U盘C代码,看看哪位高手能不能解释成VB代码http://www.codeproject.com/useritems/HwDetect.asp
      

  5.   

    经过本人测试,以上代码卸载了本人的虚拟光驱里的虚拟光盘:)版本:DAEMON Tools 4.06HE