Click here to Skip to main content
15,885,757 members
Articles / Programming Languages / C#

A C# Server Drop-down Control

Rate me:
Please Sign up or sign in to vote.
4.13/5 (5 votes)
29 May 2002 138.4K   2.2K   44  
A C# usercontrol that enumerates servers and displays the list in a drop-down list
using System;
using System.Runtime.InteropServices;

namespace ServerDropDown
{
	public enum ServerTypeEnum : uint
	{
		None					= 0,
		ALL						= 0xFFFFFFFF,
		WORKSTATION          	= 0x00000001,
		SERVER               	= 0x00000002,
		SQLSERVER            	= 0x00000004,
		DOMAIN_CTRL          	= 0x00000008,
		DOMAIN_BAKCTRL       	= 0x00000010,
		TIME_SOURCE          	= 0x00000020,
		AFP                  	= 0x00000040,
		NOVELL               	= 0x00000080,
		DOMAIN_MEMBER        	= 0x00000100,
		PRINTQ_SERVER        	= 0x00000200,
		DIALIN_SERVER        	= 0x00000400,
		XENIX_SERVER         	= 0x00000800,
		NT                   	= 0x00001000,
		WFW                  	= 0x00002000,
		SERVER_MFPN          	= 0x00004000,
		SERVER_NT            	= 0x00008000,
		POTENTIAL_BROWSER    	= 0x00010000,
		BACKUP_BROWSER       	= 0x00020000,
		MASTER_BROWSER       	= 0x00040000,
		DOMAIN_MASTER        	= 0x00080000,
		SERVER_OSF           	= 0x00100000,
		SERVER_VMS           	= 0x00200000,
		WINDOWS              	= 0x00400000,  /* Windows95 and above */
		DFS                  	= 0x00800000,  /* Root of a DFS tree */
		CLUSTER_NT           	= 0x01000000,  /* NT Cluster */
		DCE                  	= 0x10000000,  /* IBM DSS (Directory and Security Services) or equivalent */
		ALTERNATE_XPORT      	= 0x20000000,  /* return list for alternate transport */
		LOCAL_LIST_ONLY      	= 0x40000000,  /* Return local list only */
		DOMAIN_ENUM          	= 0x80000000
	}

	public class ServerList
	{
		[DllImport("kernel32.dll")]
		private static extern uint CopyMemory ([MarshalAs(UnmanagedType.AsAny)] object Destination, 
			[MarshalAs(UnmanagedType.AsAny)] object Source,
			int Length);

		[DllImport("netapi32.dll")]
		unsafe private static extern uint NetServerEnum([MarshalAs(UnmanagedType.LPWStr)] string ServerName, 
			uint level,
			uint* bufptr,
			uint prefmaxlen,
			ref uint entriesread,
			ref uint totalentries,
			uint servertype,
			[MarshalAs(UnmanagedType.LPWStr)] string domain, 
			uint resume_handle);

		[System.Runtime.InteropServices.StructLayoutAttribute (LayoutKind.Sequential, CharSet=CharSet.Auto)]
		private struct SERVER_INFO_101 
		{ 
			public int dwPlatformID; 
			public System.IntPtr lpszServerName;
			public int dwVersionMajor; 
			public int dwVersionMinor; 
			public int dwType; 
			public int lpszComment;
		}

		private static ServerTypeEnum _serverType;

		public ServerList()
		{
		}

		public static ServerTypeEnum ServerType
		{
			get
			{
				return _serverType;
			}
			set
			{
				_serverType = value;
			}
		}

		public static string[] GetList(string domain)
		{
			string[] servers;
			string serverName = null;
			string domainName = null; 
			uint level = 101, prefmaxlen = 0xFFFFFFFF, entriesread = 0, totalentries = 0; 
			uint resume_handle = 0;
			
			try
			{
				unsafe
				{	
					//get a pointer to the server info structure
					SERVER_INFO_101* si = null;
					SERVER_INFO_101* pTmp;	//temp pointer for use when looping through returned (pointer) array

					//this api requires a pointer to a byte array...which is actually a pointer to an array
					//of SERVER_INFO_101 structures
					uint nRes = NetServerEnum(serverName, level, (uint *) &si, prefmaxlen, ref entriesread, ref totalentries, 
						(uint)_serverType, domainName, resume_handle);
			
					servers = new string[entriesread];

					if (nRes == 0)
					{
						if ((pTmp = si) != null)					//assign the temp pointer 
						{
							for (int i = 0; i < entriesread; i++)	//loop through the entries
							{
								try
								{
									servers[i] = Marshal.PtrToStringAuto(pTmp->lpszServerName);
								}
								catch (Exception e)
								{
									string error = e.Message;
								}
								pTmp++;		//increment the pointer...essentially move to the next structure in the array
							}
						}
					}
				}  
			}
			catch (Exception /*e*/)
			{
				return null;
			}

			return servers;
		}
	}
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions