Recent

Author Topic: After the fileopen operation, the file using memStream.LoadFromFile is occupied  (Read 478 times)

Jzhen

  • New Member
  • *
  • Posts: 23

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

TRon

  • Hero Member
  • *****
  • Posts: 834
Not my area of expertise but after a successful mapviewoffile aren't you suppose to use UnMapViewOfFile function ?

see also: https://learn.microsoft.com/en-us/windows/win32/memory/creating-a-view-within-a-file
« Last Edit: February 07, 2023, 10:43:27 am by TRon »

KodeZwerg

  • Hero Member
  • *****
  • Posts: 1178
  • Fifty shades of code.
    • Delphi & FreePascal
Not my area of expertise but after a successful mapviewoffile aren't you suppose to use UnMapViewOfFile function ?
You was a little faster than me and you are correct!
@Jzhen try to explain what you try to do at all?!
« Last Edit: Tomorrow at 31:76:97 by KodeZwerg »

Jzhen

  • New Member
  • *
  • Posts: 23
Not my area of expertise but after a successful mapviewoffile aren't you suppose to use UnMapViewOfFile function ?
Not my area of expertise but after a successful mapviewoffile aren't you suppose to use 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