Enumeration of Servers

J

JD

I'm looking for a simple way to get a list of my server off of the network.
Primarily I'm looking for my SQLServers. In VB6 I'd use SQLDMO, but I was
looking for a pure .net solution. Anything?????
 
M

Marina

I don't believe there will be a pure .net solution until yukon. Until then,
you will still have to use SQLDMO.
 
Z

Zoury

You can also P/Invoke the NetServerEnum() API.. it's faster and reference
free BUT it runs only on NT platform..

//***
using System;
using System.Runtime.InteropServices;

namespace NetServerEnum.Sample
{
public enum ServerType : uint
{
None = 0,
WorkStation = 0x1,
Server = 0x2,
SQLServer = 0x4,
DomainCtrl = 0x8,
DomainBAKCtrl = 0x10,
TimeSource = 0x20,
AFP = 0x40,
Novell = 0x80,
DomainMember = 0x100,
PrintQServer = 0x200,
DialInServer = 0x400,
XenixServer = 0x800,
ServerUNIX = XenixServer,
NT = 0x1000,
tWFW = 0x2000,
ServerMFPN = 0x4000,
ServerNT = 0x8000,
PotentialBrowser = 0x10000,
BackUpBroswer = 0x20000,
MasterBrowser = 0x40000,
DomainMaster = 0x80000,
ServerOSF = 0x100000,
ServerVMS = 0x200000,
Windows = 0x400000, // Windows95 and above
DFS = 0x800000, // Root of a DFS tree
ClusterNT = 0x1000000, // NT Cluster
TerminalServer = 0x2000000, // Terminal Server
DCE = 0x10000000, // IBM DSS
AlternateXport = 0x20000000, // rtn alternate transport
LocalListOnly = 0x40000000, // rtn local only
DomainEnum = 0x80000000,
All = 0xFFFFFFFF
}

public class NetworkManagement
{
private const Int32 MAX_PREFERRED_LENGTH = -1;
private const Int32 NERR_SUCCESS = 0;
private const Int32 ERROR_MORE_DATA = 234;

[StructLayout(LayoutKind.Sequential, CharSet=CharSet.Auto)]
private struct SERVER_INFO_101
{
public Int32 dwPlatformID;
public string lpszServerName;
public Int32 dwVersionMajor;
public Int32 dwVersionMinor;
public Int32 dwType;
public string lpszComment;
}
[DllImport("netapi32.dll", SetLastError=true)]
private static extern void NetApiBufferFree
(
IntPtr lpBuffer
);
[DllImport("netapi32.dll", CharSet=CharSet.Auto, SetLastError=true)]
private static extern Int32 NetServerEnum
(
string servername,
Int32 level,
ref IntPtr bufptr,
Int32 prefmaxlen ,
ref Int32 entriesread,
ref Int32 totalentries,
ServerType servertype,
string domain,
ref Int32 resume_handle
);

public static string[] GetServerList(ServerType serverType)
{

IntPtr lpBuffer = IntPtr.Zero;
IntPtr lpTmpBuffer = IntPtr.Zero;
Int32 dwEntriesRead = 0;
Int32 dwTotalEntries = 0;
Int32 dwResumeHandle = 0;
SERVER_INFO_101 se101 = new SERVER_INFO_101();
Int32 nStructSize = 0;
string[] servers = null;

nStructSize = Marshal.SizeOf(se101);
Int32 iRet = NetServerEnum(null,
101,
ref lpBuffer,
MAX_PREFERRED_LENGTH,
ref dwEntriesRead,
ref dwTotalEntries,
serverType,
null,
ref dwResumeHandle);

if ((iRet == NERR_SUCCESS) && (iRet != ERROR_MORE_DATA))
{
servers = (string[])Array.CreateInstance(typeof(string), dwEntriesRead);
lpTmpBuffer = lpBuffer;
for (Int32 i = 0; i < dwEntriesRead; i++)
{
se101 = (SERVER_INFO_101)Marshal.PtrToStructure(lpTmpBuffer,
se101.GetType());
servers = se101.lpszServerName;
lpTmpBuffer = new IntPtr(lpTmpBuffer.ToInt32() + nStructSize);
}
}
if (!lpBuffer.Equals(IntPtr.Zero))
NetApiBufferFree(lpBuffer);

return servers;
}
}
}
//***
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Top