Forum > General

CreateFileA and high Thandle value

(1/2) > >>

metallaro1980:
i'm writing an app for cloning usb hard disk drives and it does also low level format on usb device (pen drive)
but when i try to open device (hard disk to clone) i obtain as thandle a high value


--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---    ADrive := '\\.\' + edit2.text;    //CreateFile(Path, GENERIC_READ, 0, IntPtr.Zero, OPEN_EXISTING, 0, IntPtr.Zero);    hVol := CreateFile(pchar(ADrive), GENERIC_READ, FILE_SHARE_READ or FILE_SHARE_WRITE, nil, OPEN_EXISTING, 0,0);    showmessage(inttostr(GetLastError));
what is wrong with this code?
THandle returned = 446744073709551615

exe with admin rights

best regards
andrea verdi

marcov:
So you are probably in 64-bit ?

metallaro1980:
yes 64bit

440bx:

--- Quote from: metallaro1980 on December 07, 2022, 01:28:10 pm ---what is wrong with this code?

--- End quote ---
Hard to tell from what you posted but, most likely the drive name used in the call to CreateFile.

See the link https://learn.microsoft.com/en-us/windows/win32/api/fileapi/nf-fileapi-createfilea Physical Disks and Volumes paragraph, particularly the part that starts with
--- Quote ---When opening a physical drive x:, the lpFileName string should be the following form: "\\.\PhysicalDriveX". Hard disk numbers start at zero. The following table shows some examples of physical drive strings.
--- End quote ---
Look at the examples and ensure the name you are passing to CreateFile is as expected.  There is also an example in C about DeviceIoControl that you may find useful.

HTH.

metallaro1980:
yes
i used \\.\PhysicalDriveX
but on linux i can read the hard disk
with

--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---procedure TForm1.Button1Click(Sender: TObject);var h:thandle;var hs : thandlestream;var b512 : Array512;var b5120: Array5120;var b:array of byte;var i : integer;var rimanenti:int64;var b2: Buffer10240; begin  try    try     button1.enabled := false;    bitbtn1.enabled := false;     h := fileopen(edit1.text, fmOpenWrite or fmShareExclusive);    hs := thandlestream.create(h);     for i := 0 to length(b512) - 1 do    begin      b512[i] := 0;    end;      for i := 0 to length(b5120) - 1 do    begin      b5120[i] := 0;    end;     for i := 0 to length(b2) - 1 do    begin      b2[i] := 0;    end;     sizedisk := hs.size;    rimanenti := hs.Size - 512;    perc := 0;    perc += 512;     hs.Write(b512[0], 512);     if (checkbox1.checked = false) then    begin      while (rimanenti >= 5120) do      begin        hs.write(b5120[0], 5120);        rimanenti := rimanenti - 5120;        perc += 5120;        application.ProcessMessages;      end;       if (rimanenti > 0) then      begin        setlength(b, rimanenti);        perc += rimanenti;        for i := 0 to length(b) - 1 do        begin          b[i] := 0;        end;        hs.write(b[0], sizeof(b));         application.ProcessMessages;      end;    end    else    begin      while (rimanenti >= 5120*2) do      begin        hs.write(b2[0], 5120*2);        rimanenti := rimanenti - 5120*2;        perc += 5120*2;        application.ProcessMessages;      end;       if (rimanenti > 0) then      begin        setlength(b, rimanenti);        perc += rimanenti;        for i := 0 to length(b) - 1 do        begin          b[i] := 0;        end;        hs.write(b[0], sizeof(b));         application.ProcessMessages;      end;        end;       except      on e: exception do      begin       end;     end;   finally    fileclose(h);    if (Assigned(hs)) then  hs.Free;    button1.enabled := true;    bitbtn1.enabled := true;  end;  end;      

Navigation

[0] Message Index

[#] Next page

Go to full version