#define UNICODE
#include <wtypes.h>
#include <stdio.h>
#include <lmcons.h>
#include <lmapibuf.h>
#include <lmshare.h>
#include <lmerr.h>
#include <winnt.h>
#include <string.h>
#define MAX_LEN 256
#define FILE_PER_PAGE 48
int main( int argc, char* argv[] )
{
WCHAR ServerName[MAX_LEN];
WCHAR FileSearch[MAX_LEN];
char parServer[MAX_LEN];
char parFile[MAX_LEN];
FILE_INFO_3* Buffer = NULL;
DWORD BufSize = (sizeof(FILE_INFO_3) * FILE_PER_PAGE)
DWORD EntriesRead = 0;
DWORD EntriesTotal = 0;
DWORD ResumeHndl = 0;
NET_API_STATUS Status;
if (argc < 2 OR argc > 3)
{
wprintf(L"Wrong number of parameters!\n\n");
wprintf(L"Usage : %s Server FileName\n", argv[0]);
return;
}
sprintf(parServer, "\\\\%s", argv[1]);
strcpy(parFile, argv[2]);
// Convert from MULTI_BYTES to WIDE_CHAR.
if (MultiByteToWideChar( CP_UTF7, 0, parServer, -1, ServerName, MAX_LEN ) == 0 )
{
printf("Error: Could not build the string name of the server.\n" );
return;
}
if (MultiByteToWideChar( CP_UTF7, 0, parFile, -1, FileSearch, MAX_LEN ) == 0 )
{
printf("Error: Could not build the string name of the file searched.\n" );
return;
}
// Alocate memory
Status = NetApiBufferAllocate(BufSize, (LPVOID*)&Buffer);
if (Status != NERR_Success)
{
wprintf(L"Error: Could not allocate memory for buffer.\n");
return ;
}
// Get the opened files infos on the specified
// server.
do
{
Status = NetFileEnum(ServerName,
NULL,
NULL,
3,
(LPBYTE*)&Buffer,
BufSize,
&EntriesRead,
&EntriesTotal,
&ResumeHndl);
if (Status != NERR_Success AND Status != ERROR_MORE_DATA)
return Status;
// For each opened file...
for (DWORD E = 0; E < EntriesRead; E++)
{
// check if it's the specified file...
if (wcsstr(Buffer->fi3_pathname, FileSearch) != NULL)
// Show 'Who has file opened'.
wprintf(L"%-25s\n", Buffer->fi3_username);
Buffer++;
} // End For E
} // End do...
while (Status == ERROR_MORE_DATA);
// Free the buffers memory.
if (Buffer != NULL)
NetApiBufferFree((LPVOID)Buffer);
// Everything went OK.
return;
}