?
Solved

Function to lock a file/dir

Posted on 2003-03-17
12
Medium Priority
?
255 Views
Last Modified: 2013-11-20
Hi,

  Is there any function in MFC to lock a
  file/folder so that no other user or application
  can open/read it..?

Thanx in advance
Prashanth
0
Comment
Question by:prashanthgn
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
12 Comments
 
LVL 86

Accepted Solution

by:
jkr earned 252 total points
ID: 8151735
You can lock files using '::LockFile()'. However, there is no way to do that for directories.
0
 
LVL 3

Expert Comment

by:cmaryus
ID: 8152605
i don't know about that but you can use semaphores, evets, mutexes

i can give u a semaphore example if you're interested
0
 
LVL 1

Expert Comment

by:Hermetic
ID: 8153122
At what "scope" do you need to do it-  Only when your application is running or all the time?
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 12

Assisted Solution

by:williamcampbell
williamcampbell earned 248 total points
ID: 8153287


   You can open a File

   CFile myFile;

   myFile.Open ( "File", CFile::modeReadWrite|CFile::shareExclusive );

   To lockall the files in the Folder use

   FindFirstFile ( "*.*" , ...
   
      openFile shareExclusive here    

   FindNextFile ( ... )
 
0
 

Author Comment

by:prashanthgn
ID: 8157614
Im getting confused a little bit,

I tried with the code given below, in this Im just
opening a file and sleeping, In the time this sleeps
if i try to open the file with notepad, it will report
error saying the file cannot be opened and its used
by some other application.

     CFile f;
     char* pFileName = "c:\\temp\\SampleFile.txt";
     if( !f.Open( pFileName, CFile::modeReadWrite ) )
     {
          cout<<"File could not be opened "<<endl;
          return 1;
     }


     cout<<"sleeping ..."<<endl;

     Sleep(20000);

     cout<<"sleep over ..."<<endl;

     f.Close();


Is it equivalent to locking a file?
If yes then what's the purpose of calling ::LockFile
and use semaphores or other stuff..?

Regards
Prashanth
0
 
LVL 12

Expert Comment

by:williamcampbell
ID: 8160258
Yes this equiv of a locked file. Semaphores have nothing to do with file locking they lock threads.
0
 
LVL 3

Expert Comment

by:cmaryus
ID: 8164614
william: i agree that the CFile::shareExclusive  flag is probably the best solution, but i can assure you i can lock a read/write on a file with a semaphore. you don't lock the file, you lock the operation on the file

void writeFile(hFile, ...)
{
CSemaphore hSem = CreateSemaphore(..);
if (WaitForSingleObject(hSem, 1) == WAIT_TIMEOUT)
{
    // in this case it means that another process is calling this writeFile() function
    // here it can be implemented a while loop to check until the semaphore has been signaled, in this case it means we can write in the file
    return;
}

WriteFile(hFile, ...)

// now signal the semaphore
ReleaseSemaphore(hSem, ...);
}

If 2 or many applications use the same writeFile() function only the first one will write in the log file.
0
 
LVL 12

Expert Comment

by:williamcampbell
ID: 8168208

 A bit off topic for this problem but...

 1) hSem is local to the Writefile function so Release cannot access it.
 2) Writefile is local to one Application (how can another Application call a function inside another app)
 3) Even if the WriteFile Function is 'Locked' the hFile handle isn't.
 4) WaitForSingleObject for 1 second in a While loop is inefficient
 5) If You are trying to protect a shared resource so You should use a CriticalSection
 6) Semapohres are usually used as 'Counters' decrementing a certain number of times and then releasing.
 
0
 
LVL 3

Expert Comment

by:cmaryus
ID: 8172075
1) hSem is local to the Writefile function so Release cannot access it.
-> if the name of the semaphore is the same, CreateSemaphore will return a handle to the same semaphore, even if used in different applications
2) Writefile is local to one Application (how can another Application call a function inside another app)
-> yep, i know that, the ideea is that if another application uses the same writeFile function. if another apllication don't use it...semaphore has no effect
3) Even if the WriteFile Function is 'Locked' the hFile handle isn't.
-> see 2)
4) WaitForSingleObject for 1 second in a While loop is inefficient
-> it was just an example, you can set it to any value
5) If You are trying to protect a shared resource so You should use a CriticalSection
->here you are right, but i was just saying that you can use semaphores ...
6) Semapohres are usually used as 'Counters' decrementing a certain number of times and then releasing
-> true.

The point is that you can use semphores, events, critical sections, mutexes, but it depends on the case. In this case if  3'rd party application acces the file you cannot use them.
So i don't say this is the best solution, i said it's a possible solution.
0

Featured Post

Learn how to optimize MySQL for your business need

With the increasing importance of apps & networks in both business & personal interconnections, perfor. has become one of the key metrics of successful communication. This ebook is a hands-on business-case-driven guide to understanding MySQL query parameter tuning & database perf

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction: The undo support, implementing a stack. Continuing from the eigth article about sudoku.   We need a mechanism to keep track of the digits entered so as to implement an undo mechanism.  This should be a ‘Last In First Out’ collec…
Introduction: Dialogs (2) modeless dialog and a worker thread.  Handling data shared between threads.  Recursive functions. Continuing from the tenth article about sudoku.   Last article we worked with a modal dialog to help maintain informat…
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
NetCrunch network monitor is a highly extensive platform for network monitoring and alert generation. In this video you'll see a live demo of NetCrunch with most notable features explained in a walk-through manner. You'll also get to know the philos…
Suggested Courses

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question