DeviceNotifier.pas
上传用户:wanyu_2000
上传日期:2021-02-21
资源大小:527k
文件大小:4k
源码类别:

DVD

开发平台:

Delphi

  1. {-----------------------------------------------------------------------------
  2.  Unit Name: DeviceNotifier
  3.  Author:    Paul Fisher / Andrew Semack
  4.  Purpose:   Class for hot plug cd / dvd (usb etc)
  5.  History:
  6. -----------------------------------------------------------------------------}
  7. {$WARN SYMBOL_DEPRECATED OFF}
  8. unit DeviceNotifier;
  9. interface
  10. uses
  11.   Windows, Messages, SysUtils, Classes, Forms;
  12. type
  13.   PDevBroadcastHdr = ^DEV_BROADCAST_HDR;
  14.   DEV_BROADCAST_HDR = packed record
  15.     dbch_size: DWORD;
  16.     dbch_devicetype: DWORD;
  17.     dbch_reserved: DWORD;
  18.   end;
  19.   PDevBroadcastDeviceInterface = ^DEV_BROADCAST_DEVICEINTERFACE;
  20.   DEV_BROADCAST_DEVICEINTERFACE = record
  21.     dbcc_size: DWORD;
  22.     dbcc_devicetype: DWORD;
  23.     dbcc_reserved: DWORD;
  24.     dbcc_classguid: TGUID;
  25.     dbcc_name: short;
  26.   end;
  27. const
  28.   GUID_DEVINTERFACE_USB_DEVICE: TGUID =
  29.   '{A5DCBF10-6530-11D2-901F-00C04FB951ED}';
  30.   DBT_DEVICEARRIVAL = $8000; // system detected a new device
  31.   DBT_DEVICEREMOVECOMPLETE = $8004; // device is gone
  32.   DBT_DEVTYP_DEVICEINTERFACE = $00000005; // device interface class
  33. type
  34.   TDeviceNotifier = class(TComponent)
  35.   private
  36.     FWindowHandle: HWND;
  37.     FOnUSBArrival: TNotifyEvent;
  38.     FOnUSBRemove: TNotifyEvent;
  39.     FHandle: pointer;
  40.     procedure WndProc(var Msg: TMessage);
  41.     function USBRegister: Boolean;
  42.     function USBUnRegister: boolean;
  43.   protected
  44.     procedure WMDeviceChange(var Msg: TMessage); dynamic;
  45.   public
  46.     constructor Create(AOwner: TComponent); override;
  47.     destructor Destroy; override;
  48.   published
  49.     property OnUSBArrival: TNotifyEvent read FOnUSBArrival write FOnUSBArrival;
  50.     property OnUSBRemove: TNotifyEvent read FOnUSBRemove write FOnUSBRemove;
  51.   end;
  52. implementation
  53. constructor TDeviceNotifier.Create(AOwner: TComponent);
  54. begin
  55.   inherited Create(AOwner);
  56.   FHandle := nil;
  57.   FWindowHandle := AllocateHWnd(WndProc);
  58.   USBRegister;
  59. end;
  60. destructor TDeviceNotifier.Destroy;
  61. begin
  62.   USBUnRegister;
  63.   DeallocateHWnd(FWindowHandle);
  64.   inherited Destroy;
  65. end;
  66. procedure TDeviceNotifier.WndProc(var Msg: TMessage);
  67. begin
  68.   if (Msg.Msg = WM_DEVICECHANGE) then
  69.   begin
  70.     try
  71.       WMDeviceChange(Msg);
  72.     except
  73.       Application.HandleException(Self);
  74.     end;
  75.   end
  76.   else
  77.     Msg.Result := DefWindowProc(FWindowHandle, Msg.Msg, Msg.wParam, Msg.lParam);
  78. end;
  79. procedure TDeviceNotifier.WMDeviceChange(var Msg: TMessage);
  80. var
  81.   devType: Integer;
  82.   Datos: PDevBroadcastHdr;
  83. begin
  84.   if (Msg.wParam = DBT_DEVICEARRIVAL) or (Msg.wParam = DBT_DEVICEREMOVECOMPLETE)
  85.     then
  86.   begin
  87.     Datos := PDevBroadcastHdr(Msg.lParam);
  88.     devType := Datos^.dbch_devicetype;
  89.     if devType = DBT_DEVTYP_DEVICEINTERFACE then
  90.     begin
  91.       if Msg.wParam = DBT_DEVICEARRIVAL then
  92.       begin
  93.         if Assigned(FOnUSBArrival) then
  94.           FOnUSBArrival(Self);
  95.       end
  96.       else
  97.       begin
  98.         if Assigned(FOnUSBRemove) then
  99.           FOnUSBRemove(Self);
  100.       end;
  101.     end;
  102.   end;
  103. end;
  104. function TDeviceNotifier.USBRegister: Boolean;
  105. var
  106.   dbi: DEV_BROADCAST_DEVICEINTERFACE;
  107.   Size: Integer;
  108. begin
  109.   Result := False;
  110.   Size := SizeOf(DEV_BROADCAST_DEVICEINTERFACE);
  111.   ZeroMemory(@dbi, Size);
  112.   dbi.dbcc_size := Size;
  113.   dbi.dbcc_devicetype := DBT_DEVTYP_DEVICEINTERFACE;
  114.   dbi.dbcc_reserved := 0;
  115.   dbi.dbcc_classguid := GUID_DEVINTERFACE_USB_DEVICE;
  116.   dbi.dbcc_name := 0;
  117.   FHandle := RegisterDeviceNotification(FWindowHandle, @dbi,
  118.     DEVICE_NOTIFY_WINDOW_HANDLE);
  119.   if Assigned(FHandle) then
  120.     Result := True;
  121. end;
  122. function TDeviceNotifier.USBUnRegister: boolean;
  123. begin
  124.   if assigned(FHandle) then
  125.     Result := UnRegisterDeviceNotification(FHandle)
  126.   else
  127.     Result := False;
  128. end;
  129. end.