#include <windows.h>
#include <lm.h>
#include <stdio.h>
#include <stdlib.h>
#pragma hdrstop



#define MAXLEN 256



int main( int argc, char *argv[] )
{
	SESSION_INFO_502 *buf, *cur;
	DWORD read, total, resumeh, rc, i;
	wchar_t server[MAXLEN], client[MAXLEN], user[MAXLEN];

	if ( argc < 2 || argc > 4 )
	{
		puts( "usage: nsesse \\\\server [\\\\client [user]]" );
		return 1;
	}

	if ( argc >= 4 )
		mbstowcs( user, argv[3], MAXLEN );
	else
		user[0] = L'\0';

	if ( argc >= 3 )
		mbstowcs( client, argv[2], MAXLEN );
	else
		client[0] = L'\0';

	if ( argc >= 2 )
		mbstowcs( server, argv[1], MAXLEN );
	else
		server[0] = L'\0';

	resumeh = 0;
	do
	{
		buf = NULL;
		rc = NetSessionEnum( (char *) server, (char *) client, (char *) user,
			502, (BYTE **) &buf, 2048, &read, &total, &resumeh );
		if ( rc != ERROR_MORE_DATA && rc != ERROR_SUCCESS )
			break;

		printf( "\ngot %lu entries out of %lu\n", read, total );
		for ( i = 0, cur = buf; i < read; ++ i, ++ cur )
		{
			// Note: the capital S in the format string will expect Unicode
			// strings, as this is a program written/compiled for ANSI.
			printf( "%-20.20S     %-20.20S     %-.20S\n",
				cur->sesi502_cname, cur->sesi502_username, cur->sesi502_transport );
		}

		if ( buf != NULL )
			NetApiBufferFree( buf );

	} while ( rc == ERROR_MORE_DATA );

	if ( rc != ERROR_SUCCESS )
		printf( "NSE() returned %lu\n", rc );

	return 0;
}

