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

DVD

开发平台:

Delphi

  1. {-----------------------------------------------------------------------------
  2.  Unit Name: Devices
  3.  Author:    Paul Fisher / Andrew Semack
  4.  Purpose:   Container Class for TDevice
  5.  History:
  6. -----------------------------------------------------------------------------}
  7. unit Devices;
  8. interface
  9. uses
  10.   Windows, Classes, Device, DeviceNotifier, DeviceTypes, DeviceHelper,
  11.   SCSITypes, SysUtils;
  12. type
  13.   TDevices = class
  14.   private
  15.     FOnDeviceInstalledEvent: TNotifyEvent;
  16.     FOnDeviceRemovedEvent: TNotifyEvent;
  17.     FDeviceList: TList;
  18.     FDeviceNotifier: TDeviceNotifier;
  19.     function GetDeviceCount: integer;
  20.     function GetDevice(Index: integer): TDevice;
  21.   protected
  22.     procedure ClearDeviceList;
  23.     procedure FormatDeviceList;
  24.     procedure DeviceInstalled(Sender: TObject);
  25.     procedure DeviceRemoved(Sender: TObject);
  26.   public
  27.     procedure Refresh;
  28.     property Count: integer read GetDeviceCount;
  29.     property Items[Index: integer]: TDevice read GetDevice;
  30.     property OnDeviceInstalled: TNotifyEvent read FOnDeviceInstalledEvent write
  31.       FOnDeviceInstalledEvent;
  32.     property OnDeviceRemoved: TNotifyEvent read FOnDeviceRemovedEvent write
  33.       FOnDeviceRemovedEvent;
  34.     constructor Create;
  35.     destructor Destroy; override;
  36.   end;
  37. implementation
  38. { TDevices }
  39. constructor TDevices.Create;
  40. begin
  41.   FDeviceNotifier := TDeviceNotifier.Create(nil);
  42.   with FDeviceNotifier do
  43.   begin
  44.     OnUSBArrival := DeviceInstalled;
  45.     OnUSBRemove := DeviceRemoved;
  46.   end;
  47.   FDeviceList := TList.Create;
  48.   Refresh;
  49. end;
  50. function TDevices.GetDevice(Index: integer): TDevice;
  51. begin
  52.   Result := TDevice(FDeviceList[Index])
  53. end;
  54. procedure TDevices.Refresh;
  55. begin
  56.   ClearDeviceList;
  57.   FormatDeviceList;
  58. end;
  59. function TDevices.GetDeviceCount: integer;
  60. begin
  61.   Result := FDeviceList.Count;
  62. end;
  63. procedure TDevices.ClearDeviceList;
  64. var
  65.   i: integer;
  66. begin
  67.   for i := FDeviceList.Count - 1 downto 0 do
  68.     GetDevice(i).Free;
  69.   FDeviceList.Clear;
  70. end;
  71. destructor TDevices.Destroy;
  72. begin
  73.   ClearDeviceList;
  74.   FDeviceNotifier.Free;
  75.   FDeviceList.Free;
  76.   inherited;
  77. end;
  78. procedure TDevices.FormatDeviceList;
  79. var
  80.   Info: PCDBurnerInfo;
  81.   index: integer;
  82.   SPTICDs: TSPTIWriters;
  83. begin
  84.   GetSPTICdRomDrives(SPTICDs);
  85.   for index := 0 to SPTICDs.CdRomCount - 1 do
  86.   begin
  87.     New(Info);
  88.     Info.VendorSpec := SPTICDs.CdRom[index].VendorSpec;
  89.     Info.Revision := SPTICDs.CdRom[index].Revision;
  90.     Info.VendorID := trim(SPTICDs.CdRom[index].Vendor);
  91.     Info.ProductID := trim(SPTICDs.CdRom[index].ProductId);
  92.     Info.VendorName := Format('%s %s', [Info.VendorID, Info.ProductID]);
  93.     Info.DriveLetter := SPTICDs.CdRom[index].DriveLetter;
  94.     Info.DriveID := GatherDeviceID(SPTICDs.CdRom[index].HaId,
  95.       SPTICDs.CdRom[index].Target, SPTICDs.CdRom[index].Lun,
  96.       SPTICDs.CdRom[index].DriveLetter);
  97.     Info.Lun := SPTICDs.CdRom[index].Lun;
  98.     Info.HaId := SPTICDs.CdRom[index].HaId;
  99.     Info.Target := SPTICDs.CdRom[index].Target;
  100.     Info.DriveIndex := index;
  101.     Info.SptiHandle := SPTICDs.CdRom[index].DriveHandle;
  102.     FDeviceList.Add(TDevice.Create(Info));
  103.   end;
  104. end;
  105. procedure TDevices.DeviceRemoved(Sender: TObject);
  106. begin
  107.   if assigned(FOnDeviceRemovedEvent) then
  108.     FOnDeviceRemovedEvent(Self);
  109. end;
  110. procedure TDevices.DeviceInstalled(Sender: TObject);
  111. begin
  112.   if assigned(FOnDeviceInstalledEvent) then
  113.     FOnDeviceInstalledEvent(Self);
  114. end;
  115. end.