Search - Articles
DevASP.NET for ASP.NET, VB.NET, XML and C# (C-Sharp) Developers Tuesday, March 03, 2009
Dev Articles
Search Directory
ASP.NET
VB.Net
C-Sharp
SQL Server
 

How can we Lock Files.

Author: DevASP
Download Source Code : 632_Locking Files src.zip

The CreateFile function’s third parameter controls the accessibility of an open file to other processes. For example, if you specify FILE_SHARE_READ when you open a file with CreateFile, then............

While this file-wide access control scheme is appropriate in many cases, there are many others where it is simply too general. For example, you may have a file of structures available on a server. It might contain employee records, and there may be many users on the network who need to simultaneously access the file to retrieve or modify employee information. The file-wide access control system implemented by CreateFile has a granularity that is too common for this sort of activity. What you want is the ability to lock individual records while they are in use, and to then free them up when the user has finished with them.Many users may need access to different parts of the same file. File locking lets each user lock just the part they need. The APIs offers two different functions, LockFile and LockFileEx, to implement this functionality.

 

LockFile:         

Locks a range of bytes Signature of this function looks like this.

 

 

BOOL LockFile(HANDLE fileHandle,DWORD fileOffsetLow,DWORD fileOffsetHigh,DWORD lockLow,DWORD lockHigh);

 

fileHandle-                     Handle to the file to lock

fileOffsetLow-                 Offset to start of locked section (low-order 32 bits)

fileOffsetHigh-                Offset to start of locked section (high-order 32 bits)

lockLow-                       Number of bytes to lock (low-order 32 bits)

lockHigh-                       Number of bytes to lock (high-order 32 bits)

Returns TRUE on success.

 

UnlockFile:

Unlocks a locked range of bytes Signature of this function looks like this.

 

BOOL UnlockFile(HANDLE fileHandle,DWORD fileOffsetLow,DWORD fileOffsetHigh,DWORD unlockLow,DWORD unlockHigh);

 

fileHandle-                     Handle to the file to lock

fileOffsetLow-                 Offset to start of locked section (low-order 32 bits)

fileOffsetHigh-                Offset to start of locked section (high-order 32 bits)

unlockLow-                    Number of bytes to unlock (low-order 32 bits)

unlockHigh-                   Number of bytes to unlock (high-order 32 bits)

Returns TRUE on success.

 

LockFileEx:

Locks a range of bytes Signature of this function looks like this.

 

BOOL LockFileEx(HANDLE fileHandle,DWORD flags,DWORD reserved,

   DWORD numBytesLow,DWORD numBytesHigh,LPOVERLAPPED overlapped);

 

fileHandle-                     Handle to file created by CreateFile

flags-                            Access flags

reserved-                      Must be zero

numBytesLow-              Number of bytes to lock (low-order 32 bits)

numBytesHigh-              Number of bytes to lock (high-order 32 bits)

overlapped-                    Structure containing starting address of block

Returns TRUE on success.

 

UnlockFileEx:

Unlocks a locked range of bytes Signature of this function looks like this.

 

BOOL UnlockFileEx(HANDLE file,DWORD reserved,DWORD numBytesLow,

   DWORD numBytesHigh,LPOVERLAPPED overlapped);

 

Flags-                           Access flags

Reserved-                      Must be zero

NumBytesLow-              Number of bytes to lock (low-order 32 bits)

NumBytesHigh-             Number of bytes to lock (high-order 32 bits)

Overlapped-                   Structure containing starting address of block

Returns TRUE on success.

 

The LockFileEx function is slightly more difficult to use because you have to create an OVERLAPPED structure and place into it the starting address of the block that you want to lock (the rest of the fields in the structure are unused). However, LockFileEx offers more control over the locking process and is therefore preferred in many cases.In this Article example code I have example code which describe how to use the LockFile and UnlockFile to lock files. The code demonstrates how to lock a structure in a file using the LockFile function. The program assumes the existence of a data file created by this code, and locks record three in the file. The program then waits for the user to press return to unlock the record and continue. First create a dialog based application named “Locking Files” and write the code given below at the top of the file.

 

//global variable to store the file name

char filename[]="C:\\data.txt";

//Handle to the locked file

HANDLE fileHandle;     

//structure to unlock from the file

typedef struct    

{

      int a, b, c;

} data;

 

File name is the path to a file to which multiple applications will access. fileHandle is the global handle to the file.” data” is the structure variable that we are going to lock in the file. We are going to lock the third record in the file Now Create a file in the Root of C directory named “data.txt” and place some data in it in this format

1 2 3

4 5 6

7 8 9

0 1 2

2 3 4

From the above data, I will explain that how our function will Lock the file for 3rd record. Add a button on the dialog and make its Caption property to “Lock File” and add the following code after double click in the button.

 

BOOL success;

     

      // Open the file

      fileHandle = CreateFile(filename, GENERIC_READ,FILE_SHARE_READ,0, OPEN_EXISTING, 0, 0);

      if (fileHandle == INVALID_HANDLE_VALUE)

      {

            MessageBox("Cant open File");

            return;

      }

 

      // Lock record 3, wait, and unlock

      success = LockFile(fileHandle, sizeof(data) * 2,0, sizeof(data), 0);

      if (!success)

            MessageBox("File Cant be Locked");

      else

            MessageBox("File is Locked");

 

Now add another button and make its Caption property to “UnLock Files” and add the following code after double click on it.

 

 

BOOL success;

 

      success=UnlockFile(fileHandle, sizeof(data) * 2, 0, sizeof(data), 0);

      if(success)

            MessageBox("File is unlocked");

      else

            MessageBox("File Cant be UnLocked");

 

Delete the Cancel Button and change the Ok button Caption to “CloseHandle” then add the following code after double click on it. Now the code in the function OnBnClickedOk should be look like this

 

CloseHandle(fileHandle);

      OnOK();

 

Now compile the code. it will show u a dialog just close it. To check whether the file is being locked or not make the two copies of this project on different location and run them both and then lock the file from one application then true to lock it through another application. You will Get the result.

Article Comments
can u pls mail me the above code in vb.net as i have developed dataentry form for multiply user and the above code will be usefull for me

Posted on 5/31/2007 1:59:48 PM by ROCKY

how we can lock files? please tell me.

Posted on 9/24/2008 4:35:06 AM by handsome

   
Add Article Comment:
Name :
Email Address :
   
Comments :
 
   
<< How we can Make Our Windows Controls Unicode Supportive.

Disclaimer - Privacy
© 2002-2017 DevASP.net