CreateFileA and high Thandle value lazarus / 62-General ---------------------------------------------------------------------- metallaro1980 · 2022-12-07 12:28 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  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i     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 · 2022-12-07 12:47 So you are probably in 64-bit ? ---------------------------------------------------------------------- metallaro1980 · 2022-12-07 12:52 yes 64bit ---------------------------------------------------------------------- 440bx · 2022-12-07 14:53 Quote from: metallaro1980 on December 07, 2022, 01:28:10 pmwhat is wrong with this code? 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 QuoteWhen 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.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 · 2022-12-07 16:12 yes i used \\.\PhysicalDriveX but on linux i can read the hard disk with Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i 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; ---------------------------------------------------------------------- Remy Lebeau · 2022-12-11 02:43 Quote from: metallaro1980 on December 07, 2022, 01:28:10 pmwhen i try to open device (hard disk to clone) i obtain as thandle a high value Why do you think that is a problem?  As long as the value is not $FFFFFFFFFFFFFFFF (aka INVALID_HANDLE_VALUE), which is what CreateFile() returns on failure, then any other value indicates success, not failure. ---------------------------------------------------------------------- metallaro1980 · 2022-12-12 12:20 because the routines that copy all sectors of hard disk  don't start and do nothing createfile works only with name ... for example G: with c++ i can read the values of all sectors lazarus on linux i use: fileopen and thandlestream and it works ---------------------------------------------------------------------- Thaddy · 2022-12-12 12:31 Don't use CreateFileA, simply use CreateFile, which will resolve to the default for the OS. ---------------------------------------------------------------------- marcov · 2022-12-12 12:33 Quote from: metallaro1980 on December 12, 2022, 01:20:15 pmbecause the routines that copy all sectors of hard disk  don't start and do nothing createfile works only with name ... for example G: with c++ i can read the values of all sectors lazarus on linux i use: fileopen and thandlestream and it works Do you use CreateFileA or W on C++? ---------------------------------------------------------------------- metallaro1980 · 2022-12-12 20:06 hFile = CreateFile(            "\\\\.\\PhysicalDrive0",                           GENERIC_READ,            1,            NULL,            OPEN_EXISTING,            FILE_ATTRIBUTE_NORMAL,            NULL); but maybe now it works  with lazarus windows is windows  i prefer linux because with fileopen and thandlestream i know the size