本帖最后由 itsky2 于 2020-11-11 18:38 编辑
Global Const $tagSTORAGE_PROPERTY_QUERY = 'ULONG_ptr PropertyId;' & _
'ULONG_ptr QueryType;' & _
'byte AdditionalParameters[4]'
Global Const $tagSTORAGE_DESCRIPTOR_HEADER = 'dword Version;' & _
'dword Size'
Func _Get_DiskName($sDrive)
Local $a_HardwareInfo[3], $aReturn, $tTemp, $ptrTemp
Local $hKernel32 = DllOpen('Kernel32.dll')
$aReturn = DllCall($hKernel32, 'ptr', 'CreateFileW', _
'wstr', '\\.\' & $sDrive, _
'dword', 0, _
'dword', 7, _
'ptr', 0, _
'dword', 3, _
'dword', 0, _
'ptr', 0 _
)
If @error Or $aReturn[0] = Ptr(-1) Then Return SetError(@error, @extended, '')
Local $hDevice = $aReturn[0]
Local $tSTORAGE_PROPERTY_QUERY = DllStructCreate($tagSTORAGE_PROPERTY_QUERY)
DllStructSetData($tSTORAGE_PROPERTY_QUERY, 'PropertyId', 0)
DllStructSetData($tSTORAGE_PROPERTY_QUERY, 'QueryType', 0)
Local $tSTORAGE_DESCRIPTOR_HEADER = DllStructCreate($tagSTORAGE_DESCRIPTOR_HEADER)
$aReturn = DllCall($hKernel32, 'int', 'DeviceIoControl', _
'handle', $hDevice, _
'dword', 0x002D1400, _
'ptr', DllStructGetPtr($tSTORAGE_PROPERTY_QUERY), _
'dword', DllStructGetSize($tSTORAGE_PROPERTY_QUERY), _
'ptr', DllStructGetPtr($tSTORAGE_DESCRIPTOR_HEADER), _
'dword', DllStructGetSize($tSTORAGE_DESCRIPTOR_HEADER), _
'dword*', 0, _
'ptr', 0 _
)
If @error Then Return SetError(@error, @extended, '')
Local $bRemoveableMedia = False
If DllStructGetData($tSTORAGE_DESCRIPTOR_HEADER, "Size") Then
Local Const $tagSTORAGE_DEVICE_DESCRIPTOR = 'ulong Version;' & _
'ulong Size;' & _
'byte DeviceType;' & _
'byte DeviceTypeModifier;' & _
'boolean RemovableMedia;' & _
'boolean CommandQueueing;' & _
'ulong VendorIdOffset;' & _
'ulong ProductIdOffset;' & _
'ulong ProductRevisionOffset;' & _
'ulong SerialNumberOffset;' & _
'ulong BusType;' & _
'ulong RawPropertiesLength;' & _
'byte RawDeviceProperties[' & DllStructGetData($tSTORAGE_DESCRIPTOR_HEADER, "Size") & ']'
Local $tSTORAGE_DEVICE_DESCRIPTOR = DllStructCreate($tagSTORAGE_DEVICE_DESCRIPTOR)
$aReturn = DllCall($hKernel32, 'int', 'DeviceIoControl', _
'handle', $hDevice, _
'dword', 0x002D1400, _
'ptr', DllStructGetPtr($tSTORAGE_PROPERTY_QUERY), _
'dword', DllStructGetSize($tSTORAGE_PROPERTY_QUERY), _
'ptr', DllStructGetPtr($tSTORAGE_DEVICE_DESCRIPTOR), _
'dword', DllStructGetSize($tSTORAGE_DEVICE_DESCRIPTOR), _
'dword*', 0, _
'ptr', 0 _
)
$bRemoveableMedia = DllStructGetData($tSTORAGE_DEVICE_DESCRIPTOR, "RemovableMedia")
If DllStructGetData($tSTORAGE_DEVICE_DESCRIPTOR, "VendorIdOffset") Then
$ptrTemp = DllStructGetPtr($tSTORAGE_DEVICE_DESCRIPTOR) + DllStructGetData($tSTORAGE_DEVICE_DESCRIPTOR, "VendorIdOffset")
$tTemp = DllStructCreate("char Temp[" & _WinAPI_StringLenA($ptrTemp) & "]", $ptrTemp)
$a_HardwareInfo[0] = DllStructGetData($tTemp, "Temp")
EndIf
If DllStructGetData($tSTORAGE_DEVICE_DESCRIPTOR, "ProductIdOffset") Then
$ptrTemp = DllStructGetPtr($tSTORAGE_DEVICE_DESCRIPTOR) + DllStructGetData($tSTORAGE_DEVICE_DESCRIPTOR, "ProductIdOffset")
$tTemp = DllStructCreate("char Temp[" & _WinAPI_StringLenA($ptrTemp) & "]", $ptrTemp)
$a_HardwareInfo[1] = DllStructGetData($tTemp, "Temp")
EndIf
If DllStructGetData($tSTORAGE_DEVICE_DESCRIPTOR, "ProductRevisionOffset") Then
$ptrTemp = DllStructGetPtr($tSTORAGE_DEVICE_DESCRIPTOR) + DllStructGetData($tSTORAGE_DEVICE_DESCRIPTOR, "ProductRevisionOffset")
$tTemp = DllStructCreate("char Temp[" & _WinAPI_StringLenA($ptrTemp) & "]", $ptrTemp)
$a_HardwareInfo[2] = DllStructGetData($tTemp, "Temp")
EndIf
EndIf
DllCall($hKernel32, 'int', 'CloseHandle', 'hwnd', $hDevice)
DllClose($hKernel32)
Return SetError(0, _
$bRemoveableMedia, _
StringRegExpReplace($a_HardwareInfo[0] & $a_HardwareInfo[1] & $a_HardwareInfo[2], '\h+', ' '))
EndFunc ;==>_Get_DiskName
传参:'PhysicalDrive' & 数字
|