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.

Students Click Here

Microsoft: FoxPro FAQ

API Functions

How to lock a file using an API function by Mike Gagnon
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)

    IF Not FILE(lcFile)
        CREATE TABLE (lcFile) (dt T)
        INSERT INTO (lcAlias) VALUES (datetime())
        USE (lcFile) ALIAS (lcAlias) SHARED

    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)
        * 6 = ERROR_INVALID_HANDLE - already in exclusive use
        ? "Error code:", GetLastError()
    = CloseHandle (hFile)

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

    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: FoxPro FAQ Index
Back to Microsoft: FoxPro Forum

My Archive

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