ps: флешки и винты блочить умею, вопрос лишь с media device
блочу такой штукой:
function Eject_DevHotPlugMedia(LetVol:Char;Rep,TempRep:Integer;MessageEject,MediaRemove: Boolean):Boolean;
var
HVolume : THandle;
lpBytesReturned : DWORD;
PreventRemoval : TPREVENT_MEDIA_REMOVAL;
FormatLettreLecteur : String;
I : Integer;
begin
Result:=False;
hVolume:=INVALID_HANDLE_VALUE;
try
hVolume:=CreateFile(PChar('\\.\'+LetVol+':'),FILE_SHARE_READ or FILE_SHARE_WRITE,0,nil,OPEN_EXISTING,0,0);
if hVolume=INVALID_HANDLE_VALUE then
hVolume:=CreateFile(PChar('\\.\'+LetVol+':'),GENERIC_READ,0,nil,OPEN_EXISTING,0,0);
if hVolume=INVALID_HANDLE_VALUE then
hVolume:=CreateFile(PChar('\\.\'+LetVol+':'),0,FILE_SHARE_READ or FILE_SHARE_WRITE,nil,OPEN_EXISTING,0,0);
if hVolume=INVALID_HANDLE_VALUE then
exit;
for I := 0 to Rep - 1 do
begin
PreventRemoval.PreventMediaRemoval:=False;
if DeviceIoControl(hVolume,FSCTL_LOCK_VOLUME, nil,0,nil,0,lpBytesReturned,nil) then
if DeviceIoControl(hVolume,FSCTL_DISMOUNT_VOLUME, nil,0,nil,0,lpBytesReturned,nil) then
if DeviceIoControl(hVolume,IOCTL_STORAGE_MEDIA_REMOVAL,@PreventRemoval,SizeOf(TPREV ENT_MEDIA_REMOVAL),nil,0,lpBytesReturned,nil) then
if DeviceIoControl(hVolume,IOCTL_STORAGE_EJECT_MEDIA,nil,0,nil,0,lpBytesReturned,ni l)then
if DeviceIoControl(hVolume,FSCTL_UNLOCK_VOLUME,nil,0,nil,0,lpBytesReturned,nil) then
begin
FormatLettreLecteur:=Format('%s:\',[Uppercase(LetVol)]);
ShChangeNotify(SHCNE_MEDIAREMOVED,SHCNF_PATH,PChar(FormatLettreLecteur),Nil);
Result:=True;
if (MessageEject) and (not MediaRemove) then
MessageDlg('Le matériel peut-être retiré en toute sécurité !',mtInformation,[mbOK],0);
if (MessageEject) and (MediaRemove) then
MessageDlg('Le lecteur est ouvert !',mtInformation,[mbOK],0);
Break;
end;
if not Result then
Sleep(TempRep);
end;
finally
CloseHandle(hVolume);
end;
if (not Result) and (MessageEject) and (MediaRemove) then
MessageDlg('Le lecteur est en cours d''utilisation par un autre processus !',mtWarning,[mbOK],0);
if (not Result) and (not MediaRemove) then
Eject_DevHotPlug(LetVol,Rep,TempRep,MessageEject,False);
end;