this slowpoke moves

Block Hard Disk

uses FileCtrl

private
  { Declarations privates }
    function CTL(Device, Methode, NumF, Acces: Integer): Integer;
    
var
  hFile: THandle;
  Form1: TForm1;

const
  FILE_ANY_ACCESS: Integer = $00000000;
  METHOD_BUFFERED: Integer = $00000000;
  FILE_DEVICE_FILE_SYSTEM: Integer = $00000009;

//

function TForm1.CTL(Device, Methode, NumF, Acces: Integer): Integer;
begin
  Result := (Methode) or (Device shl 16) or (Acces shl 14) or (NumF shl 2);
end;

// Sperren
procedure TForm1.Button1Click(Sender: TObject);
var
  bByteReturn: Cardinal;
  FSCTL_LOCK_VOLUME: integer;
begin
try
  FSCTL_LOCK_VOLUME := CTL(FILE_DEVICE_FILE_SYSTEM,
                       METHOD_BUFFERED,
                       6,
                       FILE_ANY_ACCESS);

  hFile := CreateFile(PChar('//./' + copy(ComboBox1.Text,
                      1,
                      pos(':', ComboBox1.Text))),
                      GENERIC_WRITE or GENERIC_READ,
                      FILE_SHARE_READ + FILE_SHARE_WRITE,
                      nil, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0);

  if hFile = INVALID_HANDLE_VALUE then
  begin
    ShowMessage(SysErrorMessage(GetLastError));   
    Exit;
  end;
  if NOT DeviceIoControl(hFile,
                        FSCTL_LOCK_VOLUME,
                        nil,
                        0,
                        nil,
                        0,
                        bByteReturn,
                        nil)
  then
    ShowMessage(SysErrorMessage(GetLastError))   
  else                                           
    Caption := ComboBox1.Text + ' > Locked (' + IntToStr(hFile) + ')';
  ShowMessage('If everything went well, dont forget to UNLOCK the same driver you have' +
              ^M + 'LOCK! otherwise by closing the program the volumes will be refreed when the handles are closed');
  except
    ShowMessage(SysErrorMessage(GetLastError))
end;
end;

// Freigeben
procedure TForm1.Button2Click(Sender: TObject);
begin
  if hFile = INVALID_HANDLE_VALUE then
    Exit;
 if CloseHandle(hFile) then   
    Caption := ComboBox1.Text + ' > Unlocked';
end;

Keine Kommentare:

Kommentar veröffentlichen

Beliebte Posts

Translate