unit Unit1;
{$mode objfpc}{$H+}
interface
uses
Classes, SysUtils, FileUtil, Forms, Controls, Graphics, Dialogs, StdCtrls,
Windows;
type
{ TForm1 }
TForm1 =
class(TForm)
procedure FormCreate(Sender: TObject);
private
{ private declarations }
public
{ public declarations }
end;
const
GUID_DEVINTERFACE_USB_DEVICE: TGUID = '
{A5DCBF10-6530-11D2-901F-00C04FB951ED}';
DBT_DEVICEARRIVAL = $8000;
// system detected a new device
DBT_DEVICEREMOVECOMPLETE = $8004;
// device is gone
DBT_DEVTYP_DEVICEINTERFACE = $00000005;
// device interface class
DBT_DEVTYP_VOLUME = $00000002;
var
Form1: TForm1;
implementation
uses JwaWinUser, JwaDbt;
{$R *.lfm}
type
TMWndProc = Windows.WNDPROC;
var
OldWndProc: TMWndProc;
function MyWndProc(my_wnd : HWND; Msg : UINT; my_wparam : WPARAM; my_lparam : LPARAM): LRESULT;
stdcall;
var
devType : Integer;
Datos : PDevBroadcastHdr;
begin
Result := 0;
case Msg
of
WM_DEVICECHANGE:
begin
if (my_wparam = DBT_DEVICEARRIVAL)
or (my_wparam = DBT_DEVICEREMOVECOMPLETE)
then
begin
Datos := PDevBroadcastHdr(Pointer(my_lparam));
devType := Datos^.dbch_devicetype;
if devType = DBT_DEVTYP_VOLUME
then
begin
if my_wparam = DBT_DEVICEARRIVAL
then
ShowMessage('
USB-Gerät eingesteckt!')
else
ShowMessage('
USB-Gerät herausgenommen!');
end;
end;
// Return TRUE to grant the request
// Return BROADCAST_QUERY_DENY to deny the request.
// Weiß nicht genau was ich hier zurück geben soll
end;
// WM_DEVICECHANGE
end;
// case
Result := Windows.CallWindowProc(OldWndProc, my_wnd, Msg, my_wparam, my_lparam);
end;
{ TForm1 }
procedure TForm1.FormCreate(Sender: TObject);
var
dbi: DEV_BROADCAST_DEVICEINTERFACE_W;
dbv: DEV_BROADCAST_VOLUME;
Size: Integer;
RDN: HDEVNOTIFY;
arr :
array[0 .. 0]
of word;
begin
arr[0] := 0;
Size := SizeOf(DEV_BROADCAST_DEVICEINTERFACE_W);
ZeroMemory(@dbi, Size);
dbi.dbcc_size := Size;
dbi.dbcc_devicetype := DBT_DEVTYP_DEVICEINTERFACE;
dbi.dbcc_reserved := 0;
dbi.dbcc_classguid := GUID_DEVINTERFACE_USB_DEVICE;
dbi.dbcc_name := arr;
RDN := RegisterDeviceNotificationW(Form1.Handle, @dbi, DEVICE_NOTIFY_WINDOW_HANDLE);
if not Assigned(RDN)
then
ShowMessage('
Error Register Message');
Size := SizeOf(DEV_BROADCAST_VOLUME);
ZeroMemory(@dbv, Size);
dbv.dbcv_size := Size;
dbv.dbcv_devicetype := DBT_DEVTYP_VOLUME;
dbv.dbcv_reserved := 0;
dbv.dbcv_flags := 0;
RDN := RegisterDeviceNotification(Form1.Handle, @dbi, DEVICE_NOTIFY_WINDOW_HANDLE);
if not Assigned(RDN)
then
ShowMessage('
Error Register Message');
OldWndProc := TMWndProc(Windows.GetWindowLong(Self.Handle, GWL_WNDPROC));
Windows.SetWindowLong(Self.Handle, GWL_WNDPROC, LongInt(@MyWndProc));
end;
end.