Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Is there?
Message
General information
Forum:
Visual FoxPro
Category:
Windows API functions
Title:
Miscellaneous
Thread ID:
00175162
Message ID:
00175183
Views:
30
The code below will provide you with the functionality that you are looking for.
It requires the POINTERS.VCX class which you can find here in the Universal Thread.
The user running this function MUST have administrator access.

In my implementation, I load an array containing the users that have the file opened, as well as some additional information into an array. I then process the array.

Just take the code below, please it in a prg and run it. Remember to download POINTERS.VCX.

CLEAR

m.lcServer = "\\continuum" && the server name
m.lcFile = "d:\putz\data" && the actual server path
m.lcUserID = "" && user to check

? GetNetFilesOpened( m.lcServer, m.lcFile, m.lcUserID, "aFils" )

DISPLAY MEMO LIKE aFils*

*******************************
FUNCTION GetNetFilesOpened
*******************************
LPARAMETERS tcServerName, tcBasePath, tcUserName, tcArrayName
*-- This function returns information about open files.
* It returns the files open only if they were
* opened using a share on that computer.
*
*-- It uses:
* - the NetFileEnum Win32 API function
* to retrieve the wanted information from the OS.
* - the Pointers Control to convert the result
* from NetFileEnum to VFP info.
*
*-- Parameters:
* 1. The server name. It can also be a domain name.
* Use an empty string for the current computer.
* Ex: MyServer
* 2. The base path. Only the files in this directory
* (and all subdirectories) will be returned.
* 3. The user name.
* Use an empty string for all users.
* Ex: JoeDoe

LOCAL lcServerName, lcBasePath, lcUserName, lnBufferPointer
LOCAL lnPreferedMaxLength, lnEntriesRead, lnTotalEntries
LOCAL lnResumeHandle, lnError
LOCAL loPointersObject
LOCAL lnI, lcDll
LOCAL llContinue, lnpFileInfo, lnpFileName, lcFileName
LOCAL lnLocks, lnpUserName, lcUserName

*-- Returned values
#DEFINE ERROR_ACCESS_DENIED 5 && The user does not have access to the requested information.
#DEFINE ERROR_INVALID_LEVEL 124 && The value specified for the Level parameter is invalid.
#DEFINE ERROR_MORE_DATA "N/A" && More entries are available. Specify a large enough buffer to receive all entries.
#DEFINE ERROR_NOT_ENOUGH_MEMORY 234 && Insufficient memory is available.
#DEFINE NERR_BufTooSmall 2123 && The supplied buffer is too small.

*-- Release the array if it already exists
RELEASE &tcArrayName.

IF " NT " $ OS()
*-- NetFileEnum() is in NetAPI32.dll on Windows NT
lcDll = "NETAPI32"
ELSE
*-- NetFileEnum() is in SvrAPI.dll on Windows 9x
lcDll = "SVRAPI"
ENDIF

lnError = -1

DECLARE INTEGER NetFileEnum IN &lcDll ;
STRING @ServerName, ;
STRING @BasePath, ;
STRING @UserName, ;
INTEGER nLevel, ;
INTEGER @BufferPointer, ;
INTEGER PreferedMaxLength, ;
INTEGER @EntriesRead, ;
INTEGER @TotalEntries, ;
INTEGER @ResumeHandle

IF " NT " $ OS()
*-- NetApiBufferFree is not defined on Windows 9x
DECLARE INTEGER NetApiBufferFree IN NETAPI32 ;
INTEGER Pointer
ENDIF

*-- Instantiate the Pointers control.
SET CLASSLIB TO POINTERS ADDITIVE
loPointersObject = CREATEOBJECT("Pointers")
RELEASE CLASSLIB POINTERS

*-- The server name, the base path and the user name
* must be in Unicode format.
lcServerName = STRCONV(STRCONV(tcServerName + CHR(0), 1), 5)
lcBasePath = STRCONV(STRCONV(tcBasePath + CHR(0), 1), 5)
lcUserName = STRCONV(STRCONV(tcUserName + CHR(0), 1), 5)

lnPreferedMaxLength = 100000000

lnResumeHandle = 0
lnEntriesRead = 0
lnTotalEntries = 0
lnBufferPointer = 0
lnElements = 0

llContinue = .t.
DO WHILE llContinue
lnError = NetFileEnum( ;
lcServerName, ;
lcBasePath, ;
lcUserName, ;
3, ;
@lnBufferPointer, ;
lnPreferedMaxLength, ;
@lnEntriesRead, ;
@lnTotalEntries, ;
@lnResumeHandle)
IF lnEntriesRead = 0
llContinue = .F.
EXIT
ENDIF
IF lnError = 0
FOR lnI = 1 TO lnEntriesRead
IF lnI == 1
PUBLIC &tcArrayName.
ENDIF
DIMENSION &tcArrayName[ lnI, 5 ]

lnpFileInfo = lnBufferPointer + (lnI - 1) * 20

*-- Extract the file id.
&tcArrayName[ lnI, 1] = loPointersObject.Converter.DWORDStringToNumber( ;
loPointersObject.GetMemory(lnpFileInfo, 4))

*-- Extract the permissions on this file
&tcArrayName[ lnI, 2] = GetFilePermissions( loPointersObject.Converter.DWORDStringToNumber( ;
loPointersObject.GetMemory(lnpFileInfo + 4, 4)), ";" )

*-- Extract the number of locks on this file.
&tcArrayName[ lnI, 3] = loPointersObject.Converter.DWORDStringToNumber( ;
loPointersObject.GetMemory(lnpFileInfo + 8, 4))

*-- Extract the file name.
lnpFileName = loPointersObject.Converter.DWORDStringToNumber( ;
loPointersObject.GetMemory(lnpFileInfo + 12, 4))
&tcArrayName[ lnI, 4] = loPointersObject.GetMemory(lnpFileName, "U")

*-- Extract the user name that opened the file.
lnpUserName = loPointersObject.Converter.DWORDStringToNumber( ;
loPointersObject.GetMemory(lnpFileInfo + 16, 4))
&tcArrayName[ lnI, 5] = loPointersObject.GetMemory(lnpUserName, "U")

ENDFOR

*-- Free the memory allocated by NetFileEnum
IF (lnBufferPointer <> 0) AND ;
(" NT " $ OS())
NetApiBufferFree(lnBufferPointer)
ENDIF
ELSE
llContinue = .f.
ENDIF
ENDDO

RETURN( lnError )

*********************************
FUNCTION GetFilePermissions( lnPermissions, m.lcDelimiter )
*********************************
LOCAL lnPermElems, lcRetVal

lcRetVal = ""
lnPermElems = 0

IF BitTest(lnPermissions, 0)
lnPermElems = lnPermElems + 1
DIMENSION aPermissions[lnPermElems]
aPermissions[lnPermElems] = "Read"
ENDIF

IF BitTest(lnPermissions, 1)
lnPermElems = lnPermElems + 1
DIMENSION aPermissions[lnPermElems]
aPermissions[lnPermElems] = "Write"
ENDIF

IF BitTest(lnPermissions, 2)
lnPermElems = lnPermElems + 1
DIMENSION aPermissions[lnPermElems]
aPermissions[lnPermElems] = "Create"
ENDIF

IF BitTest(lnPermissions, 3)
lnPermElems = lnPermElems + 1
DIMENSION aPermissions[lnPermElems]
aPermissions[lnPermElems] = "Execute"
ENDIF

IF BitTest(lnPermissions, 4)
lnPermElems = lnPermElems + 1
DIMENSION aPermissions[lnPermElems]
aPermissions[lnPermElems] = "Delete"
ENDIF

IF BitTest(lnPermissions, 5)
lnPermElems = lnPermElems + 1
DIMENSION aPermissions[lnPermElems]
aPermissions[lnPermElems] = "ChangeAttributes"
ENDIF

IF BitTest(lnPermissions, 6)
lnPermElems = lnPermElems + 1
DIMENSION aPermissions[lnPermElems]
aPermissions[lnPermElems] = "ChangeACL"
ENDIF

IF lnPermElems > 0
lcRetVal = SPACE(128)
ArrayToList( @aPermissions, @lcRetVal, m.lcDelimiter )
ENDIF

RETURN( lcRetVal )

*********************************
FUNCTION ArrayToList( aArray, m.lcList, m.lcDelimiter )
*********************************
* This function will take an array and load its contents into
* a string (m.lcList) delimited with the delimiter specified
* in (m.lcDelimiter). The m.lcList needs to be passed by
* reference. Return value is the number of elements processed
* ie: m.lcString = SPACE(256)
* m.lnEntries = ArrayToList( aMyArray, @lcString, "," )
LOCAL m.lnElements, x

m.lnElements = ALEN( aArray, 1 )
m.lcList = ""

FOR x = 1 TO m.lnElements
m.lcList = m.lcList + IIF( !EMPTY( m.lcList ), m.lcDelimiter, "" ) + ALLTRIM( aArray[x] )
ENDFOR

RETURN( m.lnElements )

*********************************
FUNCTION ListToArray( m.lcList, aArray, m.lcDelimiter )
*********************************
* This function takes a list string delimited by m.lcDelimiter and
* creates an array aArray of the values. Return value is the number
* of elements in the final array.
* ie: m.lnEntries = ListToArray( "1,2,3,4,5", "aMyArray", "," )
LOCAL m.lnDelimCount, x, m.lnStartPos, m.lnDelimPos,;
m.lnElements, m.lnEndPos, m.lnWidth

m.lnElements = 0
m.lnDelimCount = OCCURS( m.lcDelimiter, m.lcList )

IF !EMPTY( m.lcList )
PUBLIC &aArray
IF m.lnDelimCount == 0
m.lnElements = m.lnElements + 1
DIMENSION &aArray.[1,1]
&aArray.[1,1] = ALLTRIM(m.lcList)
ELSE
m.lnStartPos = 1
DIMENSION &aArray.[lnDelimCount+1,1]
FOR x = 1 TO m.lnDelimCount
m.lnElements = m.lnElements + 1
m.lnDelimPos = AT( m.lcDelimiter, m.lcList, x )
m.lnEndPos = IIF( m.lnDelimPos == 0, LEN( m.lcList ) + 1, m.lnDelimPos )
m.lnWidth = ( m.lnEndPos - m.lnStartPos )

m.lcValue = ALLTRIM(SUBSTR( m.lcList, m.lnStartPos, m.lnWidth ))
&aArray.[lnElements,1]=lcValue
m.lnStartPos = m.lnDelimPos + 1
ENDFOR
m.lnElements = m.lnElements + 1
&aArray.[lnElements,1]=ALLTRIM(SUBSTR( m.lcList, m.lnStartPos ))
ENDIF
ENDIF

RETURN( m.lnElements )
Juan L. Romero
gcandela@javanet.com
Previous
Reply
Map
View

Click here to load this message in the networking platform