Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

Function to lock a file/dir

Posted on 2003-03-17
12
Medium Priority
?
267 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
9 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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
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

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!

Question has a verified solution.

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

Introduction: Displaying information on the statusbar.   Continuing from the third article about sudoku.   Open the project in visual studio. Status bar – let’s display the timestamp there.  We need to get the timestamp from the document s…
Introduction: Hints for the grid button.  Nested classes, templated collections.  Squash that darned bug! Continuing from the sixth article about sudoku.   Open the project in visual studio. First we will finish with the SUD_SETVALUE messa…
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.
this video summaries big data hadoop online training demo (http://onlineitguru.com/big-data-hadoop-online-training-placement.html) , and covers basics in big data hadoop .
Suggested Courses

577 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