#include <windows.h>
#include <lm.h>
#include <stdio.h>
#include <stdlib.h>
#pragma hdrstop



#define MAXLEN 256



int main( int argc, char *argv[] )
{
	int i;
	DWORD rc, varlen;
	wchar_t alert[MAXLEN], service[MAXLEN];
	byte buf[4096]; // staging area for the ADMIN_OTHER_INFO struct
	wchar_t *pstr;
	ADMIN_OTHER_INFO *paoi = (ADMIN_OTHER_INFO *) buf;

	if ( argc < 4 )
	{
		puts( "\nusage: nare alert-name service-name errcode [string ...]\n" );
		puts( "alert-name: \"ADMIN\" or \"USER\"; others require different" );
		puts( "foo_OTHER_INFO structs in the source code!\n" );
		puts( "service-name: e.g., \"RPCLOCATOR\" or \"NETPOPUP\"" );
		puts( "errcode: an integer error number (-1 sends to all possible recipients)" );
		puts( "string: up to ten additional strings\n" );
		puts( "Try these:" );
		puts( "    nare ADMIN MYSVC -1 alert hoo-ah" );
		puts( "    nare ADMIN MYSVC 2203" );
		return 1;
	}

	mbstowcs( alert, argv[1], MAXLEN );
	mbstowcs( service, argv[2], MAXLEN );

	paoi->alrtad_errcode = atoi( argv[3] );
	paoi->alrtad_numstrings = argc - 4; // other args are strings

	if ( paoi->alrtad_numstrings > 9 )
	{
		printf( "Sorry, but %d strings is too much. Max is 9.", paoi->alrtad_numstrings );
		return 1;
	}

	// start putting strings right after the fixed struct
	pstr = (wchar_t *) ( buf + sizeof ADMIN_OTHER_INFO );
	for ( i = 4; i < argc; ++ i )
	{
		mbstowcs( pstr, argv[i], buf + sizeof buf - (byte *) pstr );
		pstr += wcslen( pstr ) + 1; // string length plus \0 word
	}
	
	varlen = (byte *) pstr - &buf[0]; // total byte count incl fixed struct

	rc = NetAlertRaiseEx( alert, buf, varlen, service );

	if ( rc != NERR_Success )
		printf( "NARE() returned %lu\n", rc );

	return 0;
}
