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?
I'm not sure if you're just looking to enumerate drive letters? The for-loop below does that, going through all letters, regardless of whether there's a drive for that letter.
Or, if you're looking for a different way to find removable drives, there's a function for that below, too. (Yours may be better...) Surprisingly, on my test, Windows.GetDriveType does NOT consider CD drives as removable. USB drives are flagged as removable, as one would expect.
You can access this information using WMI. If you use this SQL you can access information about installed disks.
This code retrive information about drives.
You must add ActiveX and WbemScripting_TLB (this must be imported) in your uses. With this you can access all information of the disks.
To retrive the letter of all disk you can combine (retrieve can do with the same code) the access to the classes Win32_LogicalDiskToPartition and Win32_DiskDrive.
If you search WMI you can find more related codes.
Regards.