title: Odd sharing access violation (OS Error 32) with Freepascal implementation of libEWF
body: <p>I am hoping to implement the workings of a C library called <a href="https://github.com/libyal/libewf" rel="nofollow">libEWF</a> (by Joachim Metz) into my Freepascal program. A chap called Erwan has already converted an earlier version of the <a href="http://labalec.fr/erwan/?tag=ewfdelphilibewf" rel="nofollow">library to Delphi</a> early last year sometime, which I have an in turn tweaked a little for use with Freepascal. </p>
  
  <p>My problem relates to <a href="https://msdn.microsoft.com/en-us/library/windows/desktop/ms681382(v=vs.85).aspx" rel="nofollow">OS Error code 32 'access sharing violation'</a> which is returned by the following code and suggests there is a problem with accessing the output file (which is created as a zero byte file but nothing is ever written to it). </p>
  
  <pre><code>uses
  LibEWFUnit, Windows, ....  
  // Image\backup disk using EWF image format, return bytes imaged
  function WindowsImageDiskEWF(hDiskHandle : THandle; DiskSize : Int64) : Int64;
  var
    Buffer  : array [0..32767] of Byte; 
    fLibEWF : TLibEWF;
  
  begin
  
    // Initilise the libEWF instance
    fLibEWF:=TLibEWF.create;
    fLibEWF.libewf_SetCompressionValues(1,0);
    fLibEWF.libewf_SetHeaderValue('acquiry_software_version','YAFFI');
  
    // Create and open the output file, .e.g MyImage.E01 with write access      
    if fLibEWF.libewf_open(frmYaffi.ledtImageName.Text,LIBEWF_OPEN_WRITE)=0 then
    begin
    try      
    // Now to seek to start of device
     FileSeek(hDiskHandle, 0, 0);
       repeat
         // Read source device handle in buffered segments. 
         BytesRead     := FileRead(hDiskHandle, Buffer, SizeOf(Buffer));
         if BytesRead = -1 then
           begin
             RaiseLastOSError;
             exit;
           end;
         inc(TotalBytesRead, BytesRead);
  
         // Now write buffer to libEWF instance, but -1 keeps being returned
         BytesWritten  := fLibEWF.libewf_write_random(@Buffer, SizeOf(Buffer), TotalBytesRead);         
         if BytesWritten = -1 then
           begin
             RaiseLastOSError;  //THIS KEEPS BEING RAISED : OS ERROR 32
             exit;
           end;          
         inc(TotalBytesWritten, BytesWritten);
     .....
     finally
       fLibEWF.libewf_close;
     ..... 
    end;
  end;
  </code></pre>
  
  <p>and on it is supposed to go until TotalBytesRead equals the size of the input device. </p>
  
  <p>However, as the notes show, for some reason, -1 is always being returned by </p>
  
  <pre><code>BytesWritten  := fLibEWF.libewf_write_random(@Buffer, SizeOf(Buffer), BytesRead);
  </code></pre>
  
  <p>and RaiseLastOSError keeps generating OS error code 32 - access sharing violation. Using <a href="http://emcosoftware.com/unlock-it" rel="nofollow">UnlockIT</a> to check if anything else has a hold on the image file file, it shows only my prgram does. Same goes for the DLL file that comes with the converted library that Erwan supplies. </p>
  
  <p>I have tried contacting Erwan to see if he can help (as he manages to create E01 backup files with his utility <a href="http://labalec.fr/erwan/?cat=26" rel="nofollow">CloneDisk</a> which uses this same library but with Delphi) but an answer has not arrived yet and may never do. So I am hoping someone can offer me guidance on how to resolve this? Joachim Metz has been good enough to reply to me but he clearly has many projects on but despite that he was good enough to tell me that the sharing rights are assigned by <a href="https://github.com/libyal/libcfile/blob/master/libcfile/libcfile_file.c#L505" rel="nofollow">this other library, line 505 of libcfile_file.c</a>. But as my unit is from an earlier version of libEWF that was then converted to Delphi and then to Freepascal, I don't think I can use or rely on that. </p>
  
  <p>The converted Delphi unit that I have tweaked for use with Freepascal is visible <a href="https://github.com/tedsmith/yaffi/blob/master/ewfacquireForFPCAPI/LibEWFUnit.pas" rel="nofollow">HERE</a>. It is line 301 of that file that makes result = -1 which is how BytesWritten subsequently becomes equal to -1 and therefore fails. If anyone can see what changes I need to make, I'd be obliged. </p>
  
  <p>And before anyone says "just convert the C library to a pascal unit", can I suggest you try to do that before suggesting it! If it was that easy, I'd download the latest and most recent master from <a href="https://github.com/libyal/libewf" rel="nofollow">https://github.com/libyal/libewf</a> and create a new, fresh DLL and unit with all the modernisations. But I've spent three days battling with that concept and been met with nothing but failure.So Erwans Delphi unit is the best hope I have. Thanks   </p>