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.