Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 1083097
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T22:23:18+00:00 2026-05-16T22:23:18+00:00

In my application (Delphi), I need to list all the USB storage devices. These

  • 0

In my application (Delphi), I need to list all the USB storage devices. These can be either flash memory sticks or external storage drives.

There is a Jvcl component JvDriveCombo, and it has the DriveType property – the problem is if I select DriveType := Fixed then in addition to the external drive, it also lists the internal drives (C:\, D:\ etc). However, I only want to list the external drives.

I believe there is DeviceIoControl function (I saw it on MSDN) but I have no idea of how to use it.

I wonder if anyone can help me with the proper way / code to list USB storage devices?

Thanks.

EDIT:

I just found some sample code and am posting it here:

uses .... jwawinbase, JwaWinIoctl;

procedure TForm1.Button1Click(Sender: TObject);
var
  DriveCmdStr: string;
  DriveHandle: THandle;
  ADriveLetter: string;
  hp: STORAGE_HOTPLUG_INFO;
  rlen: DWORD;
begin

  ADriveLetter := 'H';
  DriveCmdStr := Format('\\.\%s:', [ADriveLetter]);
  DriveHandle := CreateFile(PChar(DriveCmdStr), GENERIC_READ, FILE_SHARE_WRITE,
    nil, OPEN_EXISTING, 0, 0);

  if DriveHandle = INVALID_HANDLE_VALUE then
    Exit;

  DeviceIoControl(DriveHandle, IOCTL_STORAGE_GET_HOTPLUG_INFO, nil, 0, @hp,
    SizeOf(hp), @rlen, nil);

  CloseHandle(DriveHandle);

  if hp.MediaRemovable then
    showmessage('media removable');

end;

Now I would like to just know how to enumerate all the drive letters. Which is the most efficient function?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-16T22:23:18+00:00Added an answer on May 16, 2026 at 10:23 pm
    {$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;
    
    function 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 GetUsbDrives(List: TStrings);
    var
      DriveBits: set of 0..25;
      I: Integer;
      Drive: AnsiChar;
    begin
      List.BeginUpdate;
      try
        Cardinal(DriveBits) := GetLogicalDrives;
    
        for I := 0 to 25 do
          if I in DriveBits then
          begin
            Drive := Chr(Ord('a') + I);
            if GetBusType(Drive) = BusTypeUsb then
              List.Add(Drive);
          end;
      finally
        List.EndUpdate;
      end;
    end;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need to write a Delphi application that pulls entries up from various tables
What is the best way to make a Delphi application (Delphi 2007 for Win32)
I have a Delphi application similar to Taskbar Shuffle that includes a hook dll.
I'm writing an application in Delphi which uses an SQLite3 database. I'd like to
When I compile my application under Delphi 2006 I get the following warning [Pascal
I would like to create a Delphi application for Windows XP which allows dropping
Is there a good code snippets application for Delphi or general purpose with IDE
I'm using the TServerSocket component in my Delphi application. I would like to limit
Which component is best to use for receiving HTTP requests in Delphi application?
Background I want to be able to parse Javascript source in a Delphi Application.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.