Files Delphi

Title: NetFileEnum
Question: How can I get a list of open network files on a given Server?
Answer:
I needed a function that gives me a list of open files on my apps & files server so I searched for any kind of code. Finally I found some code on a post. I used it to create this unit.
UNIT uNetFileEnum;
INTERFACE
USES
Classes, SysUtils,
Windows;
CONST
SUCCESS = 0; // Success
NERR_Success = 0; // Success
NERR_DLLError = -1001; // Error loading DLL
NERR_Unknow = -1000; // Undefined Error
MAX_PREFERRED_LENGTH = DWORD(-1);
TYPE
NET_API_STATUS = DWORD;
PFILE_INFO_2 = ^FILE_INFO_2;
FILE_INFO_2 = PACKED RECORD
fi2_id : DWord;
END;
PFILE_INFO_3 = ^FILE_INFO_3;
FILE_INFO_3 = PACKED RECORD
fi3_id : DWord;
fi3_permissions : DWord;
fi3_num_locks : DWord;
fi3_pathname : PWChar;
fi3_username : PWChar;
END;
TNetFileInfo = RECORD
id : Cardinal;
permissions : Cardinal;
num_locks : Cardinal;
pathname : WideString;
username : WideString;
END;
TNetFilesInfo = ARRAY OF TNetFileInfo;
TcsNetFileEnum = FUNCTION(
CONST servername : PWChar;
CONST basepath : PWChar;
CONST username : PWChar;
CONST level : DWord;
CONST bufptr : Pointer;
CONST prefmaxlen : DWord;
CONST entriesread : PDWord;
CONST totalentries : PDWord;
CONST resume_handle : PDWord
) : NET_API_STATUS; stdcall;
TcsNetApiBufferFree = FUNCTION(
CONST Buffer : Pointer
) : NET_API_STATUS; stdcall;
FUNCTION NetFileEnum(ServerName, Basepath,UserName: WideString; Level: Cardinal; VAR Entries: TNetFilesInfo): Integer;
IMPLEMENTATION
FUNCTION NetFileEnum(ServerName, Basepath,UserName: WideString; Level: Cardinal; VAR Entries: TNetFilesInfo): Integer;
VAR
hDLL : DWORD;
w32NetFileEnum : TcsNetFileEnum;
w32NetApiBufferFree : TcsNetApiBufferFree;
pfi3Work : PFILE_INFO_3;
pfi3Save : PFILE_INFO_3;
dwEntriesRead : DWORD;
dwTotalEntries : DWORD;
dwResult : DWORD;
i : integer;
pserver, ppath, pusername: PWChar;
BEGIN
Result:= NERR_Unknow;
w32NetFileEnum:=nil;
w32NetApiBufferFree:=nil;
hDLL := LoadLibrary('NETAPI32.DLL');
TRY
IF hDLL = 0 THEN
exit;
@w32NetFileEnum := GetProcAddress(hDLL,'NetFileEnum');
@w32NetApiBufferFree := GetProcAddress(hDLL,'NetApiBufferFree');
IF (hDLL = 0) or (@w32NetFileEnum = nil) or (@w32NetApiBufferFree = nil) THEN
Result:= NERR_DLLError
ELSE
BEGIN
SetLength(Entries,0);
IF ServerName'' THEN
pserver:=pwchar(ServerName)
ELSE
pserver:=nil;
IF basepath'' THEN
ppath:=pwchar(basepath)
ELSE
ppath:=nil;
IF username'' THEN
pusername:=pwchar(username)
ELSE
pusername:=nil;
dwResult := w32NetFileEnum(pserver,ppath,pusername,3,
@pfi3Work,MAX_PREFERRED_LENGTH,@dwEntriesRead,@dwTotalEntries,nil);
IF dwResult NERR_Success THEN
result:=dwResult
ELSE
TRY
pfi3Save:=pfi3Work;
SetLength(Entries,dwEntriesRead);
FOR i := 1 TO dwEntriesRead DO
BEGIN
WITH Entries[i-1] DO
BEGIN
id := pfi3Save.fi3_id;
permissions := pfi3Save.fi3_permissions;
num_locks := pfi3Save.fi3_num_locks;
pathname := pfi3save.fi3_pathname;
username := pfi3save.fi3_username;
END;
Inc(pfi3Save);
END;
FINALLY
Result:= dwResult;
w32NetApiBufferFree(pfi3Work);
END;
END;
FINALLY
IF hDLL 0 THEN
FreeLibrary(hDLL);
END;
END;
{
// Usage Example
uses
StrMan;
begin
memo1.Lines.Clear;
if GetNetFileEnum('\\SERVER','','',3,RESULT)= Success then
try
for i := Low(result) to High(result) do
with result[i] do
begin
memo1.Lines.Add(Format('%d'+#9+'%s'+#9#9+'[%d]'+#9+'//%d//: %s ',[id, sm.pad(UserName,12),permissions,num_locks,pathname]));
end;
finally
SetLength(result,0);
end;
end;
}
END.