Sofern du es wirklich voll automatisch haben und nicht ständig pollen willst, dann schau dir mal WM_DEVICECHANGE an..
Delphi-Quellcode:
type
TForm1 =
class(TForm)
public
procedure DeviceChange(
var Msg: TMessage);
message WM_DEVICECHANGE;
end;
// ......
const
DBT_DEVICEARRIVAL = $8000;
DBT_DEVTYP_VOLUME = $02;
type
_DEV_BROADCAST_HDR =
record
dbch_size: DWORD;
dbch_devicetype: DWORD;
dbch_reserved: DWORD;
end;
PDevBroadcastHdr = ^TDevBroadcastHdr;
TDevBroadcastHdr = _DEV_BROADCAST_HDR;
_DEV_BROADCAST_VOLUME =
record
dbcv_size: DWORD;
dbcv_devicetype: DWORD;
dbcv_reserved: DWORD;
dbcv_unitmask: DWORD;
dbcv_flags: WORD;
end;
PDevBroadcastVolume = ^TDevBroadcastVolume;
TDevBroadcastVolume = _DEV_BROADCAST_VOLUME;
function BitScanForward(Value: Integer): Integer;
asm
bsf eax, eax
end;
procedure TForm1.DeviceChange(
var Msg: TMessage);
var
DriveLetter: Char;
begin
case Msg.WParam
of
DBT_DEVICEARRIVAL:
if PDevBroadcastHdr(Msg.LParam).dbch_devicetype = DBT_DEVTYP_VOLUME
then
begin
DriveLetter := Char(65 + BitScanForward(PDevBroadcastVolume(Msg.LParam).dbcv_unitmask));
ShowMessage('
New device plugged in: ' + DriveLetter);
// ShowMessage('DriveType: ' + IntToStr(GetDriveType(pChar(DriveLetter + ':'))));
end;
end;
end;