Einzelnen Beitrag anzeigen

hathor
(Gast)

n/a Beiträge
 
#23

AW: Erkennen, ob USB-Stick oder SD-Card eingesteckt oder herausgenommen?

  Alt 4. Jul 2015, 21:17
Vielleicht ist GUID_IO_VOLUME_MOUNT die Lösung:
(In LAZARUS nicht getestet, bei XE7 funktioniert es.)

Delphi-Quellcode:
unit Unit1;

interface

uses
  Winapi.Windows, Winapi.Messages, System.SysUtils, System.Classes,
  Vcl.Graphics, Vcl.Controls, Vcl.Forms, Vcl.StdCtrls;

type
  TForm1 = class(TForm)
    Memo1: TMemo;
    procedure FormCreate(Sender: TObject);
    procedure FormDestroy(Sender: TObject);
  private
    FWindow: HWND;
    FDevNotificationHandle: HDEVNOTIFY;
    procedure WndMethod(var Message: TMessage);
    function HandleDeviceChange(Event: DWORD; Data: Pointer): Boolean;
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

type
  DEV_BROADCAST_HANDLE = record
    dbch_size: DWORD;
    dbch_devicetype: DWORD;
    dbch_reserved: DWORD;
    dbch_handle: THandle;
    dbch_hdevnotify: HDEVNOTIFY;
    dbch_eventguid: TGUID;
    dbch_nameoffset: LONG;
  end;

const
  DBT_DEVTYP_DEVICEINTERFACE = $0005;
  GUID_IO_VOLUME_MOUNT: TGUID = '{B5804878-1A96-11D2-8FFD-00A0C9A06D32}';

procedure TForm1.FormCreate(Sender: TObject);
var
  dbh: DEV_BROADCAST_HANDLE;
begin
  FWindow := AllocateHWnd(WndMethod);
  dbh := Default(DEV_BROADCAST_HANDLE);
  dbh.dbch_size := SizeOf(dbh);
  dbh.dbch_devicetype := DBT_DEVTYP_DEVICEINTERFACE;
  dbh.dbch_eventguid := GUID_IO_VOLUME_MOUNT;
  FDevNotificationHandle := RegisterDeviceNotification(FWindow, @dbh,
    DEVICE_NOTIFY_WINDOW_HANDLE);
  Win32Check(FDevNotificationHandle <> nil);
end;

procedure TForm1.FormDestroy(Sender: TObject);
begin
  if FDevNotificationHandle <> nil then
    Win32Check(UnregisterDeviceNotification(FDevNotificationHandle));
  DeallocateHWnd(FWindow);
end;

procedure TForm1.WndMethod(var Message: TMessage);
begin
  case Message.Msg of
    WM_DEVICECHANGE:
      Message.Result := ord(HandleDeviceChange(Message.WParam,
        Pointer(Message.LParam)));
  else
    Message.Result := DefWindowProc(FWindow, Message.Msg, Message.WParam,
      Message.LParam);
  end;
end;

function TForm1.HandleDeviceChange(Event: DWORD; Data: Pointer): Boolean;
begin
  Winapi.Windows.Beep(2000,100); Winapi.Windows.Beep(1000,100);
  Memo1.Lines.Add(Format('%4x', [Event]));
  Result := True;
  case Event of
  $07: Memo1.Lines.Add('DEVICECHANGE');
  $8000: Memo1.Lines.Add('DEVICEARRIVAL');
  $8004: Memo1.Lines.Add('DEVICEREMOVECOMPLETE');
(* oder
  7:      Memo1.Lines.Add('DEVICECHANGE');
  32768 : Memo1.Lines.Add('DEVICEARRIVAL');
  32769 : Memo1.Lines.Add('');
  32770 : Memo1.Lines.Add('');
  32771 : Memo1.Lines.Add('');
  32772 : Memo1.Lines.Add('DEVICEREMOVECOMPLETE');
  32773 : Memo1.Lines.Add('');
*)

  end;
end;

end.
(*
Bedeutungen der angezeigten Codes:
WM_DEVICECHANGE 0x07
DBT_CONFIGCHANGECANCELED 0x19
DBT_CONFIGCHANGED 0x18
DBT_CUSTOMEVENT 0x8006
DBT_DEVICEARRIVAL 0x8000 <--------------------
DBT_DEVICEQUERYREMOVE 0x8001
DBT_DEVICEQUERYREMOVEFAILED 0x8002
DBT_DEVICEREMOVECOMPLETE 0x8004 <-------------
DBT_DEVICEREMOVEPENDING 0x8003
DBT_DEVICETYPESPECIFIC 0x8005
DBT_QUERYCHANGECONFIG 0x17
DBT_USERDEFINED 0xffff
*)

Geändert von hathor ( 4. Jul 2015 um 22:50 Uhr)
  Mit Zitat antworten Zitat