1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196
| unit AntMediaConnected;
interface
uses Windows, Messages, SysUtils, Classes, Forms, Dialogs;
const
DBT_DEVICEARRIVAL = $8000; // system detected a new device
DBT_DEVICEREMOVECOMPLETE = $8004; // device is gone
DBT_DEVTYP_VOLUME = $00000002; // device volume class
type
PDevBroadcastHdr = ^DEV_BROADCAST_HDR;
PDevBroadcastHeader = PDevBroadcastHdr;
DEV_BROADCAST_HDR = packed record
dbch_size: DWORD;
dbch_devicetype: DWORD;
dbch_reserved: DWORD;
end;
PDevBroadcastVolume = ^DEV_BROADCAST_VOLUME;
DEV_BROADCAST_VOLUME = packed record
dbch_size: DWORD;
dbch_devicetype: DWORD;
dbch_reserved: DWORD;
dbcv_unitmask: DWORD;
dbcv_flags: WORD;
end;
type
TTypeSizeOf = (tsoByte, tsoKiloByte, tsoMegaByte, tsoGigabyte, tsoTeraByte);
TMediaConnected = class(TComponent)
private
FDeviceLetter: Char;
FVolName: Array[0..Max_Path]Of Char;
FileSysName : Array[0..Max_Path]Of Char;
FVolSerial : DWord;
FileMaxLen : DWord;
FileFlags : DWord;
FFreeSpace : double;
FSize : double;
FBusySpace: double;
FWindowHandle: HWND;
FOnUSBArrival: TNotifyEvent;
FOnUSBRemove: TNotifyEvent;
procedure WndProc(var Msg: TMessage);
// function USBRegister: Boolean;
protected
procedure WMDeviceChange(var Msg: TMessage); dynamic;
public
function GetSerialVolume: Cardinal;
function GetNameVolume: String;
function GetFreeSpace(TypeSizeReturn: TTypeSizeOf = tsoByte): Double;
function GetBusySpace(TypeSizeReturn: TTypeSizeOf = tsoByte): Double;
function GetSize(TypeSizeReturn: TTypeSizeOf = tsoByte): Double;
function GetLetterMedia: Char;
constructor Create(AOwner: TComponent); override;
destructor Destroy; override;
published
property OnUSBArrival: TNotifyEvent read FOnUSBArrival write FOnUSBArrival;
property OnUSBRemove: TNotifyEvent read FOnUSBRemove write FOnUSBRemove;
end;
procedure Register;
implementation
procedure Register;
begin
RegisterComponents('AntFIRST',[TMediaConnected]);
end;
{==============================================================================}
{ TMediaConnected }
{==============================================================================}
function FormatSize(Value: Double; TypeSizeReturn: TTypeSizeOf): Double;
begin
case TypeSizeReturn of
tsoByte: Result := Value / 1024;
tsoKiloByte: Result := Value / 1048576;
tsoMegaByte: Result := Value / 1073741824;
else
Result := Value;
end;
end;
//------------------------------------------------------------------------------
constructor TMediaConnected.Create(AOwner: TComponent);
begin
inherited Create(AOwner);
FWindowHandle := AllocateHWnd(WndProc);
//USBRegister;
end;
//------------------------------------------------------------------------------
destructor TMediaConnected.Destroy;
begin
DeallocateHWnd(FWindowHandle);
inherited Destroy;
end;
//------------------------------------------------------------------------------
procedure TMediaConnected.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 TMediaConnected.WMDeviceChange(var Msg: TMessage);
var
PHeader : PDevBroadcastHeader;
PData : PDevBroadcastVolume;
Index : Integer;
begin
inherited;
PHeader:=PDevBroadcastHeader(Msg.lParam);
FDeviceLetter:=#0;
case Msg.WParam of
DBT_DEVICEARRIVAL :
begin
if (PHeader^.dbch_devicetype = DBT_DEVTYP_VOLUME) then
begin
PData:=PDevBroadcastVolume(Msg.lParam);
for Index:=0 to 25 do
if ((PData^.dbcv_unitmask shr Index) = 1) then
begin
FDeviceLetter:=Chr(65+Index);
break;
end;
if GetVolumeInformation(PChar(FDeviceLetter+':\'),FVolName,Max_Path,@FVolSerial, FileMaxLen, FileFlags,FileSysName,Max_Path) then
begin
FFreeSpace:=DiskFree(Ord(FDeviceLetter)-64);
FSize:=DiskSize(Ord(FDeviceLetter)-64);
FBusySpace:= FSize - FFreeSpace;
end;
end;
if Assigned(FOnUSBArrival) then
FOnUSBArrival(Self);
end;
DBT_DEVICEREMOVECOMPLETE:
begin
if (PHeader^.dbch_devicetype = DBT_DEVTYP_VOLUME) then
begin
PData:=PDevBroadcastVolume(Msg.lParam);
for Index:=0 to 25 do
if ((PData^.dbcv_unitmask shr Index) = 1) then
begin
FDeviceLetter:=Chr(65+Index);
break;
end;
end;
if Assigned(FOnUSBRemove) then
FOnUSBRemove(Self);
end;
end;
end;
//------------------------------------------------------------------------------
function TMediaConnected.GetSerialVolume:cardinal;
begin
Result:=FVolSerial;
end;
//------------------------------------------------------------------------------
function TMediaConnected.GetNameVolume:string;
begin
Result:=FVolName;
end;
//------------------------------------------------------------------------------
function TMediaConnected.GetLetterMedia:char;
begin
Result:=FDeviceLetter;
end;
//------------------------------------------------------------------------------
function TMediaConnected.GetFreeSpace(TypeSizeReturn: TTypeSizeOf = tsoByte): Double;
begin
result := FormatSize(FFreeSpace, TypeSizeReturn);
end;
//------------------------------------------------------------------------------
function TMediaConnected.GetBusySpace(TypeSizeReturn: TTypeSizeOf = tsoByte): Double;
begin
result := FormatSize(FBusySpace, TypeSizeReturn);
end;
//------------------------------------------------------------------------------
function TMediaConnected.GetSize(TypeSizeReturn: TTypeSizeOf = tsoByte): Double;
begin
result := FormatSize(FSize, TypeSizeReturn);
end;
end. |
Partager