AGB  ·  Datenschutz  ·  Impressum  







Anmelden
Nützliche Links
Registrieren
Zurück Delphi-PRAXiS Programmierung allgemein Win32/Win64 API (native code) Lock File but My app still can read it?
Thema durchsuchen
Ansicht
Themen-Optionen

Lock File but My app still can read it?

Ein Thema von user · begonnen am 10. Feb 2011 · letzter Beitrag vom 11. Feb 2011
Antwort Antwort
user

Registriert seit: 13. Jul 2010
25 Beiträge
 
Delphi 2 Desktop
 
#1

Lock File but My app still can read it?

  Alt 10. Feb 2011, 03:56
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!
  Mit Zitat antworten Zitat
shmia

Registriert seit: 2. Mär 2004
5.508 Beiträge
 
Delphi 5 Professional
 
#2

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

  Alt 10. Feb 2011, 17:12
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;
Andreas
  Mit Zitat antworten Zitat
Benutzerbild von Assarbad
Assarbad

Registriert seit: 8. Okt 2010
Ort: Frankfurt am Main
1.234 Beiträge
 
#3

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

  Alt 10. Feb 2011, 18:53
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).
Oliver
"... aber vertrauen Sie uns, die Physik stimmt." (Prof. Harald Lesch)
  Mit Zitat antworten Zitat
user

Registriert seit: 13. Jul 2010
25 Beiträge
 
Delphi 2 Desktop
 
#4

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

  Alt 11. Feb 2011, 03:51
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.
  Mit Zitat antworten Zitat
Antwort Antwort


Forumregeln

Es ist dir nicht erlaubt, neue Themen zu verfassen.
Es ist dir nicht erlaubt, auf Beiträge zu antworten.
Es ist dir nicht erlaubt, Anhänge hochzuladen.
Es ist dir nicht erlaubt, deine Beiträge zu bearbeiten.

BB-Code ist an.
Smileys sind an.
[IMG] Code ist an.
HTML-Code ist aus.
Trackbacks are an
Pingbacks are an
Refbacks are aus

Gehe zu:

Impressum · AGB · Datenschutz · Nach oben
Alle Zeitangaben in WEZ +1. Es ist jetzt 12:39 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