Recent

Author Topic: How to perform an action on an actual drive as opposed to its drive lettering  (Read 4723 times)

Gizmo

  • Hero Member
  • *****
  • Posts: 831
Connected to this thread (http://www.lazarus.freepascal.org/index.php/topic,12230.0.html) but slightly different, thus the new question.

I have a Button and a ListBox on my form. When the button is pressed the ListBox is populated with all logical drives on the computer. I have an OnClick event for the ListBox for whatever logical drive the user clicks with his mouse. I then want the program to hash that chosen drive using the SHA1Hash and SHA1Print functions.

Initially, I realised the generated hash was that of the string representation of the drive letter, i.e the hash of chars "C:\" (because I passed it ListBox1.Items[index]).

So I then added a TFileSearcher element to the mix as I've used that successfully for recursively finding files and then hashing them. As far as I can tell though, in this newer implementation that I'm trying, it seems to find all the files on the chosen drive and shows me the final hash value of the final file it finds.

So, how can I get my program to hash the actual logical drive (i.e. the whole partition) that is represented by the content of ListBox1.Items[index], i.e. Drive C:\, D:\ or whatever?

My code so far (which gives me the final hash of the final file, not the volume):

Code: [Select]
procedure TForm1.ListBox1Click(Sender: TObject);
var
  index : integer;
  FS : TFileSearcher;
  DriveToHash, DriveHashValue : string;

begin
  index := ListBox1.ItemIndex;

  if MessageDlg('Hash selected drive?', 'Are you sure you wish to hash "'+ ListBox1.Items[index] + '"',
  mtConfirmation, [mbYes, mbNo],0) = mrYes
  then
    begin
    FS := TFileSearcher.Create;
    DriveToHash := (ListBox1.Items[index]);
    FS.Search(DriveToHash, '*.*', True);
    DriveHashValue := SHA1Print(SHA1File(DriveToHash));
    FS.Free;
    ShowMessage(DriveHashValue);
    end;
end;    

Gizmo

  • Hero Member
  • *****
  • Posts: 831
Thinking about it more carefully, I think I need to find and use a function that detects the logical start and end sector of a chosen partition (say Drive C:\) and then use a read and seek combination to read each secotr between the two because of course there's more to a volume than the normal live files.

José Mejuto

  • Full Member
  • ***
  • Posts: 136
Thinking about it more carefully, I think I need to find and use a function that detects the logical start and end sector of a chosen partition (say Drive C:\) and then use a read and seek combination to read each secotr between the two because of course there's more to a volume than the normal live files.

Hello,

To do so you need administrator rights, you can not use the current "in use" partition (data not yet completly updated, so hash changes after a few seconds) and you need to use special CreateFile modes to access volumes.

 

TinyPortal © 2005-2018