unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, LinkLabel;
type
TForm1 =
class(TForm)
Label1: TLabel;
private
{ Private-Deklarationen }
procedure WMDeviceChange(
var Msg: TMessage);
message WM_DEVICECHANGE;
public
{ Public-Deklarationen }
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
const
DBT_DEVICEARRIVAL = $8000;
// A device or piece of media has been inserted and is now available.
DBT_DEVICEQUERYREMOVE = $8001;
// Permission is requested to remove a device or piece of media. Any application can deny this request and cancel the removal.
DBT_DEVICEQUERYREMOVEFAILED = $8002;
// A request to remove a device or piece of media has been canceled.
DBT_DEVICEREMOVECOMPLETE = $8004;
// A device or piece of media has been removed.
DBT_DEVTYP_VOLUME = $00000002;
// Logical volume. This structure is a DEV_BROADCAST_VOLUME structure.
DBTF_MEDIA = $0001;
// Change affects media in drive. If not set, change affects physical device or drive.
DBTF_NET = $0002;
// Indicated logical volume is a network volume.
type
PDevBroadcastHdr = ^TDevBroadcastHdr;
TDevBroadcastHdr =
packed record
dbcd_size: DWORD;
dbcd_devicetype: DWORD;
dbcd_reserved: DWORD;
end;
PDevBroadcastVolume = ^TDevBroadcastVolume;
TDevBroadcastVolume =
packed record
dbcv_size: DWORD;
dbcv_devicetype: DWORD;
dbcv_reserved: DWORD;
dbcv_unitmask: DWORD;
dbcv_flags: Word;
end;
procedure TForm1.WMDeviceChange(
var Msg: TMessage);
var
Drive:
String;
function GetDrive(pDBVol: PDevBroadcastVolume):
string;
var
i: Byte;
Maske: DWORD;
DevBroadcastVolume: TDevBroadcastVolume;
begin
DevBroadcastVolume := pDBVol^;
// for Debug
if (pDBVol^.dbcv_flags
and DBTF_MEDIA) = DBTF_MEDIA
then // <--<<
begin
Maske := pDBVol^.dbcv_unitmask;
for i := 0
to 25
do
begin
if (Maske
and 1) = 1
then Result := Char(i + Ord('
A')) + '
: ';
Maske := Maske
shr 1;
end;
end;
end;
begin
case Msg.wParam
of
DBT_DEVICEARRIVAL:
if PDevBroadcastHdr(Msg.lParam)^.dbcd_devicetype = DBT_DEVTYP_VOLUME
then
Label1.Caption := '
Datenträger in Laufwerk '+ GetDrive(PDevBroadcastVolume(Msg.lParam))+'
eingelegt';
DBT_DEVICEREMOVECOMPLETE:
if PDevBroadcastHdr(Msg.lParam)^.dbcd_devicetype = DBT_DEVTYP_VOLUME
then
begin
Drive := GetDrive(PDevBroadcastVolume(Msg.lParam));
Label1.Caption := '
Datenträger aus Laufwerk '+Drive+'
entfernt';
end;
end;
end;
end.