INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Jobs

API Functions

How to lock a file using an API function by mgagnon
Posted: 8 Sep 02


DO decl

#DEFINE GENERIC_READ 2147483648 && 0x80000000
#DEFINE GENERIC_WRITE 1073741824 && 0x40000000
#DEFINE cret  Chr(13)
#DEFINE dcret Chr(13)+Chr(13)

    LOCAL lcPath, lcAlias, lcFile, hFile, lnHeader, lcBuffer
    lcPath  = GetTmpPath()
    lcAlias = "Test"
    lcFile  = lcPath + lcAlias + ".dbf"

    * creating a testing table
    IF USED (lcAlias)
        USE IN (lcAlias)
    ENDIF

    IF Not FILE(lcFile)
        SET SAFETY OFF
        CREATE TABLE (lcFile) (dt T)
        SET SAFETY ON
        INSERT INTO (lcAlias) VALUES (datetime())
    ELSE
        USE (lcFile) ALIAS (lcAlias) SHARED
    ENDIF

    lnHeader = HEADER()
    USE IN (lcAlias)

    lcBuffer = REPLI (Chr(0), 250)
    hFile = OpenFile (lcFile, @lcBuffer, GENERIC_READ)

    IF LockFile (hFile, 0,0, lnHeader,0) <> 0
        = MessageB ("File " + lcFile + " is locked." + dcret +;
            "At this point do not close this Dialog " +;
            "and start another VFP instance." + dcret +;
            "Use another instance to open " + lcFile + cret +;
            "or just try to read the tables on " + lcPath + " " + ;
            "using regular DIRECTORY command." + dcret +;
            "After that click Ok to unlock the target file.", 64, "Locking a file")

        = UnlockFile (hFile, 0,0, lnHeader,0)
    ELSE
        * 6 = ERROR_INVALID_HANDLE - already in exclusive use
        ? "Error code:", GetLastError()
    ENDIF
    = CloseHandle (hFile)

FUNCTION  GetTmpPath
    LOCAL lpBuffer, lnLength
    lpBuffer = SPACE(250)
    lnLength = GetTempPath (LEN(lpBuffer), @lpBuffer)
RETURN Iif (lnLength=0, "C:\Temp", PADR(lpBuffer, lnLength))

PROCEDURE  decl
    DECLARE INTEGER GetLastError IN kernel32

    DECLARE INTEGER GetTempPath IN kernel32;
        INTEGER nBufferLength, STRING @ lpBuffer

    DECLARE INTEGER LockFile IN kernel32;
        INTEGER hFile, INTEGER dwFileOffsetLow,;
        INTEGER dwFileOffsetHigh,;
        INTEGER nNumberOfBytesToLockLow,;
        INTEGER nNumberOfBytesToLockHigh

    DECLARE INTEGER UnlockFile IN kernel32;
        INTEGER hFile, INTEGER dwFileOffsetLow,;
        INTEGER dwFileOffsetHigh,;
        INTEGER nNumberOfBytesToUnlockLow,;
        INTEGER nNumberOfBytesToUnlockHigh

    DECLARE INTEGER OpenFile IN kernel32;
        STRING   lpFileName,;
        STRING @ lpReOpenBuff,;
        INTEGER  wStyle

    DECLARE INTEGER CloseHandle IN kernel32;
        INTEGER hObject


Mike Gagnon

Back to Microsoft: Visual FoxPro FAQ Index
Back to Microsoft: Visual FoxPro Forum

My Archive

Resources

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close