Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Get list of USB devices

822 views
Skip to first unread message

Diego Barros

unread,
Jan 16, 2005, 8:58:35 PM1/16/05
to
Hi all,

I was wondering how I would go about getting a list of any attached USB
devices to a Windows machine? USB devices being such things as flash
drives, etc. Any help would be appreciated.

Regards,
Diego

Robert Marquardt

unread,
Jan 17, 2005, 12:59:11 AM1/17/05
to

It is possible to enumerate the USB devices, but the list is more or
less useless because you will enumerate drivers/devices which are often
used by other drivers.
For USB sticks you should enumerate the volumes.

Please contact me directly and i will send you some example source.

Joanna Carter (TeamB)

unread,
Jan 17, 2005, 4:15:37 AM1/17/05
to
"Robert Marquardt" <robert_m...@gmx.de> a écrit dans le message de
news: 41eb...@newsgroups.borland.com...

> Please contact me directly and i will send you some example source.

Please do not take topics offline like this, other people may want to see
the answer as well. Please post your reply to this thread, putting any large
files in the attachments group if necessary.

Joanna

--
Joanna Carter (TeamB)

Consultant Software Engineer
TeamBUG support for UK-BUG
TeamMM support for ModelMaker


Robert Marquardt

unread,
Jan 17, 2005, 10:02:04 AM1/17/05
to
Joanna Carter (TeamB) wrote:

> Please do not take topics offline like this, other people may want to see
> the answer as well. Please post your reply to this thread, putting any large
> files in the attachments group if necessary.

That example will soon be part of the Jedi-Apilib (SourceForge), but i
do not want to have it circulate too much before that.
It is part of the Config Manager API conversion.

Joanna Carter (TeamB)

unread,
Jan 17, 2005, 4:22:30 PM1/17/05
to
"Robert Marquardt" <robert_m...@gmx.de> a écrit dans le message de
news: 41eb...@newsgroups.borland.com...

> That example will soon be part of the Jedi-Apilib (SourceForge), but i


> do not want to have it circulate too much before that.
> It is part of the Config Manager API conversion.

I understand; could you recreate a small sample that would demonstrate the
principle ?

Robert Marquardt

unread,
Jan 17, 2005, 11:57:48 PM1/17/05
to
Joanna Carter (TeamB) wrote:

> I understand; could you recreate a small sample that would demonstrate the
> principle ?

Tomorrow. I deleted the wrong file.

Robert Marquardt

unread,
Jan 18, 2005, 12:28:28 AM1/18/05
to
Joanna Carter (TeamB) wrote:

> I understand; could you recreate a small sample that would demonstrate the
> principle ?

Here is the central part enumerating the volumes and (not) checking for
USB sticks:

function GetDriveInstanceID(MountPointName: string; var DeviceInst:
DEVINST): Boolean;
const
GUID_DEVINTERFACE_VOLUME: TGUID =
'{53f5630d-b6bf-11d0-94f2-00a0c91efb8b}';
var
StorageGUID: TGUID;
PnPHandle: HDEVINFO;
DevData: TSPDevInfoData;
DeviceInterfaceData: TSPDeviceInterfaceData;
FunctionClassDeviceData: PSPDeviceInterfaceDetailData;
Success: LongBool;
Devn: Integer;
BytesReturned: DWORD;
Inst: DEVINST;
S, FileName, MountName, DevID: string;
begin
Result := False;
StorageGUID := GUID_DEVINTERFACE_VOLUME;
PnPHandle := SetupDiGetClassDevs(@StorageGUID, nil, 0,
DIGCF_PRESENT or DIGCF_DEVICEINTERFACE);
if PnPHandle = Pointer(INVALID_HANDLE_VALUE) then
Exit;
Devn := 0;
repeat
DeviceInterfaceData.cbSize := SizeOf(TSPDeviceInterfaceData);
Success := SetupDiEnumDeviceInterfaces(PnPHandle, nil, StorageGUID,
Devn, DeviceInterfaceData);
if Success then
begin
DevData.cbSize := SizeOf(DevData);
BytesReturned := 0;
SetupDiGetDeviceInterfaceDetail(PnPHandle, @DeviceInterfaceData,
nil, 0, BytesReturned, @DevData);
if (BytesReturned <> 0) and
(GetLastError = ERROR_INSUFFICIENT_BUFFER) then
begin
FunctionClassDeviceData := AllocMem(BytesReturned);
try
FunctionClassDeviceData.cbSize :=
SizeOf(TSPDeviceInterfaceDetailData);
if SetupDiGetDeviceInterfaceDetail(PnPHandle,
@DeviceInterfaceData, FunctionClassDeviceData,
BytesReturned, BytesReturned, @DevData) then
begin
// the trick to find if it is on a USB device
FileName := PTSTR(@FunctionClassDeviceData.DevicePath[0]);
Inst := DevData.DevInst;
CM_Get_Parent(Inst, Inst, 0);
CM_Get_Parent(Inst, Inst, 0);
DevID := GetDeviceID(Inst);

// no need in this example to check for USB only
// if Pos('USB\', DevID) = 1 then
begin
S := '\';
S := PTSTR(@FunctionClassDeviceData.DevicePath) + S;
MountName := GetVolumeNameForVolumeMountPointString(S);
if MountName = MountPointName then
begin
DeviceInst := Inst;
Result := True;
Exit;
end;
end;
end;
finally
FreeMem(FunctionClassDeviceData);
end;
end;
end;
Inc(Devn);
until not Success;
SetupDiDestroyDeviceInfoList(PnPHandle);
end;

0 new messages