[DUG] USB device information

Phil Middlemiss phil at tumonz.co.nz
Thu Jul 21 16:06:45 NZST 2005


Here is some code that we use to scan for Garmin USB GPS devices. You 
will have to adapt it to your purposes, but it should give you an idea 
of the routines you need to use, and the order in which you need to use 
them:

    { scan for USB devices }
    deviceinfoset := SetupDiCreateDeviceInfoList(nil, 0);
    if (DWORD(deviceinfoset) <> INVALID_HANDLE_VALUE) then
      try
      // Retrieve the device information set for the interface class.
      guid := GUID_DEVINTERFACE_GRMNUSB;
      aguid := @GUID_DEVINTERFACE_GRMNUSB;
      newdeviceinfoset := SetupDiGetClassDevsEx(aguid, nil, 0, 
DIGCF_PRESENT or DIGCF_DEVICEINTERFACE,
        deviceinfoset, nil, nil);
      if(DWORD(newdeviceinfoset) <> INVALID_HANDLE_VALUE) then
        try
        { enumerate the devices }
        aindex := 0;
        while True do
          begin
          infodata.cbSize := SizeOf(TSPDevInfoData);
          { for each device }
          if SetupDiEnumDeviceInfo(newdeviceinfoset, aindex, infodata) then
            begin
            pinfodata := @infodata;
            bindex := 0;
            while True do
              begin
              { for each interface }
              interfacedata.cbSize := SizeOf(TSPDeviceInterfaceData);
              if SetupDiEnumDeviceInterfaces(newdeviceinfoset, 
pinfodata, guid, bindex, interfacedata) then
                begin
                pinterfacedata := @interfacedata;
                preqsize := @reqsize;
                { get the buffer size }
                if (not 
SetupDiGetDeviceInterfaceDetail(newdeviceinfoset, pinterfacedata, nil, 0,
                preqsize, nil)) and (GetLastError = 
ERROR_INSUFFICIENT_BUFFER) then
                  begin
                  { allocate the buffer }
                  pdetaildata := AllocMem(reqsize);
                  pdetaildata.cbSize := 
SizeOf(TSPDeviceInterfaceDetailData);
                  { get the details }
                    try
                    if SetupDiGetDeviceInterfaceDetail(newdeviceinfoset, 
pinterfacedata, pdetaildata, Cardinal(reqsize),
                    preqsize, pinfodata) then
                      begin
                      { check if the device has already been found }
                      apath := PChar(@pdetaildata.DevicePath);
                      alreadyfound := False;
                      for i := 0 to High(foundevices) do
                        begin
                        alreadyfound := foundevices[i].path = apath;
                        if alreadyfound then
                          begin
                          foundevices[i].found := True;
                          break;
                          end;
                        end;
                      if alreadyfound then
                        begin
                        Inc(bindex);
                        continue;
                        end;
                      { add the device }

                      <snip>

                    finally
                    FreeMem(pdetaildata);
                    end;
                  end;
                end
              else
                begin
                anerror := GetLastError;
                if anerror = ERROR_NO_MORE_ITEMS then
                  break;
                end;
              Inc(bindex);
              end;
            end
          else
            begin
            anerror := GetLastError;
            if anerror = ERROR_NO_MORE_ITEMS then
              break;
            end;
          Inc(aindex);
          end;
        finally
        SetupDiDestroyDeviceInfoList(newdeviceinfoset);
        end;
      finally
        SetupDiDestroyDeviceInfoList(deviceinfoset);
      end;



More information about the Delphi mailing list