Jump to content
Sign in to follow this  

LockFile Function

Recommended Posts


Hello Guys ,

I have some thing new , but I dont know how can I accomplish this task.

i searched in MSDN library for temporarily lock any file.

I found this information.

LockFile Function

Locks the specified file for exclusive access by the calling process.

To specify additional options, use the LockFileEx function.



__in HANDLE hFile,

__in DWORD dwFileOffsetLow,

__in DWORD dwFileOffsetHigh,

__in DWORD nNumberOfBytesToLockLow,

__in DWORD nNumberOfBytesToLockHigh



hFile [in]

A handle to the file. The file handle must have been created with the GENERIC_READ or GENERIC_WRITE access right. For more information, see File Security and Access Rights.

dwFileOffsetLow [in]

The low-order 32 bits of the starting byte offset in the file where the lock should begin.

dwFileOffsetHigh [in]

The high-order 32 bits of the starting byte offset in the file where the lock should begin.

nNumberOfBytesToLockLow [in]

The low-order 32 bits of the length of the byte range to be locked.

nNumberOfBytesToLockHigh [in]

The high-order 32 bits of the length of the byte range to be locked.

Return Value

If the function succeeds, the return value is nonzero.

If the function fails, the return value is zero. To get extended error information, call GetLastError.


If the call to LockFile completes synchronously, a completion entry may not be queued when a completion port is associated with the file handle.

The UnlockFile function unlocks a file region locked by LockFile.

Locking a region of a file gives the threads of the locking process exclusive access to the specified region using this file handle. If the file handle is inherited by a process created by the locking process, the child process is not granted access to the locked region. If the locking process opens the file a second time, it cannot access the specified region through this second handle until it unlocks the region.

Locking a region of a file does not prevent reading from a mapped file view.

You can lock bytes that are beyond the end of the current file. This is useful to coordinate adding records to the end of a file.

Locks may not overlap an existing locked region of the file.

If LockFile cannot lock a region of a file, it returns zero immediately. It does not block. To issue a file lock request that will block until the lock is acquired, use LockFileEx without LOCKFILE_FAIL_IMMEDIATELY.

If a process terminates with a portion of a file locked or closes a file that has outstanding locks, the locks are unlocked by the operating system. However, the time it takes for the operating system to unlock these locks depends upon available system resources. Therefore, it is recommended that your process explicitly unlock all files it has locked when it terminates. If this is not done, access to these files may be denied if the operating system has not yet unlocked them.


For an example, see Appending One File to Another File.


Client Requires Windows Vista, Windows XP, or Windows 2000 Professional.

Server Requires Windows Server 2008, Windows Server 2003, or Windows 2000 Server.

Header Declared in WinBase.h; include Windows.h.

Library Use Kernel32.lib.

DLL Requires Kernel32.dll.

I tried my best, but in vain :)

Please guys help me.

73 108 111 118 101 65 117 116 111 105 116

Share this post

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this