3

Olá, preciso fazer com que um "ComboBox" exiba a letra dos volumes dos pen drives que possam eventualmente estar conectados ao computador, como poderei fazer isto?

6
  • O que você tentou? 17/06/2015 às 13:28
  • Explique o que você quer dizer com isto! 17/06/2015 às 13:29
  • 1
    Pascal, poderia mostrar o que você já fez de código? O que você tem pronto? Quais as limitações que você tem? 17/06/2015 às 13:33
  • Limitação é o que eu mais tenho, estou começando agora, a questão é unicamente esta, é exibir o nome das unidades conectadas, só isto. 17/06/2015 às 13:46
  • 1
    @CaioFelipePereira pq não coloca como resposta citando a fonte?
    – Math
    17/06/2015 às 14:10

2 Respostas 2

2

isso pode te ajudar, ele adiciona em uma lista todos os drives que precisa.

uses ... FileCtrl, Vcl.StdCtrls

function ListaDrives : TStringList; 
var 
  Lista : TStringList; 
  DriveNum : Integer; 
  LetraDrive : Char; 
  DriveBits : set of 0..25; 
  TipoDrive : TDriveType; 
begin 
  Lista := TStringList.Create; 
  Integer (DriveBits) := GetLogicalDrives; 
  for DriveNum := 0 to 25 do 
    begin 
      if not (DriveNum in DriveBits) then 
        Continue; 
      LetraDrive := UpCase (Char (DriveNum + ord ('a'))); 
      TipoDrive := TDriveType (GetDriveType (PChar (LetraDrive + ':')));

      case TipoDrive of 
        dtFloppy : Lista.Add ('Drive ' + LetraDrive + ': - Disco
flexível'); 
        dtFixed  : Lista.Add ('Drive ' + LetraDrive + ': - Disco
rígido'); 
        dtCDROM  : Lista.Add ('Drive ' + LetraDrive + ': - CD-ROM'); 
        dtRAM    : Lista.Add ('Drive ' + LetraDrive + ': - RAM Disk'); 
        dtNetwork: Lista.Add ('Drive ' + LetraDrive + ': - Drive de
rede'); 
      end; 
    end; 
  Result := Lista; 
end; 
2
  • 1
    Cara gostei muito, só há um problema, não tenho ideia de como fazer com que esta função exiba os volumes no ComboBox, ou em qualquer outro componente. 23/06/2015 às 0:46
  • O retorno desta função é um TstringList, o combobox voce consegue adicionar item por item, dentro de um for receba a a posição i do string list de retorno da função =) 6/07/2015 às 18:46
2

De acordo com esta discussão no SO gringo, e seguindo a sugestão do @Math, o código abaixo deve solucionar o seu problema:

{$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;
1
  • Está dando erro nesta ultima linha Drive := Chr(Ord('a') + I); 17/06/2015 às 14:21

Você deve fazer log-in para responder a esta pergunta.

Esta não é a resposta que você está procurando? Pesquise outras perguntas com a tag .