Добрый день.
По сабжу, у меня есть много USB устройств, а контролировать отключение и включение нужно только одного устройства. Флешки и т.п. вынимать можно, но вот со сканером отпечатков пальцев играться нельзя, вот хотелось бы отлавливать таких нарушителей.
Само событие по которому происходит подключение/отключение я нашел, но вот как быть дальше?
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;
type
PDevBroadcastHdr = ^DEV_BROADCAST_HDR;
DEV_BROADCAST_HDR = packed record
dbch_size: DWORD;
dbch_devicetype: DWORD;
dbch_reserved: DWORD;
end;
PDevBroadcastDeviceInterface = ^DEV_BROADCAST_DEVICEINTERFACE;
DEV_BROADCAST_DEVICEINTERFACE = record
dbcc_size: DWORD;
dbcc_devicetype: DWORD;
dbcc_reserved: DWORD;
dbcc_classguid: TGUID;
dbcc_name: short;
end;
TForm1 = class(TForm)
lst1: TListBox;
procedure FormCreate(Sender: TObject);
procedure FormDestroy(Sender: TObject);
private
FWindowHandle: HWND;
procedure WndProc(var Msg: TMessage);
function USBRegister: Boolean;
procedure WMDeviceChange(var Msg: TMessage); dynamic;
public
end;
var
Form1: TForm1;
const
GUID_DEVINTERFACE_USB_DEVICE: TGUID = '';
DBT_DEVICEARRIVAL = $8000;
DBT_DEVICEQUERYREMOVE = $8001;
DBT_DEVICEQUERYREMOVEFAILED = $8002;
DBT_DEVICEREMOVEPENDING = $8003;
DBT_DEVICEREMOVECOMPLETE = $8004;
DBT_DEVTYP_DEVICEINTERFACE = $00000005;
implementation
procedure TForm1.WndProc(var Msg: TMessage);
begin
if (Msg.Msg = WM_DEVICECHANGE) then
begin
try
WMDeviceChange(Msg);
except
Application.HandleException(Self);
end;
end
else
Msg.Result := DefWindowProc(FWindowHandle, Msg.Msg, Msg.wParam, Msg.lParam);
end;
procedure TForm1.WMDeviceChange(var Msg: TMessage);
var
devType: Integer;
Datos: PDevBroadcastHdr;
begin
if (Msg.wParam = DBT_DEVICEARRIVAL) or (Msg.wParam = DBT_DEVICEREMOVECOMPLETE) or (Msg.wParam = DBT_DEVICEQUERYREMOVE) then
begin
Datos := PDevBroadcastHdr(Msg.lParam);
devType := Datos^.dbch_devicetype;
if devType = DBT_DEVTYP_DEVICEINTERFACE then
begin
if Msg.wParam = DBT_DEVICEARRIVAL then
begin
end
else
if Msg.wParam = DBT_DEVICEQUERYREMOVE then
begin
end;
else
if Msg.wParam = DBT_DEVICEREMOVECOMPLETE then
begin
end;
end;
end;
end;
procedure TForm1.FormCreate(Sender: TObject);
begin
inherited;
FWindowHandle := AllocateHWnd(WndProc);
USBRegister;
end;
function TForm1.USBRegister: Boolean;
var
dbi: DEV_BROADCAST_DEVICEINTERFACE;
Size: Integer;
r: Pointer;
begin
Result := False;
Size := SizeOf(DEV_BROADCAST_DEVICEINTERFACE);
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 := 0;
r := RegisterDeviceNotification(FWindowHandle, @dbi, DEVICE_NOTIFY_WINDOW_HANDLE);
if Assigned(r) then
Result := True;
end;
procedure TForm1.FormDestroy(Sender: TObject);
begin
DeallocateHWnd(FWindowHandle);
end;
end.
Смотрю в сторону SetupAPI, только как прикрутить не знаю.