Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

See Who's Opened A File And Want To Close It ?

Status
Not open for further replies.

lavomar1

Programmer
May 13, 2002
17
0
0
CA
Windows NT 4 have a helpful program "File Manager" (winfile.exe) that shows wich user is currently into a network file.

But in widows 2K,and more, the File Manager is not working properly (clicking "Open By" button did nothing).

So I want to create a little program that show me these file and I would also want to have the possibility to close the file on the server.

Can you tell me wich functions will be useful for this project ???

Thanx !
 
Ok... I find it...

With the "Platform SDK : Network Management" I can use

- NetFileClose
- NetFileEnum
- NetFileGetInfo

:)
 
Here's a sample :

Code:
#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&quot;Wrong number of parameters!\n\n&quot;);
   wprintf(L&quot;Usage : %s Server FileName\n&quot;, argv[0]);
   return;
   } 

sprintf(parServer, &quot;\\\\%s&quot;, 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(&quot;Error: Could not build the string name of the server.\n&quot; );
   return;
   }
if (MultiByteToWideChar( CP_UTF7, 0, parFile, -1, FileSearch, MAX_LEN ) == 0 )
   {
   printf(&quot;Error: Could not build the string name of the file searched.\n&quot; );
   return;
   }
                              // Alocate memory 
Status    = NetApiBufferAllocate(BufSize,    (LPVOID*)&Buffer);
if (Status != NERR_Success)
   {
   wprintf(L&quot;Error: Could not allocate memory for buffer.\n&quot;);
   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&quot;%-25s\n&quot;, 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;
}
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top