#include <windows.h>
#include <lm.h>
#include <stdio.h>
#include <stdlib.h>
#pragma hdrstop



#pragma comment( lib, "netapi32.lib" )



#define MAXLEN 256



int main( int argc, char *argv[] )
{
	SHARE_INFO_2 *buf, *cur;
	DWORD read, total, resumeh, rc, i;
	wchar_t server[MAXLEN];
	char tempstr[20], *type;
	static char *types[] = { "disk", "printq", "device", "ipc" };

	if ( argc > 2 )
	{
		puts( "usage: nshe [\\\\server]" );
		return 1;
	}

	if ( argc >= 2 )
		mbstowcs( server, argv[1], MAXLEN );
	else
		server[0] = L'\0';

	resumeh = 0;
	do
	{
		buf = NULL;
		rc = NetShareEnum( argc < 2? NULL: server,
			2, (BYTE **) &buf, 8192, &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.

			if ( ( cur->shi2_type & ~STYPE_SPECIAL ) >= 0 &&
				( cur->shi2_type & ~STYPE_SPECIAL ) <= STYPE_IPC )
				type = types[( cur->shi2_type & ~STYPE_SPECIAL )];
			else
				sprintf( type = tempstr, "%08lx", cur->shi2_type );

			printf( "%-20.20S     %-8.8s%c     %-.40S\n",
				cur->shi2_netname, type, ( cur->shi2_type & STYPE_SPECIAL )? '*': ' ',
				cur->shi2_path );
		}

		if ( buf != NULL )
			NetApiBufferFree( buf );

	} while ( rc == ERROR_MORE_DATA );

	if ( rc != ERROR_SUCCESS )
		printf( "NShE() returned %lu\n", rc );

	return 0;
}

