[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

vb 6 or c

Posted on 2009-04-28
1
Medium Priority
?
154 Views
Last Modified: 2013-11-26
Dear
 I need to write a programe in VB6 or in c, which queries the network and shows the Domains/Servers/Workstations structure. It should also shows the users of each Server or Workstation and should be able to send messages to the selected PC.
Any Body their Please Help me in this. I need big help from your side
Thanks and Regards
0
Comment
Question by:Showkatdar
1 Comment
 
LVL 86

Accepted Solution

by:
jkr earned 1500 total points
ID: 24254205
To enumerate servers and their roles, use 'WNetEnumResource()' (http://win32.mvps.org/network/wnoe_wner.html), to enumerate users, there's 'NetUserEnum()' (http://win32.mvps.org/network/nue_nqdi.cpp). Messages can be sent using 'NetMessageBufferSend()' (http://win32.mvps.org/network/nmbs.html). You'll find the code samples below. I'd rather not use VB, since these APIs require structures and pointers, which can become cumbersome in VB.
#include <stdio.h>
#include <windows.h>
#pragma hdrstop
 
 
 
// compile and link with
 
//	cl wnoe_wner.c mpr.lib
 
 
int main( void );
int doEnum( int level, NETRESOURCE *pnr );
void exterr( void );
 
 
 
void exterr( void )
{
	char errbuf[2048], namebuf[2048];
	DWORD err;
 
	errbuf[0] = namebuf[0] = '\0';
	if ( WNetGetLastError( &err, errbuf, sizeof errbuf, namebuf, sizeof namebuf ) == NO_ERROR )
		printf( "Error %lu (\"%s\") reported by \"%s\".\n",
			err, errbuf, namebuf );
	return;
}
 
 
 
int doEnum( int level, NETRESOURCE *pnr )
{
	DWORD rc, rc2;
	HANDLE hEnum;
	DWORD count, bufsize, ui;
	NETRESOURCE buf[200];
	const char *type, *cont;
 
	rc = WNetOpenEnum( RESOURCE_GLOBALNET, RESOURCETYPE_ANY, 0, pnr, &hEnum );
	if ( rc == ERROR_ACCESS_DENIED )
	{
		printf( "%-6.6s %-4.4s%*s  Error 5 -- access denied\n", "", "", level * 2, "" );
		return 1;
	}
 
	if ( rc )
	{
		printf( "WNOE(): rc = %lu, gle = %lu\n", rc, rc2 = GetLastError() );
		if ( rc2 == ERROR_EXTENDED_ERROR )
			exterr();
		return 0;
	}
 
	while ( 1 )
	{
		count = (DWORD) -1L;
		bufsize = sizeof buf;
		rc = WNetEnumResource( hEnum, &count, buf, &bufsize );
		if ( rc != NO_ERROR )
			break;
		for ( ui = 0; ui < count; ++ ui )
		{
			switch ( buf[ui].dwDisplayType )
			{
				case RESOURCEDISPLAYTYPE_DOMAIN:
					type = "domain"; break;
				case RESOURCEDISPLAYTYPE_GENERIC:
					type = "generic"; break;
				case RESOURCEDISPLAYTYPE_SERVER:
					type = "server"; break;
				case RESOURCEDISPLAYTYPE_SHARE:
					type = "share"; break;
				default:
					type = "unknown"; break;
			}
			cont = ( buf[ui].dwUsage & RESOURCEUSAGE_CONTAINER )? "container": "";
			printf( "%-6.6s %-4.4s%*s  %s (%s)\n", type, cont, level * 2, "",
				buf[ui].lpRemoteName, buf[ui].lpProvider );
			// now we recurse if it's a container
			if ( buf[ui].dwUsage & RESOURCEUSAGE_CONTAINER )
				doEnum( level + 1, &buf[ui] );
		}
	}
 
	if ( rc != ERROR_NO_MORE_ITEMS ) // bad things
	{
		printf( "WNER(): rc = %lu, gle = %lu\n", rc, rc2 = GetLastError() );
		if ( rc2 == ERROR_EXTENDED_ERROR )
			exterr();
	}
 
	WNetCloseEnum( hEnum );
	return 1;
}
 
 
 
int main( void )
{
	doEnum( 0, NULL );
 
	return 0;
}
 
--------------------------->8-------------------------------
 
#include <windows.h>
#include <lm.h>
#include <stdio.h>
#pragma hdrstop
 
 
 
// 28jul97 felixk   fixed a nasty bug in do_NetQueryDisplayInformation
//                  (next_index was set even if read == 0: accvio)
 
 
void print_user( const wchar_t *name, DWORD flags )
{
	// NOTE: capital "S" in a printf format string means "Unicode",
	// if we are an ANSI program
 
	printf( "%-40.40S ", name ); // display Unicode user name
	if ( flags & UF_NORMAL_ACCOUNT )
		puts( "(domain user)" );
	else if ( flags & UF_TEMP_DUPLICATE_ACCOUNT )
		puts( "(local user)" );
	else if ( flags & UF_WORKSTATION_TRUST_ACCOUNT )
		puts( "(computer account)" );
	else if ( flags & UF_SERVER_TRUST_ACCOUNT )
		puts( "(PDC/BDC computer account)" );
	else if ( flags & UF_INTERDOMAIN_TRUST_ACCOUNT )
		puts( "(trusted domain account)" );
	else
		puts( "(funny account type)" );
}
 
 
 
void do_NetUserEnum( const wchar_t *server )
{
	USER_INFO_1 *buf, *cur;
	DWORD read, totalremaining, resumeh, rc, i;
 
	resumeh = 0;
	do
	{
		buf = NULL;
		rc = NetUserEnum( server, 1, 0, (BYTE **) &buf, 1024, &read, &totalremaining, &resumeh );
		if ( rc != ERROR_MORE_DATA && rc != ERROR_SUCCESS )
			break;
 
		printf( "\ngot %lu entries out of %lu\n", read, totalremaining );
		for ( i = 0, cur = buf; i < read; ++ i, ++ cur )
			print_user( cur->usri1_name, cur->usri1_flags );
 
		if ( buf != NULL )
			NetApiBufferFree( buf );
 
	} while ( rc == ERROR_MORE_DATA );
 
	if ( rc != ERROR_SUCCESS )
		printf( "NUE() returned %lu\n", rc );
}
 
 
 
void do_NetQueryDisplayInformation( const wchar_t *server, int level )
{
	void *buf;
	NET_DISPLAY_USER *ndu;
	NET_DISPLAY_MACHINE *ndm;
	NET_DISPLAY_GROUP *ndg;
	DWORD read, next_index, rc, i;
 
	next_index = 0;
	do
	{
		buf = NULL;
		rc = NetQueryDisplayInformation( server, level, next_index, 10, 1024, &read, &buf );
		if ( rc != ERROR_MORE_DATA && rc != ERROR_SUCCESS )
			break;
 
		printf( "\ngot %lu entries\n", read );
 
		switch ( level )
		{
			case 1: // users
				for ( i = 0, ndu = (NET_DISPLAY_USER *) buf; i < read; ++ i, ++ ndu )
					print_user( ndu->usri1_name, ndu->usri1_flags );
				// take the last element's next_index
				if ( read > 0 )
					next_index = ((NET_DISPLAY_USER *)buf)[read - 1].usri1_next_index;
				break;
			case 2: // machines
				for ( i = 0, ndm = (NET_DISPLAY_MACHINE *) buf; i < read; ++ i, ++ ndm )
					print_user( ndm->usri2_name, ndm->usri2_flags );
				// take the last element's next_index
				if ( read > 0 )
					next_index = ((NET_DISPLAY_MACHINE *)buf)[read - 1].usri2_next_index;
				break;
			case 3: // groups
				for ( i = 0, ndg = (NET_DISPLAY_GROUP *) buf; i < read; ++ i, ++ ndg )
					printf( "%-40.40S (group)\n", ndg->grpi3_name );
				// take the last element's next_index
				if ( read > 0 )
					next_index = ((NET_DISPLAY_GROUP *)buf)[read - 1].grpi3_next_index;
				break;
		}
 
		if ( buf != NULL )
			NetApiBufferFree( buf );
 
	} while ( rc == ERROR_MORE_DATA );
 
	if ( rc != ERROR_SUCCESS )
		printf( "NQDI() returned %lu\n", rc );
}
 
 
 
int main( void )
{
	// set this to \\your_domain_controller
	wchar_t *servername = L"\\\\BABYA";
 
	// get all accounts
	puts( "\n*** Enumerating accounts: NetUserEnum()" );
	do_NetUserEnum( servername );
 
	// get users
	puts( "\n*** Enumerating users: NetQueryDisplayInformation()" );
	do_NetQueryDisplayInformation( servername, 1 );
 
	// get machine accounts
	puts( "\n*** Enumerating machine accounts: NetQueryDisplayInformation()" );
	do_NetQueryDisplayInformation( servername, 2 );
 
	// get groups
	puts( "\n*** Enumerating groups: NetQueryDisplayInformation()" );
	do_NetQueryDisplayInformation( servername, 3 );
 
	return 0;
}
 
--------------------------->8-------------------------------
 
#include <windows.h>
#include <lm.h>
#include <stdio.h>
#include <stdlib.h>
#pragma hdrstop
#pragma comment( lib, "netapi32.lib" )
 
 
 
#define MAXLEN 256
 
 
 
void enum_names( const wchar_t *server );
 
 
 
int main( int argc, char *argv[] )
{
	DWORD rc;
	wchar_t server[MAXLEN], name[MAXLEN], from[MAXLEN], msg[MAXLEN];
 
	if ( argc != 5 && argc != 2 )
	{
		puts( "\nusage: nmbs \\\\server message-name from-name \"message\"" );
		puts( "       nmbs \\\\server\n" );
		puts( "First form: Sends the message to the <message-name> alias on <server>." );
		puts( "       Use whatever you like as <fromname>." );
		puts( "Second form: Lists <message-name>s on <server>. Note that not all of" );
		puts( "       them may work." );
 
		return 1;
	}
 
	mbstowcs( server, argv[1], MAXLEN );
	server[MAXLEN - 1] = L'\0';
 
	if ( argc == 2 )
	{
		enum_names( server );
	}
	else
	{
		mbstowcs( name, argv[2], MAXLEN );
		name[MAXLEN - 1] = L'\0';
		mbstowcs( from, argv[3], MAXLEN );
		from[MAXLEN - 1] = L'\0';
		mbstowcs( msg, argv[4], MAXLEN );
		msg[MAXLEN - 1] = L'\0';
 
		printf( "\nTrying ... " );
		rc = NetMessageBufferSend( server, name, from, (byte *) &msg[0], wcslen( msg ) * 2 );
 
		if ( rc != NERR_Success )
		{
			printf( "NMBS() returned %lu\n", rc );
			return 1;
		}
 
		puts( "Done." );
	}
 
	return 0;
}
 
 
 
void enum_names( const wchar_t *server )
{
	MSG_INFO_1 *buf, *cur;
	DWORD read, total, resumeh, rc, i;
 
	printf( "\nAvailable message-names on server %S:\n", server );
	resumeh = 0;
	do
	{
		buf = NULL;
		rc = NetMessageNameEnum( server, 1, (BYTE **) &buf,
			512, &read, &total, &resumeh );
		if ( rc != ERROR_MORE_DATA && rc != ERROR_SUCCESS )
			break;
 
		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->msgi1_name );
		}
 
		if ( buf != NULL )
			NetApiBufferFree( buf );
 
	} while ( rc == ERROR_MORE_DATA );
 
	if ( rc != ERROR_SUCCESS )
		printf( "NMNE() returned %lu\n", rc );
}

Open in new window

0

Featured Post

How to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Have you ever wanted to restrict the users input in a textbox to numbers, and while doing that make sure that they can't 'cheat' by pasting in non-numeric text? Of course you can do that with code you write yourself but it's tedious and error-prone …
This article describes some techniques which will make your VBA or Visual Basic Classic code easier to understand and maintain, whether by you, your replacement, or another Experts-Exchange expert.
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
The goal of this video is to provide viewers with basic examples to understand and use conditional statements in the C programming language.
Suggested Courses

868 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question