type
TForm1 =
class(TForm)
procedure FormCreate(Sender: TObject);
private
procedure WMDeviceChange(
var Msg: TMessage);
message WM_DEVICECHANGE;
{ Private-Deklarationen }
public
{ Public-Deklarationen }
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
const
DBT_DEVICEARRIVAL = $8000;
DBT_DEVICEREMOVECOMPLETE = $8004;
DBT_DEVTYP_VOLUME = $00000002;
type
PDEV_BROADCAST_VOLUME = ^DEV_BROADCAST_VOLUME;
DEV_BROADCAST_VOLUME =
record
dbcv_size: DWORD;
dbcv_devicetype: DWORD;
dbcv_reserved: DWORD;
dbcv_unitmask: DWORD;
dbcv_flags: DWORD;
end;
PDevBroadcastHdr = ^DEV_BROADCAST_HDR;
DEV_BROADCAST_HDR =
packed record
dbch_size: DWORD;
dbch_devicetype: DWORD;
dbch_reserved: DWORD;
end;
function BitMaskToChar(BitMask: DWORD): char;
var
i: integer;
c: char;
begin
for i := 0
to 25
do
begin
if (BitMask
and 1) = 1
then
Result := Char(i + Ord('
A'));
BitMask := BitMask
shr 1;
end;
end;
procedure TForm1.WMDeviceChange(
var Msg: TMessage);
begin
if (Msg.wParam = DBT_DEVICEARRIVAL)
or (Msg.wParam = DBT_DEVICEREMOVECOMPLETE)
then
if PDevBroadcastHdr(Msg.LParam).dbch_devicetype = DBT_DEVTYP_VOLUME
then
begin
if Msg.wParam = DBT_DEVICEARRIVAL
then
Messagebox(0, '
Neues Ding...', pchar(BitMaskToChar(PDEV_BROADCAST_VOLUME(Msg.LParam)^.dbcv_unitmask)+'
'), 64);
if Msg.wParam = DBT_DEVICEREMOVECOMPLETE
then
Messagebox(0, '
Ding wech...', pchar(BitMaskToChar(PDEV_BROADCAST_VOLUME(Msg.LParam)^.dbcv_unitmask)+'
'), 64);
end;
end;
procedure TForm1.FormCreate(Sender: TObject);
var
dbv: DEV_BROADCAST_VOLUME;
begin
ZeroMemory(@dbv, SizeOf(DEV_BROADCAST_VOLUME));
dbv.dbcv_size := SizeOf(DEV_BROADCAST_VOLUME);
dbv.dbcv_devicetype := DBT_DEVTYP_VOLUME;
dbv.dbcv_reserved := 0;
dbv.dbcv_unitmask := $FFFFFFFF;
dbv.dbcv_flags := 0;
RegisterDeviceNotification(
Handle, @dbv, DEVICE_NOTIFY_WINDOW_HANDLE);
end;