Delphi-PRAXiS

Delphi-PRAXiS (https://www.delphipraxis.net/forum.php)
-   Win32/Win64 API (native code) (https://www.delphipraxis.net/17-win32-win64-api-native-code/)
-   -   Lock File but My app still can read it? (https://www.delphipraxis.net/158234-lock-file-but-my-app-still-can-read.html)

user 10. Feb 2011 02:56

Lock File but My app still can read it?
 
Let's say my app require a file. While my app actives in memory, I want to lock the file from being deleted and my app still can read the content of the file. I don't want to use LockFile API because every time my app read the file, it have to unlock the file (with UnlockFile), considering my app is a multi-threading app.

I won't store the content of file into memory, it will consume much memory. I wonder if there is any tricks or other API to solve my issue? No malicious way!

shmia 10. Feb 2011 16:12

AW: Lock File but My app still can read it?
 
There are two different ways in window to lock files or parts of files.
1.) file locking
While a file is opened it is protected against deleting.
Windows doesn't allow to delete a file while it is opened by one or more processes.
2.) record locking
With LockFile() and UnlockFile() you can lock a range of bytes to prevent an other process to read or write this part of the file.

Delphi-Quellcode:
var
  fs : TFileStream;
  s : Ansistring;
begin
  fs := TFileStream.Create(filename, fmOpenRead or fmShareDenyWrite);
  try
    // File is open now and can't be deleted
    ...
    // read some data from file
    SetLength(s, 100);
    fs.ReadBuffer(s[1], 100);

    ....

  finally
    fs.free; // close file handle and free memory for FileStream object
  end;
end;

Assarbad 10. Feb 2011 17:53

AW: Lock File but My app still can read it?
 
Zitat:

Zitat von user (Beitrag 1080738)
I won't store the content of file into memory, it will consume much memory. I wonder if there is any tricks or other API to solve my issue?

Absolutely. Use MMFs (memory mapped files) instead and open the file in a way that gives your process exclusive access.

See: MSDN-Library durchsuchenCreateFileMapping, MSDN-Library durchsuchenMapViewOfFileEx, MSDN-Library durchsuchenFlushViewOfFile, MSDN-Library durchsuchenUnmapViewOfFile

I reckon the set of API names does not necessarily give away how useful it is. You intend not to keep all of it in memory, fine. Go MMF. MMFs will allow you to map only a portion and even then this view is backed by the file itself. Consider it something like the principle of the page file, just inverse. But the best is that you can treat the mapped view as though it was a buffer in memory. The memory manager of Windows will take care that it gets paged back in whenever you access a page that has been paged out (i.e. is/was backed by the file you created the mapping of).

But may I ask why exactly - or rather what for - you need the locking? If you just want to prevent other processes from touching the file(s), why not open it exclusively with MSDN-Library durchsuchenCreateFile with only MSDN-Library durchsuchenFILE_SHARE_READ or even 0? Shouldn't that resolve your issue (even without MMF)? This will allow any other processes to open the file, but only for reading. So your process would be the only one able to write to the file using that particular handle (subsequent handles will have the same limitations, no matter whether inside your process or another one).

user 11. Feb 2011 02:51

AW: Lock File but My app still can read it?
 
My program only read a specific line from the file and I want to prevent the file being deleted by other processes or human. I think from Assarbad's description CreateFile with FILE_SHARE_READ will resolve my problem. I am away from my keyboard, I'll try the code later and confirm it.


Alle Zeitangaben in WEZ +1. Es ist jetzt 04:41 Uhr.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
LinkBacks Enabled by vBSEO © 2011, Crawlability, Inc.
Delphi-PRAXiS (c) 2002 - 2023 by Daniel R. Wolf, 2024 by Thomas Breitkreuz