#include <windows.h>
#include <lm.h>
#include <stdio.h>
#include <stdlib.h>
#pragma hdrstop



#define MAXLEN 256



int main( int argc, char *argv[] )
{
	CONNECTION_INFO_1 *buf, *cur;
	DWORD read, total, resumeh, rc, i;
	wchar_t server[MAXLEN], client[MAXLEN];

	if ( argc != 3 )
	{
		puts( "usage: nconne \\\\server {\\\\client | sharename}" );
		puts( "Note: the second argument must have \\\\ if it's a client computer name." );
		puts( "If not, it will be taken to be a sharename." );
		return 1;
	}

	mbstowcs( client, argv[2], MAXLEN );
	mbstowcs( server, argv[1], MAXLEN );

	resumeh = 0;
	do
	{
		buf = NULL;
		rc = NetConnectionEnum( (char *) server, (char *) client, 1,
			(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 );
		printf( "%9.9s %-6.6s %4s %4s %-20.20s %.20s\n",
			"id ", "type", "#opn", "#usr", "user name", "share or client name" );
		printf( "%9.9s %-6.6s %4s %4s %-20.20s %.20s\n",
			"---------", "------", "----", "----",
			"--------------------", "--------------------" );
		for ( i = 0, cur = buf; i < read; ++ i, ++ cur )
		{
			const char *type;
			switch ( cur->coni1_type )
			{
				case STYPE_DISKTREE:	type = "disk";   break;
				case STYPE_PRINTQ:		type = "printq"; break;
				case STYPE_DEVICE:		type = "device"; break;
				case STYPE_IPC:			type = "ipc";    break;
				default:				type = "unknwn"; break;
			}

			// Note: the capital S in the format string will expect Unicode
			// strings, as this is a program written/compiled for ANSI.
			printf( "%08lxh %-6.6s %4lu %4lu %-20.20S %.20S\n",
				cur->coni1_id, type, cur->coni1_num_opens, cur->coni1_num_users,
				cur->coni1_username, cur->coni1_netname );
		}

		if ( buf != NULL )
			NetApiBufferFree( buf );

	} while ( rc == ERROR_MORE_DATA );

	if ( rc != ERROR_SUCCESS )
		printf( "NCE() returned %lu\n", rc );

	return 0;
}
