#include <windows.h>
#include <lm.h>
#include <stdio.h>
#include <stdlib.h>
#pragma hdrstop

#pragma comment( lib, "netapi32.lib" )



#define MAXLEN 256
#define BUFSIZE 48 // for normal use, I recommend >= 2048



int main( int argc, char *argv[] )
{
	LOCALGROUP_MEMBERS_INFO_3 *buf, *cur;
	DWORD read, total, resumeh, rc, i;
	wchar_t server[MAXLEN], group[MAXLEN];

	if ( argc != 3 )
	{
		puts( "usage: nlggm \\\\server localgroupname" );
		return 1;
	}

	mbstowcs( server, argv[1], MAXLEN );
	mbstowcs( group,  argv[2], MAXLEN );

	resumeh = 0;
	do
	{
		buf = NULL;
		rc = NetLocalGroupGetMembers( server, group, 3, (BYTE **) &buf,
			BUFSIZE, &read, &total, &resumeh );
		if ( rc != ERROR_MORE_DATA && rc != ERROR_SUCCESS )
			break;

		printf( "\n%S: got %lu entries out of %lu remaining\n", group, 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( "%S\n", cur->lgrmi3_domainandname );
		}

		if ( buf != NULL )
			NetApiBufferFree( buf );

	} while ( rc == ERROR_MORE_DATA );

	if ( rc != ERROR_SUCCESS )
		printf( "NLGGM() returned %lu\n", rc );

	return 0;
}
