Identify what is in the USB port

Asked

Viewed 2,133 times

5

When a device is connected to a USB port, the operating system identifies the device. For example, on Windows, you can see which devices are connected to USB ports in Device Manager.

I need to programmatically verify that a specific device is connected to the computer. Is there any way to get this information in Delphi?

  • 1

    Hello, you could check if this windows API has what you need to do? Winusb

  • Solves, but adds a level of complexity : But thank you!

  • Maybe you could use WMI, it’s an easier API. But it depends on what you want to do

  • Does it need to be in Delphi only? It could be another language like Python for example?

  • 1

    @Denercarvalho Can be in Python, as long as you use a library that can be used in Delphi (I am maintaining an existing system).

  • 1

    The libusb library works on Windows, but since it is a C library, it would have to interface (declare the functions with prototype compatible with C)

  • 1

    http://www.devmedia.com.br/pen-drive-usb-com-delphi is open source, I believe it can help

  • What version of your Android?

  • @Artur_indio 2006

Show 4 more comments

1 answer

2


Follow this example, I have tested it here at Delphi xe6:

unit Unit1;

interface

uses
  Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
  Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls;

{$MINENUMSIZE 4}

const
  IOCTL_STORAGE_QUERY_PROPERTY =  $002D1400;

type
  STORAGE_QUERY_TYPE = (PropertyStandardQuery = 0, PropertyExistsQuery, PropertyMaskQuery, PropertyQueryMaxDefined);
  TStorageQueryType = STORAGE_QUERY_TYPE;

  STORAGE_PROPERTY_ID = (StorageDeviceProperty = 0, StorageAdapterProperty);
  TStoragePropertyID = STORAGE_PROPERTY_ID;

  STORAGE_PROPERTY_QUERY = packed record
    PropertyId: STORAGE_PROPERTY_ID;
    QueryType: STORAGE_QUERY_TYPE;
    AdditionalParameters: array [0..9] of AnsiChar;
  end;

  TStoragePropertyQuery = STORAGE_PROPERTY_QUERY;
  STORAGE_BUS_TYPE = (BusTypeUnknown = 0, BusTypeScsi, BusTypeAtapi, BusTypeAta, BusType1394, BusTypeSsa, BusTypeFibre,
    BusTypeUsb, BusTypeRAID, BusTypeiScsi, BusTypeSas, BusTypeSata, BusTypeMaxReserved = $7F);
  TStorageBusType = STORAGE_BUS_TYPE;
  STORAGE_DEVICE_DESCRIPTOR = packed record
    Version: DWORD;
    Size: DWORD;
    DeviceType: Byte;
    DeviceTypeModifier: Byte;
    RemovableMedia: Boolean;
    CommandQueueing: Boolean;
    VendorIdOffset: DWORD;
    ProductIdOffset: DWORD;
    ProductRevisionOffset: DWORD;
    SerialNumberOffset: DWORD;
    BusType: STORAGE_BUS_TYPE;
    RawPropertiesLength: DWORD;
    RawDeviceProperties: array [0..0] of AnsiChar;

  end;

  TStorageDeviceDescriptor = STORAGE_DEVICE_DESCRIPTOR;

  TForm1 = class(TForm)
    mmo1: TMemo;
    btn1: TButton;
    procedure btn1Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  function GetBusType(Drive: AnsiChar): TStorageBusType;
  procedure GetUsbDrives(memo: TMemo);
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.btn1Click(Sender: TObject);
begin

  GetUsbDrives(mmo1);

end;

function TForm1.GetBusType(Drive: AnsiChar): TStorageBusType;
var
  H: THandle;
  Query: TStoragePropertyQuery;
  dwBytesReturned: DWORD;
  Buffer: array [0..1023] of Byte;
  sdd: TStorageDeviceDescriptor absolute Buffer;
  OldMode: UINT;
begin
  Result := BusTypeUnknown;

  OldMode := SetErrorMode(SEM_FAILCRITICALERRORS);
  try
    H := CreateFile(PChar(Format('\\.\%s:', [AnsiLowerCase(Drive)])), 0, FILE_SHARE_READ or FILE_SHARE_WRITE, nil,
      OPEN_EXISTING, 0, 0);
    if H <> INVALID_HANDLE_VALUE then
    begin
      try
        dwBytesReturned := 0;
        FillChar(Query, SizeOf(Query), 0);
        FillChar(Buffer, SizeOf(Buffer), 0);
        sdd.Size := SizeOf(Buffer);
        Query.PropertyId := StorageDeviceProperty;
        Query.QueryType := PropertyStandardQuery;
        if DeviceIoControl(H, IOCTL_STORAGE_QUERY_PROPERTY, @Query, SizeOf(Query), @Buffer, SizeOf(Buffer), dwBytesReturned, nil) then
          Result := sdd.BusType;
      finally
        CloseHandle(H);
      end;
    end;
  finally
    SetErrorMode(OldMode);
  end;
end;

procedure TForm1.GetUsbDrives(memo: TMemo);
var
  DriveBits: set of 0..25;
  I: Integer;
  Drive: AnsiChar;
begin
    Cardinal(DriveBits) := GetLogicalDrives;

    for I := 0 to 25 do
      if I in DriveBits then
      begin
        Drive := AnsiChar(Ord('a') + I);
        if GetBusType(Drive) = BusTypeUsb then
          memo.Lines.Add(Drive);
      end;
end;

end.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.