Novell Home

Example of GetConnectionsOpenFiles()

From Developer Community

This code scans all connection numbers on the server and for every connection reads information about its open files with GetConnectionsOpenFiles(). The program calculates the number of open files with TXT extension and prints it before exiting.

Sample Code

#include <nwcntask.h>
#include <nwconn.h>
#include <nwfileng.h>
#include <nit/nwenvrn1.h>
#include <nwthread.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main( void) {
	unsigned int users = 0;
	unsigned int maxNumberOfStations = ( unsigned int) GetMaximumNumberOfStations();
	unsigned int i;
	for ( i = 1; i < maxNumberOfStations; i++) {
		int lastRecord = 0;
		int lastTask = 0;
		CONN_OPEN_FILES openFiles;
		char buffer[ ENVSERV_BUFFER1_SIZE];
		if ( GetConnectionsOpenFiles(( WORD) i, &lastRecord, &lastTask, sizeof( openFiles), &openFiles, buffer, sizeof( buffer)) == 0) {
			while ( lastRecord || lastTask) {
				unsigned char path[ 1024];
				unsigned long path_count;
				int ccode;
				/* GetPathFromDirectoryEntry( openFiles.u.con386.volumeNumber, ( unsigned short) openFiles.u.con386.parentDirEntry, ( unsigned char*) &path_len, path); */
				ccode = FEMapVolumeNumberToName( openFiles.u.con386.volumeNumber, path);
				if ( ccode == 0) {
					printf( "%.*s:", *path, path + 1);
				}
				else {
					printf( "FEMapVolumeNumberToName failed, ccode = %d\n", ccode);
				}
				ccode = FEMapVolumeAndDirectoryToPathForNS( openFiles.u.con386.volumeNumber, openFiles.u.con386.dirEntry, openFiles.u.con386.nameSpace, path, &path_count);
				if ( ccode == 0) {
					unsigned char* path_ptr = path;
					int j;
					for ( j = 0; j < path_count; ++j) {
						printf( "\\%.*s", *path_ptr, path_ptr + 1);
						path_ptr += *path_ptr + 1;
					}
				}
				else {
					printf( "FEMapVolumeAndDirectoryToPathForNS failed, ccode = %d\n", ccode);
				}
				puts( "");
				if ( strncmp(( char*) openFiles.u.con386.fileName + openFiles.u.con386.nameLength - 3, "TXT", 3) == 0) {
					users++;
					break;
				}
				GetConnectionsOpenFiles(( WORD) i, &lastRecord, &lastTask, sizeof( openFiles), &openFiles, buffer, sizeof( buffer));
			}
		}
		else {
			puts( "GetConnectionsOpenFiles faled");
		}
		ThreadSwitchLowPriority();
	}
	printf( "%u user(s) have *.TXT files open\n", users);
	return EXIT_SUCCESS;
}

--Dmitry Mityugov

Novell® Making IT Work As One

© 2009 Novell, Inc. All Rights Reserved.