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;
}
}
}