Lazarus

Programming => General => Topic started by: Jzhen on February 07, 2023, 10:14:16 am

Title: After the fileopen operation, the file using memStream.LoadFromFile is occupied
Post by: Jzhen on February 07, 2023, 10:14:16 am

Code: Pascal  [Select][+][-]
  1.   result:=True;
  2.   FFileHandle:=fileopen(filename,fmopenreadwrite);
  3.   if  FFileHandle=invalid_handle_value then
  4.   begin
  5.     //raise exception.Create('failed to open or create file');
  6.     result:=False;
  7.   end;
  8.   try
  9.     FFilesize:=Windows.GetFileSize(FFileHandle,nil);
  10.     FMapHandle:=createfilemapping(Ffilehandle,nil,Page_readwrite,0,FFilesize,nil);
  11.     if FMapHandle=0 then
  12.     BEGIN
  13.      //raise exception.Create('failed to create file mapping');
  14.      result:=False;
  15.     END;
  16.   finally
  17.     closehandle(FFileHandle);
  18.   end;
  19.   try
  20.     Fdata:=mapviewoffile(FMapHandle,file_map_all_access,0,0,ffilesize);
  21.     if fdata=nil then
  22.     BEGIN
  23.      //raise exception.Create('failed to map view of file ');
  24.      result:=False;
  25.     END;
  26.   finally
  27.     closehandle(FMapHandle);
  28.   end;
  29.  
  30.   memStream := TmemoryStream.Create;
  31.   memStream.LoadFromFile(filename);
  32.  
  33.  

After using fileopen to operate the file, use memStream. LoadFromFile (filename) to call the file again, and prompt that the file is occupied. Is there any missing way for me to close it
Title: Re: After the fileopen operation, the file using memStream.LoadFromFile is occupied
Post by: TRon on February 07, 2023, 10:38:23 am
Not my area of expertise but after a successful mapviewoffile aren't you suppose to use UnMapViewOfFile (https://learn.microsoft.com/en-us/windows/win32/api/memoryapi/nf-memoryapi-unmapviewoffile) function ?

see also: https://learn.microsoft.com/en-us/windows/win32/memory/creating-a-view-within-a-file
Title: Re: After the fileopen operation, the file using memStream.LoadFromFile is occupied
Post by: KodeZwerg on February 07, 2023, 10:55:59 am
Not my area of expertise but after a successful mapviewoffile aren't you suppose to use UnMapViewOfFile (https://learn.microsoft.com/en-us/windows/win32/api/memoryapi/nf-memoryapi-unmapviewoffile) function ?
You was a little faster than me and you are correct!
@Jzhen try to explain what you try to do at all?!
Title: Re: After the fileopen operation, the file using memStream.LoadFromFile is occupied
Post by: Jzhen on February 08, 2023, 03:02:46 am
Not my area of expertise but after a successful mapviewoffile aren't you suppose to use UnMapViewOfFile (https://learn.microsoft.com/en-us/windows/win32/api/memoryapi/nf-memoryapi-unmapviewoffile) function ?
Not my area of expertise but after a successful mapviewoffile aren't you suppose to use UnMapViewOfFile (https://learn.microsoft.com/en-us/windows/win32/api/memoryapi/nf-memoryapi-unmapviewoffile) function ?
You was a little faster than me and you are correct!
@Jzhen try to explain what you try to do at all?!
see also: https://learn.microsoft.com/en-us/windows/win32/memory/creating-a-view-within-a-file

 Thank you for solving the problem. I unmaps a mapped view of a file from the calling process's address space at the end of LoadFromFile, so this problem occurred. The cancel operation is successful before  LoadFromFile.
TinyPortal © 2005-2018