Recent

Author Topic: CreateFileA and high Thandle value  (Read 503 times)

metallaro1980

  • New Member
  • *
  • Posts: 36
CreateFileA and high Thandle value
« on: December 07, 2022, 01:28:10 pm »
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][+][-]
  1.     ADrive := '\\.\' + edit2.text;
  2.     //CreateFile(Path, GENERIC_READ, 0, IntPtr.Zero, OPEN_EXISTING, 0, IntPtr.Zero);
  3.     hVol := CreateFile(pchar(ADrive), GENERIC_READ, FILE_SHARE_READ or FILE_SHARE_WRITE, nil, OPEN_EXISTING, 0,0);
  4.     showmessage(inttostr(GetLastError));

what is wrong with this code?
THandle returned = 446744073709551615

exe with admin rights

best regards
andrea verdi
« Last Edit: December 07, 2022, 01:40:21 pm by metallaro1980 »

marcov

  • Administrator
  • Hero Member
  • *
  • Posts: 10492
  • FPC developer.
Re: CreateFileA and high Thandle value
« Reply #1 on: December 07, 2022, 01:47:42 pm »
So you are probably in 64-bit ?

metallaro1980

  • New Member
  • *
  • Posts: 36
Re: CreateFileA and high Thandle value
« Reply #2 on: December 07, 2022, 01:52:33 pm »
yes 64bit

440bx

  • Hero Member
  • *****
  • Posts: 3268
Re: CreateFileA and high Thandle value
« Reply #3 on: December 07, 2022, 03:53:29 pm »
what 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
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.
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.
FPC v3.0.4 and Lazarus 1.8.2 on Windows 7 SP1 64bit.

metallaro1980

  • New Member
  • *
  • Posts: 36
Re: CreateFileA and high Thandle value
« Reply #4 on: December 07, 2022, 05:12:18 pm »
yes
i used \\.\PhysicalDriveX
but on linux i can read the hard disk
with
Code: Pascal  [Select][+][-]
  1. procedure TForm1.Button1Click(Sender: TObject);
  2. var h:thandle;
  3. var hs : thandlestream;
  4. var b512 : Array512;
  5. var b5120: Array5120;
  6. var b:array of byte;
  7. var i : integer;
  8. var rimanenti:int64;
  9. var b2: Buffer10240;
  10.  
  11. begin
  12.   try
  13.     try
  14.  
  15.     button1.enabled := false;
  16.     bitbtn1.enabled := false;
  17.  
  18.     h := fileopen(edit1.text, fmOpenWrite or fmShareExclusive);
  19.     hs := thandlestream.create(h);
  20.  
  21.     for i := 0 to length(b512) - 1 do
  22.     begin
  23.       b512[i] := 0;
  24.     end;
  25.  
  26.  
  27.     for i := 0 to length(b5120) - 1 do
  28.     begin
  29.       b5120[i] := 0;
  30.     end;
  31.  
  32.     for i := 0 to length(b2) - 1 do
  33.     begin
  34.       b2[i] := 0;
  35.     end;
  36.  
  37.     sizedisk := hs.size;
  38.     rimanenti := hs.Size - 512;
  39.     perc := 0;
  40.     perc += 512;
  41.  
  42.     hs.Write(b512[0], 512);
  43.  
  44.     if (checkbox1.checked = false) then
  45.     begin
  46.       while (rimanenti >= 5120) do
  47.       begin
  48.         hs.write(b5120[0], 5120);
  49.         rimanenti := rimanenti - 5120;
  50.         perc += 5120;
  51.         application.ProcessMessages;
  52.       end;
  53.  
  54.       if (rimanenti > 0) then
  55.       begin
  56.         setlength(b, rimanenti);
  57.         perc += rimanenti;
  58.         for i := 0 to length(b) - 1 do
  59.         begin
  60.           b[i] := 0;
  61.         end;
  62.         hs.write(b[0], sizeof(b));
  63.          application.ProcessMessages;
  64.       end;
  65.     end
  66.     else
  67.     begin
  68.       while (rimanenti >= 5120*2) do
  69.       begin
  70.         hs.write(b2[0], 5120*2);
  71.         rimanenti := rimanenti - 5120*2;
  72.         perc += 5120*2;
  73.         application.ProcessMessages;
  74.       end;
  75.  
  76.       if (rimanenti > 0) then
  77.       begin
  78.         setlength(b, rimanenti);
  79.         perc += rimanenti;
  80.         for i := 0 to length(b) - 1 do
  81.         begin
  82.           b[i] := 0;
  83.         end;
  84.         hs.write(b[0], sizeof(b));
  85.          application.ProcessMessages;
  86.       end;
  87.  
  88.  
  89.  
  90.  
  91.     end;
  92.  
  93.  
  94.  
  95.     except
  96.       on e: exception do
  97.       begin
  98.  
  99.       end;
  100.  
  101.     end;
  102.  
  103.   finally
  104.     fileclose(h);
  105.     if (Assigned(hs)) then  hs.Free;
  106.     button1.enabled := true;
  107.     bitbtn1.enabled := true;
  108.   end;
  109.  
  110.  
  111. end;  
  112.  
  113.  
  114.  
  115.  

Remy Lebeau

  • Hero Member
  • *****
  • Posts: 1168
    • Lebeau Software
Re: CreateFileA and high Thandle value
« Reply #5 on: December 11, 2022, 03:43:00 am »
when 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.
Remy Lebeau
Lebeau Software - Owner, Developer
Internet Direct (Indy) - Admin, Developer (Support forum)

metallaro1980

  • New Member
  • *
  • Posts: 36
Re: CreateFileA and high Thandle value
« Reply #6 on: December 12, 2022, 01:20:15 pm »
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

  • Hero Member
  • *****
  • Posts: 12621
Re: CreateFileA and high Thandle value
« Reply #7 on: December 12, 2022, 01:31:03 pm »
Don't use CreateFileA, simply use CreateFile, which will resolve to the default for the OS.
The only thing I can say about Putin - born st Petersburg- is that he is indeed Russian, as opposed to Stalin, who was Georgian. Depending of historical time frame they could both be Lithuanian or Polish...even German. Shut him up!

marcov

  • Administrator
  • Hero Member
  • *
  • Posts: 10492
  • FPC developer.
Re: CreateFileA and high Thandle value
« Reply #8 on: December 12, 2022, 01:33:18 pm »
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

Do you use CreateFileA or W on C++?

metallaro1980

  • New Member
  • *
  • Posts: 36
Re: CreateFileA and high Thandle value
« Reply #9 on: December 12, 2022, 09:06:41 pm »
    hFile = CreateFile(
           "\\\\.\\PhysicalDrive0",                 
           GENERIC_READ,
           1,
           NULL,
           OPEN_EXISTING,
           FILE_ATTRIBUTE_NORMAL,
           NULL);

but maybe now it works  :o with lazarus
windows is windows  :D
i prefer linux because with fileopen and thandlestream i know the size
« Last Edit: December 12, 2022, 09:21:22 pm by metallaro1980 »

 

TinyPortal © 2005-2018