2005. június 8., szerda

Detect if a new drive has been added

Problem/Question/Abstract:

In my software I need to detect if a new drive is attached to the computer through an USB port. The Windows Explorer expands automatically, so I assume there is a Windows Message broadcasted. Can anyone help me how to detect a new drive?

Answer:

{ ... }
type
TForm1 = class(TForm)
{ ... }
private
procedure WMDEVICECHANGE(var Msg: TMessage); message WM_DEVICECHANGE;
{ ... }
end;

procedure TFormBookBrowse.WMDEVICECHANGE(var Msg: TMessage);
const
DBT_DEVICEARRIVAL = $8000;
DBT_DEVICEREMOVECOMPLETE = $8004;
begin
inherited;
case Msg.WParam of
DBT_DEVICEARRIVAL:
begin
{ ... }
end;
DBT_DEVICEREMOVECOMPLETE:
begin
{ ... }
end;
end;
end;


Nincsenek megjegyzések:

Megjegyzés küldése