Function to lock a file/dir

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
prashanthgnAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

jkrCommented:
You can lock files using '::LockFile()'. However, there is no way to do that for directories.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
cmaryusCommented:
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
HermeticCommented:
At what "scope" do you need to do it-  Only when your application is running or all the time?
0
Cloud Class® Course: C++ 11 Fundamentals

This course will introduce you to C++ 11 and teach you about syntax fundamentals.

williamcampbellCommented:


   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
prashanthgnAuthor Commented:
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
williamcampbellCommented:
Yes this equiv of a locked file. Semaphores have nothing to do with file locking they lock threads.
0
cmaryusCommented:
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
williamcampbellCommented:

 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
cmaryusCommented:
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
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
System Programming

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.