Kbase P26125: How to check if a file is busy using 4GL?
Autor |
  Progress Software Corporation - Progress |
Acesso |
  Público |
Publicação |
  5/29/2003 |
|
Status: Unverified
GOAL:
How to check if a file is busy using 4GL?
FACT(s) (Environment):
Windows
FIX:
This solution issues several windows API function calls to determine the current status of a given file. The FindFirstFileA function and the other functions used in this solution may be further exploited to yield a treasure of information regarding the file at hand.
DEFINE VARIABLE clpFileName AS CHARACTER NO-UNDO.
DEFINE VARIABLE mlpFindFileData AS MEMPTR NO-UNDO.
DEFINE VARIABLE iReturnValue AS INTEGER NO-UNDO.
DEFINE VARIABLE hFileHandle AS INTEGER NO-UNDO.
DEFINE VARIABLE lFileExists AS LOGICAL NO-UNDO.
/* Check if the file exists */
ASSIGN
SET-SIZE(mlpFindFileData) = 338
clpFileName = "C:\WRK91D\IsTheFileInUse.p".
RUN FindFirstFileA(
INPUT clpFileName,
INPUT mlpFindFileData,
OUTPUT iReturnValue) NO-ERROR.
ASSIGN
lFileExists = iReturnValue <> -1.
ASSIGN
SET-SIZE(mlpFindFileData) = 0.
/* Attempt to open the file with exclusive lock */
IF lFileExists THEN DO:
RUN CreateFileA (
INPUT clpFileName, /* File Name */
INPUT 1073741824, /* Generic Write */
INPUT 0, /* Exclusive */
INPUT 0, /* Security */
INPUT 3, /* Existing */
INPUT 71679, /* Normal */
INPUT 0, /* No Tempalte */
OUTPUT hFileHandle ) NO-ERROR.
/* If file is busy get Syetem Error Code */
IF (hFileHandle = -1 ) THEN DO:
RUN GetLastError(OUTPUT iReturnValue) NO-ERROR.
MESSAGE
"The file is busy" "~n"
"System Error Code:" iReturnValue "~n"
"For Error Code Deails Visit:" "~n"
"http://msdn.microsoft.com/library/default.asp?url=/library/en-us/debug/base/system_error_codes.asp"
VIEW-AS ALERT-BOX INFO BUTTONS OK.
END.
/* Else close its handle to make available */
ELSE DO:
RUN CloseHandle(INPUT hFileHandle, OUTPUT iReturnValue) NO-ERROR.
MESSAGE "File is available for use "
VIEW-AS ALERT-BOX INFO BUTTONS OK.
END.
END.
ELSE /* File does not exist */
MESSAGE "File does not exist"
VIEW-AS ALERT-BOX INFO BUTTONS OK.
DEFINE VARIABLE idwDesiredAccess AS INTEGER NO-UNDO.
DEFINE VARIABLE idwShareMode AS INTEGER NO-UNDO.
DEFINE VARIABLE ilpSecurityAttributes AS INTEGER NO-UNDO.
DEFINE VARIABLE idwCreationDisposition AS INTEGER NO-UNDO.
DEFINE VARIABLE idwFlagsAndAttributes AS INTEGER NO-UNDO.
DEFINE VARIABLE ihTemplateFile AS INTEGER NO-UNDO.
PROCEDURE FindFirstFileA EXTERNAL "kernel32.dll":
DEFINE INPUT PARAMETER lpFileName AS CHARACTER.
DEFINE INPUT PARAMETER lpFindFileData AS MEMPTR.
DEFINE RETURN PARAMETER iReturnValue AS LONG.
END.
PROCEDURE CreateFileA EXTERNAL "kernel32.dll":
DEFINE INPUT PARAMETER lpFileName AS CHAR.
DEFINE INPUT PARAMETER dwDesiredAccess AS LONG.
DEFINE INPUT PARAMETER dwShareMode AS LONG.
DEFINE INPUT PARAMETER lpSecurityAttributes AS LONG.
DEFINE INPUT PARAMETER dwCreationDisposition AS LONG.
DEFINE INPUT PARAMETER dwFlagsAndAttributes AS LONG.
DEFINE INPUT PARAMETER hTemplateFile AS LONG.
DEFINE RETURN PARAMETER iReturnValue AS LONG.
END.
PROCEDURE GetLastError EXTERNAL "kernel32.dll":
DEFINE RETURN PARAMETER iReturnValue AS LONG.
END.
PROCEDURE CloseHandle EXTERNAL "kernel32":
DEFINE INPUT PARAMETER hObject AS LONG.
DEFINE RETURN PARAMETER res AS LONG.
END.