Вверх ↑
Ответов: 233
Рейтинг: 11
#1: 2021-09-28 23:40:54 ЛС | профиль | цитата
Как можно определить скорость USB порта к которому подключена флешка (UsbHighSpeed или UsbSuperSpeed)?
Через WMI не нашел подобного параметра. В интернете написано что можно через IOCTL-запрос IOCTL_USB_GET_NODE_CONNECTION_INFORMATION_EX но за 2 дня так и не понял как это сделать.
Нашел еще delphi код, может его можно прикрутить в в InlineCode


uses
Windows, TypInfo;

type
USB_DEVICE_SPEED=(UsbLowSpeed,
UsbFullSpeed,
UsbHighSpeed,
UsbSuperSpeed);
USB_CONNECTION_STATUS=(NoDeviceConnected,
DeviceConnected,
DeviceFailedEnumeration,
DeviceGeneralFailure,
DeviceCausedOvercurrent,
DeviceNotEnoughPower,
DeviceNotEnoughBandwidth,
DeviceHubNestedTooDeeply,
DeviceInLegacyHub,
DeviceEnumerating,
DeviceReset);
SP_DEVICE_INTERFACE_DATA=packed record
cbSize: dword;
InterfaceClassGuid: TGUID;
Flags: dword;
Reserved: pointer;
end;
PSP_DEVICE_INTERFACE_DATA=^SP_DEVICE_INTERFACE_DATA;
SP_DEVICE_INTERFACE_DETAIL_DATA_W=packed record
cbSize: dword;
DevicePath: array[0..0] of WideChar;
end;
PSP_DEVICE_INTERFACE_DETAIL_DATA_W=^SP_DEVICE_INTERFACE_DETAIL_DATA_W;
SP_DEVINFO_DATA=packed record
cbSize: dword;
ClassGuid: TGUID;
DevInst: dword;
Reserved: pointer;
end;
PSP_DEVINFO_DATA=^SP_DEVINFO_DATA;
USB_DEVICE_DESCRIPTOR=packed record
bLength: byte;
bDescriptorType: byte;
bcdUSB: word;
bDeviceClass: byte;
bDeviceSubClass: byte;
bDeviceProtocol: byte;
bMaxPacketSize0: byte;
idVendor: word;
idProduct: word;
bcdDevice: word;
iManufacturer: byte;
iProduct: byte;
iSerialNumber: byte;
bNumConfigurations: byte;
end;
USB_ENDPOINT_DESCRIPTOR=packed record
bLength: byte;
bDescriptorType: byte;
bEndpointAddress: byte;
bmAttributes: byte;
wMaxPacketSize: word;
bInterval: byte;
end;
USB_PIPE_INFO=packed record
EndpointDescriptor: USB_ENDPOINT_DESCRIPTOR;
ScheduleOffset: Cardinal;
end;
USB_NODE_CONNECTION_INFORMATION_EX=packed record
ConnectionIndex: Cardinal;
DeviceDescriptor: USB_DEVICE_DESCRIPTOR;
CurrentConfigurationValue: byte;
Speed: byte;
DeviceIsHub: boolean;
DeviceAddress: word;
NumberOfOpenPipes: Cardinal;
ConnectionStatus: USB_CONNECTION_STATUS;
PipeList: array[0..0] of USB_PIPE_INFO;
end;
USB_DESCRIPTOR_REQUEST=packed record
ConnectionIndex: Cardinal;
bmRequest: byte;
bRequest: byte;
wValue: word;
wIndex: word;
wLength: word;
length: byte;
DescriptorType: byte;
Data: array[0..2047] of WideChar;
end;

const
DIGCF_PRESENT=2;
DIGCF_DEVICEINTERFACE=16;
GUID_DEVINTERFACE_USB_HUB: TGUID = '{F18A0E88-C30C-11D0-8815-00A0C906BED8}';
FILE_DEVICE_USB=$22;
FILE_ANY_ACCESS=0;
USB_GET_DESCRIPTOR_FROM_NODE_CONNECTION=260;
USB_GET_NODE_CONNECTION_INFORMATION_EX=274;
METHOD_BUFFERED=0;
USB_STRING_DESCRIPTOR_TYPE=3;
IOCTL_USB_GET_NODE_CONNECTION_INFORMATION_EX=(FILE_DEVICE_USB shl 16)+(FILE_ANY_ACCESS shl 14)+(USB_GET_NODE_CONNECTION_INFORMATION_EX shl 2)+METHOD_BUFFERED;
IOCTL_USB_GET_DESCRIPTOR_FROM_NODE_CONNECTION=(FILE_DEVICE_USB shl 16)+(FILE_ANY_ACCESS shl 14)+(USB_GET_DESCRIPTOR_FROM_NODE_CONNECTION shl 2)+METHOD_BUFFERED;

function SetupDiGetDeviceInterfaceDetailW(DeviceInfoSet: LongInt; DeviceInterfaceData: PSP_DEVICE_INTERFACE_DATA; DeviceInterfaceDetailData: PSP_DEVICE_INTERFACE_DETAIL_DATA_W; DeviceInterfaceDetailDataSize: dword; RequiredSize: Pdword; DeviceInfoData: PSP_DEVINFO_DATA): LongBool;stdcall;external 'setupapi.dll';
function SetupDiGetClassDevsW(ClassGuid: PGUID; Enumerator: PWideChar; hwndParent: LongInt; Flags: dword): LongInt;stdcall;external 'setupapi.dll';
function SetupDiEnumDeviceInterfaces(DeviceInfoSet: LongInt; DeviceInfoData: PSP_DEVINFO_DATA; InterfaceClassGuid: PGUID; MemberIndex: dword; DeviceInterfaceData: PSP_DEVICE_INTERFACE_DATA): LongBool;stdcall;external 'setupapi.dll';

var
len: Cardinal;
DevInfoSet,i,j,f,stdout: LongInt;
DevInterface: SP_DEVICE_INTERFACE_DATA=(cbSize: sizeof(SP_DEVICE_INTERFACE_DATA));
DevIntDetail: array[0..2047] of WideChar;
ConInfo: USB_NODE_CONNECTION_INFORMATION_EX;
Request: USB_DESCRIPTOR_REQUEST=(wIndex: 1033;wLength: sizeof(Request.Data));

begin
stdout:=GetStdHandle( STD_OUTPUT_HANDLE);
DevInfoSet:=SetupDiGetClassDevsW(@GUID_DEVINTERFACE_USB_HUB,0,0,DIGCF_PRESENT+DIGCF_DEVICEINTERFACE);
i:=0;
while SetupDiEnumDeviceInterfaces(DevInfoSet,0,@GUID_DEVINTERFACE_USB_HUB,i,@DevInterface) do
begin
pdword(@DevIntDetail)^:=sizeof(SP_DEVICE_INTERFACE_DETAIL_DATA_W);
SetupDiGetDeviceInterfaceDetailW(DevInfoSet,@DevInterface,@DevIntDetail,sizeof(DevIntDetail),@f,0);
f:=CreateFileW(@DevIntDetail[2],GENERIC_WRITE+GENERIC_READ,FILE_SHARE_WRITE+FILE_SHARE_READ,0,OPEN_EXISTING,0,0);
ConInfo.ConnectionIndex:=1;
while DeviceIoControl(f,IOCTL_USB_GET_NODE_CONNECTION_INFORMATION_EX,@ConInfo,sizeof(ConInfo),@ConInfo,sizeof(ConInfo),len,0) do
begin
if ConInfo.DeviceDescriptor.bLength=0 then
break;
if ConInfo.ConnectionStatus=DeviceConnected then
begin
Request.ConnectionIndex:=ConInfo.ConnectionIndex;
Request.wValue:=(USB_STRING_DESCRIPTOR_TYPE shl 8)+ConInfo.DeviceDescriptor.iManufacturer;
DeviceIoControl(f,IOCTL_USB_GET_DESCRIPTOR_FROM_NODE_CONNECTION,@Request,sizeof(Request),@Request,sizeof(Request),len,0);
WriteConsoleW(stdout,@Request.Data,Request.Length shr 1-1,len,0);
write(' ');
Request.wValue:=(USB_STRING_DESCRIPTOR_TYPE shl 8)+ConInfo.DeviceDescriptor.iProduct;
DeviceIoControl(f,IOCTL_USB_GET_DESCRIPTOR_FROM_NODE_CONNECTION,@Request,sizeof(Request),@Request,sizeof(Request),len,0);
WriteConsoleW(stdout,@Request.Data,Request.Length shr 1-1,len,0);
write(' ');
Request.wValue:=(USB_STRING_DESCRIPTOR_TYPE shl 8)+ConInfo.DeviceDescriptor.iSerialNumber;
DeviceIoControl(f,IOCTL_USB_GET_DESCRIPTOR_FROM_NODE_CONNECTION,@Request,sizeof(Request),@Request,sizeof(Request),len,0);
WriteConsoleW(stdout,@Request.Data,Request.Length shr 1-1,len,0);
writeln(' ',GetEnumName(TypeInfo(USB_DEVICE_SPEED),ConInfo.Speed));
end;
inc(ConInfo.ConnectionIndex);
end;
CloseHandle(f);
inc(i);
end;
readln;
end.
карма: 2

0