Click or drag to resize

FFLock64 Function

X#
Lock a portion of an open file.

Namespace:  XSharp.Core
Assembly:  XSharp.Core (in XSharp.Core.dll) Version: 2.21
Syntax
 FUNCTION FFLock64(
	ptrHandle AS IntPtr,
	offset AS INT64,
	length AS INT64
) AS LOGIC
Request Example View Source

Parameters

ptrHandle
Type: IntPtr
The handle of the open file you want to lock.
offset
Type: Int64
The file offset at which to start locking.
A value of zero corresponds to the first byte of the file.
length
Type: Int64
The number of bytes to lock.

Return Value

Type: Logic
TRUE if successful; otherwise, FALSE.
Remarks
FFLock() locks a specified portion of a file.
This prevents other processes from accessing that portion. More than one portion of a file can be locked, but the same byte should not be locked more than once.
It is also possible to lock past the end of a file.
This allows you to append to a file in a multi-user environment.
Remarks
Tip Tip
The low level File IO functions in the X# runtime are using .Net filestreams in the background.
That means that the file handles returned by FOpen() and FCreate() are not 'normal' file handles, but unique identifiers that are used to find the underlying stream object in a collection of streams in the runtime.
That also means that you can't use file handles for functions such as FRead() and FWrite() that were not created in the X# runtime.
If you want to access the underlying FileStream, then you should call the function FGetStream(IntPtr)
Examples
This example uses FFLock() to lock 100 bytes of a file starting at offset 300:
X#
1ptrHandle := FOpen2("c:\data\myfile.txt", ;
2        FO_READWRITE + FO_SHARED)
3IF FFLock(ptrHandle, 300, 100)
4    ? "Locked OK"
5ENDIF
This example implements an FFLock() with an optional retry:
X#
 1FUNCTION FileLock(ptrHandle AS PTR,;
 2        dwOffset AS DWORD, dwLength AS DWORD,;
 3        wSeconds AS DWORD) AS LOGIC PASCAL
 4    LOCAL lForever AS LOGIC
 5    IF FFLock(ptrHandle, dwOffset, dwLength)
 6        RETURN TRUE            // Locked
 7    ENDIF
 8    // If wSeconds is zero, retry until successful
 9    lForever := (wSeconds = 0)
10    DO WHILE (lForever .OR. wSeconds > 0)
11        IF FFLock(ptrHandle, dwOffset, dwLength)
12            RETURN TRUE    // Locked
13        ENDIF
14        InKey(.5)            // Wait up
15        nSeconds -= 1
16    ENDDO
17    RETURN FALSE                // Not locked
See Also