SetupDiGetDeviceInterfaceDetail无法获取磁盘路径信息,求高手解答!
程序代码:
'驱动器API(https://msdn.(v=vs.85).aspx) Private Declare Function SetupDiGetClassDevs Lib "SetupAPI" Alias "SetupDiGetClassDevsA" (ClassGuid As Any, ByVal Enumerator As String, ByVal hParentWnd As Long, ByVal Flags As Long) As Long Private Declare Function SetupDiGetClassDevsEx Lib "SetupAPI" Alias "SetupDiGetClassDevsExA" (ClassGuid As Any, ByVal Enumerator As String, ByVal hParentWnd As Long, ByVal Flags As Long, DeviceInfoSet As Any, ByVal MachineName As String, ByVal Reserved As Long) As Long Private Declare Function SetupDiEnumDeviceInfo Lib "SetupAPI" (ByVal DeviceInfoSet As Long, ByVal MemberIndex As Long, DeviceInfoData As Any) As Long Private Declare Function SetupDiEnumDeviceInterfaces Lib "SetupAPI" (ByVal DeviceInfoSet As Long, DeviceInfoData As Any, InterfaceClassGuid As Any, ByVal MemberIndex As Long, DeviceInterfaceData As Any) As Long Private Declare Function SetupDiEnumDriverInfo Lib "SetupAPI" (ByVal DeviceInfoSet As Long, DeviceInfoData As Any, ByVal DriverType As Long, ByVal MemberIndex As Long, DeviceInterfaceData As Any) As Long Private Declare Function SetupDiGetDeviceInterfaceDetail Lib "SetupAPI" Alias "SetupDiGetDeviceInterfaceDetailA" (ByVal DeviceInfoSet As Long, DeviceInterfaceData As Any, DeviceInterfaceDetailData As Any, ByVal DeviceInterfaceDetailDataSize As Long, RequiredSize As Long, DeviceInfoData As Any) As Long Private Declare Function SetupDiInstallDeviceInterfaces Lib "SetupAPI" (ByVal DeviceInfoSet As Long, DeviceInfoData As Any) As Long Private Declare Function SetupDiRemoveDeviceInterface Lib "SetupAPI" (ByVal DeviceInfoSet As Long, DeviceInfoData As Any) As Long Private Declare Function SetupDiDestroyDriverInfoList Lib "SetupAPI" (ByVal DeviceInfoSet As Long, DeviceInfoData As Any, ByVal DriverType As Long) As Long Private Declare Function SetupDiDestroyDeviceInfoList Lib "SetupAPI" (ByVal DeviceInfoSet As Long) As Long Private Const DIGCF_DEFAULT = &H1 '// only valid with DIGCF_DEVICEINTERFACE Private Const DIGCF_PRESENT = &H2 Private Const DIGCF_ALLCLASSES = &H4 Private Const DIGCF_PROFILE = &H8 Private Const DIGCF_DEVICEINTERFACE = &H10 Private Const ERROR_NO_MORE_ITEMS = 259& Private Type GUID Data1 As Long Data2 As Integer Data3 As Integer Data4(7) As Byte End Type Private Type SP_DEVINFO_DATA cbSize As Long ClassGuid As GUID DevInst As Long Reserved As Long End Type Private Type SP_DEVICE_INTERFACE_DATA cbSize As Long InterfaceClassGuid As GUID Flags As Long Reserved As Long End Type Private Type SP_DEVICE_INTERFACE_DETAIL_DATA cbSize As Long DevicePath(519) As Byte End Type Private Const SPINT_ACTIVE = &H1 Private Const SPINT_DEFAULT = &H2 Private Const SPINT_REMOVED = &H4 '获取磁盘信息 Private Function EnumDiskVolumeInfo() As Long Dim DeviceInfoSet As Long, MemberIndex As Long, ReturnSize As Long, OldIrql As Long, DeviceDataPoint As Long, ErrCode As Long, VolumePath As String, DevicePath() As Byte, ClassGuid As GUID, DeviceInfo As SP_DEVINFO_DATA, DeviceInterFace As SP_DEVICE_INTERFACE_DATA, DeviceInterFaceDetail As SP_DEVICE_INTERFACE_DETAIL_DATA DeviceInfoSet = SetupDiGetClassDevs(ByVal 0, vbNullString, 0, DIGCF_ALLCLASSES Or DIGCF_DEVICEINTERFACE Or DIGCF_PROFILE) If DeviceInfoSet <> -1 Then MemberIndex = 0 ClassGuid.Data1 = &H53F56307 ClassGuid.Data2 = &HB6BF ClassGuid.Data3 = &H11D0 ClassGuid.Data4(0) = &H94 ClassGuid.Data4(1) = &HF2 ClassGuid.Data4(2) = 0 ClassGuid.Data4(3) = &HA0 ClassGuid.Data4(4) = &HC9 ClassGuid.Data4(5) = &H1E ClassGuid.Data4(6) = &HFB ClassGuid.Data4(7) = &H8B DeviceInterFace.cbSize = Len(DeviceInterFace) Do While SetupDiEnumDeviceInterfaces(DeviceInfoSet, ByVal 0, ClassGuid, MemberIndex, DeviceInterFace) SetupDiGetDeviceInterfaceDetail DeviceInfoSet, DeviceInterFace, ByVal 0, 0, ReturnSize, ByVal 0 If ReturnSize > 0 Then VolumePath = String(ReturnSize, vbNullChar) 'RtlInitUnicodeString DeviceDataPoint, VolumePath DeviceInterFaceDetail.cbSize = 8 'DeviceInterFaceDetail.DevicePath = VolumePath ' LocalAlloc(&H40, ReturnSize) 'LocalLock DeviceInterFaceDetail.DevicePath 'CopyMemory ByVal DeviceInterFaceDetail.DevicePath, ReturnSize, 4 SetupDiGetDeviceInterfaceDetail DeviceInfoSet, DeviceInterFace, DeviceInterFaceDetail, ReturnSize, ReturnSize, ByVal 0 Debug.Print GetLastError 'LocalUnlock DeviceInterFaceDetail.DevicePath 'LocalFree DeviceInterFaceDetail.DevicePath ZeroMemory DeviceInterFaceDetail, 8 End If MemberIndex = MemberIndex + 1 Loop SetupDiDestroyDeviceInfoList DeviceInfoSet End If EnumDiskVolumeInfo = MemberIndex End Function