Saturday, June 16, 2012

[Delphi] Auto-detecting USB Drives

Gunakan unit pascal (Delphi) berikut ke project-mu

unit Unit1;

{ Snippet for detecting USB drive when connected and removed }

interface

uses
  Windows, Messages {, and other unit...};

// Device constants
const
  DBT_DEVICEARRIVAL          =  $00008000;
  DBT_DEVICEREMOVECOMPLETE   =  $00008004;
  DBT_DEVTYP_VOLUME          =  $00000002;

// Device structs
type
  _DEV_BROADCAST_HDR         =  packed record
    dbch_size:              DWORD;
    dbch_devicetype:        DWORD;
    dbch_reserved:          DWORD;
  end;
  DEV_BROADCAST_HDR          =  _DEV_BROADCAST_HDR;
  TDevBroadcastHeader        =  DEV_BROADCAST_HDR;
  PDevBroadcastHeader        =  ^TDevBroadcastHeader;

type
  _DEV_BROADCAST_VOLUME      =  packed record
    dbch_size:              DWORD;
    dbch_devicetype:        DWORD;
    dbch_reserved:          DWORD;
    dbcv_unitmask:          DWORD;
    dbcv_flags:             WORD;
  end;
  DEV_BROADCAST_VOLUME       =  _DEV_BROADCAST_VOLUME;
  TDevBroadcastVolume        =  DEV_BROADCAST_VOLUME;
  PDevBroadcastVolume        =  ^TDevBroadcastVolume;

type
  TMainForm = class(TForm)
    Label1: TLabel;

  { Other declarati... }

  private
    // Private declaration
    procedure WMDEVICECHANGE(var Msg: TMessage); message WM_DEVICECHANGE;
  end;

  { Other "tek-tek bengek" etc... :)) }

var
  MainForm: TMainForm;

implementation

procedure MainForm.WMDEVICECHANGE(var Msg: TMessage);
var
  lpdbhHeader:  PDevBroadcastHeader;
  lpdbvData:    PDevBroadcastVolume;
  dwIndex:      Integer;
  lpszDrive:    String;
begin

  // Perform inherited
  inherited;

  // Get the device notification header
  lpdbhHeader:= PDevBroadcastHeader(Msg.LParam);

  // Handle the message
  lpszDrive:= 'Removable drive ';
 
  case Msg.WParam of
    DBT_DEVICEARRIVAL       :    //a USB drive was connected
    begin
      if (lpdbhHeader^.dbch_devicetype = DBT_DEVTYP_VOLUME) then
      begin
        lpdbvData:= PDevBroadcastVolume(Msg.LParam);
        for dwIndex := 0 to 25 do
        begin
          if ((lpdbvData^.dbcv_unitmask shr dwIndex) = 1) then
          begin
            lpszDrive:= lpszDrive + Chr(65 + dwIndex) + ':';
            { Or place your action here… }
            Break;
          end;
        end;
          Label1.Caption:= lpszDrive + ' connected';
      end;
    end;
    DBT_DEVICEREMOVECOMPLETE:    //a USB drive was removed
    begin
      if (lpdbhHeader^.dbch_devicetype = DBT_DEVTYP_VOLUME) then
      begin
        lpdbvData:= PDevBroadcastVolume(Msg.LParam);
        for dwIndex:= 0 to 25 do
        begin
          if ((lpdbvData^.dbcv_unitmask shr dwIndex) = 1) then
          begin
            lpszDrive:= lpszDrive + Chr(65 + dwIndex) + ':';
            { Or place your action here… }
            Break;
          end;
        end;
          Label1.Caption:= lpszDrive + ' removed';
      end;
    end;
  end;
end;

{ And other and other }

// source : Google, MSDN, delphitricks.com
// source code : http://www.4shared.com/zip/3Pq62q0N/AutoUSBDetector_SC.html

end.

4 comments:

denidoank

setahun lebih saya tidak nge- post di blog ini.. saya mau break dulu ngoding dan semacamnya.. terima kasih untuk yang (mungkin) telah meng...