|
| | Submitted on: 11/19/2002 10:29:27 PM
By: Eugene Zhukovsky
Level: Intermediate User Rating:
By 3 Users Compatibility:C#
Users have accessed this article 6710 times. |
|
| | Enumerate all servers in the domain, or specific server types, like SQL Servers, Workstations etc with API.
Using netapi32.dll | | | Terms of Agreement:
By using this article, you agree to the following terms...
1) You may use
this article in your own programs (and may compile it into a program and distribute it in compiled format for languages that allow it) freely and with no charge.
2) You MAY NOT redistribute this article (for example to a web site) without written permission from the original author. Failure to do so is a violation of copyright laws.
3) You may link to this article from another website, but ONLY if it is not wrapped in a frame.
4) You will abide by any additional copyright restrictions which the author may have placed in the article or article's description. |
//declare the DLL import functions [DllImport("netapi32
//Coded by Eugene E. Zhukovsky, 8.21.2002
//declare the DLL import functions
[DllImport("netapi32.dll",EntryPoint="NetServerEnum")]
public static extern int NetServerEnum( [MarshalAs(UnmanagedType.LPWStr)]string servername,
int level,
out IntPtr bufptr,
int prefmaxlen,
ref int entriesread,
ref int totalentries,
SV_101_TYPES servertype,
[MarshalAs(UnmanagedType.LPWStr)] string domain,
int resume_handle);
[DllImport("netapi32.dll",EntryPoint="NetApiBufferFree")]
public static extern int NetApiBufferFree(IntPtr buffer);
//declare the structures to hold info
public enum SV_101_TYPES:uint
{
SV_TYPE_WORKSTATION= 0x00000001,
SV_TYPE_SERVER= 0x00000002,
SV_TYPE_SQLSERVER = 0x00000004,
SV_TYPE_DOMAIN_CTRL= 0x00000008,
SV_TYPE_DOMAIN_BAKCTRL= 0x00000010,
SV_TYPE_TIME_SOURCE= 0x00000020,
SV_TYPE_AFP= 0x00000040,
SV_TYPE_NOVELL= 0x00000080,
SV_TYPE_DOMAIN_MEMBER = 0x00000100,
SV_TYPE_PRINTQ_SERVER = 0x00000200,
SV_TYPE_DIALIN_SERVER = 0x00000400,
SV_TYPE_XENIX_SERVER = 0x00000800,
SV_TYPE_SERVER_UNIX= SV_TYPE_XENIX_SERVER,
SV_TYPE_NT= 0x00001000,
SV_TYPE_WFW= 0x00002000,
SV_TYPE_SERVER_MFPN= 0x00004000,
SV_TYPE_SERVER_NT = 0x00008000,
SV_TYPE_POTENTIAL_BROWSER = 0x00010000,
SV_TYPE_BACKUP_BROWSER= 0x00020000,
SV_TYPE_MASTER_BROWSER= 0x00040000,
SV_TYPE_DOMAIN_MASTER = 0x00080000,
SV_TYPE_SERVER_OSF= 0x00100000,
SV_TYPE_SERVER_VMS= 0x00200000,
SV_TYPE_WINDOWS= 0x00400000, SV_TYPE_DFS= 0x00800000, SV_TYPE_CLUSTER_NT= 0x01000000, SV_TYPE_TERMINALSERVER= 0x02000000, SV_TYPE_CLUSTER_VS_NT = 0x04000000, SV_TYPE_DCE= 0x10000000, SV_TYPE_ALTERNATE_XPORT= 0x20000000, SV_TYPE_LOCAL_LIST_ONLY= 0x40000000, SV_TYPE_DOMAIN_ENUM= 0x80000000,
SV_TYPE_ALL= 0xFFFFFFFF
}
[StructLayout(LayoutKind.Sequential)]
public struct SERVER_INFO_101
{
[MarshalAs(System.Runtime.InteropServices.UnmanagedType.U4)] public UInt32 sv101_platform_id;
[MarshalAs(System.Runtime.InteropServices.UnmanagedType.LPWStr)] public stringsv101_name;
[MarshalAs(System.Runtime.InteropServices.UnmanagedType.U4)] public UInt32 sv101_version_major;
[MarshalAs(System.Runtime.InteropServices.UnmanagedType.U4)] public UInt32 sv101_version_minor;
[MarshalAs(System.Runtime.InteropServices.UnmanagedType.U4)] public UInt32 sv101_type;
[MarshalAs(System.Runtime.InteropServices.UnmanagedType.LPWStr)] public stringsv101_comment;
}
public enum PLATFORM_ID
{
PLATFORM_ID_DOS = 300,
PLATFORM_ID_OS2 = 400,
PLATFORM_ID_NT = 500,
PLATFORM_ID_OSF = 600,
PLATFORM_ID_VMS = 700
}
//now let's do it!
public static void doit()
{
SERVER_INFO_101 si;
IntPtr ppSVINFO = new IntPtr();
int etriesread = 0;
int totalentries = 0;
try
{
if(NetServerEnum(null,
101,
out ppSVINFO,
-1,
ref etriesread,
ref totalentries,
SV_101_TYPES.SV_TYPE_ALL,
null,
0) == 0 )
{
Int32 ptr = ppSVINFO.ToInt32();
for(int i = 0; i < etriesread; i++)
{
si = (SERVER_INFO_101)Marshal.PtrToStructure( new IntPtr(ptr), typeof(SERVER_INFO_101) );
ptr += Marshal.SizeOf( si );
}
}
NetApiBufferFree(ppSVINFO);
}
catch(Exception e)
{
}
}
//here's some possible error codes
public enum NERR
{
NERR_Success = 0, /* Success */
ERROR_MORE_DATA = 234, // dderror
ERROR_NO_BROWSER_SERVERS_FOUND = 6118,
/// <summary>
/// The system call level is not correct.
/// </summary>
ERROR_INVALID_LEVEL = 124,
/// <summary>
/// Access is denied.
/// </summary>
ERROR_ACCESS_DENIED = 5,
/// <summary>
/// The parameter is incorrect.
/// </summary>
ERROR_INVALID_PARAMETER = 87,
/// <summary>
/// Not enough storage to process this command.
/// </summary>
ERROR_NOT_ENOUGH_MEMORY = 8,
/// <summary>
/// The network is busy.
/// </summary>
ERROR_NETWORK_BUSY = 54,
/// <summary>
/// The network path was not found.
/// </summary>
ERROR_BAD_NETPATH = 53,
/// <summary>
/// The network is not present or not started.
/// </summary>
ERROR_NO_NETWORK= 1222,
/// <summary>
/// Handle is in an invalid state.
/// </summary>
ERROR_INVALID_HANDLE_STATE= 1609,
/// <summary>
/// An extended error has occurred.
/// </summary>
ERROR_EXTENDED_ERROR = 1208
}
| | Other 8 submission(s) by this author
| | | Report Bad Submission | | | Your Vote! |
See Voting Log | | Other User Comments | 1/13/2003 7:42:11 AM: Hi,
I was struggling how to use this
SDK function in C# and this was really
helpful and this is what I was exactly
looking for.
ThankU very much for your
help
| 1/13/2003 7:44:25 AM: This is what I was exactly looking for.
I was struggling to implement this SDK
function in one of my applications.
Thanx for all the help.
| 7/28/2003 11:04:43 AM:Carl Mercier Great code! Thank you!
But can you
explain how to use it? I haven't been
able to figure it out yet... so far, I
have:
Dim servers As New
ServerEnum
servers.doit()
Thanks again!
| 7/28/2003 11:24:37 AM:Eugene Zhukovsky That's all you need. In the for loop,
the si structure will tell you all you
need to know about the enumerated
resources.
| 8/7/2003 11:31:11 PM:Carl Mercier Eugene, Can you post a sample of how
to use your code? I still can't figure
it out. Thanks!
| 8/8/2003 4:21:52 PM:Eugene Zhukovsky After the line
si =
(SERVER_INFO_101)Marshal.PtrToStructure(
new IntPtr(ptr),
typeof(SERVER_INFO_101) );
you can get
the information, like so:
int
iPlatform = si.sv101_platform_id;
HTH
| 9/18/2003 4:35:31 PM: Eugene, I wonder if you could help a VB
beginner here. Im not sure how to
implement this code in VB. Do I copy
this file to a C# code file (or attempt
to create a C# dll)? Any help would
be greatly appreciated!
| | Add Your Feedback! | Note:Not only will your feedback be posted, but an email will be sent to the code's author in your name.
NOTICE: The author of this article has been kind enough to share it with you. If you have a criticism, please state it politely or it will be deleted.
For feedback not related to this particular article, please click here. | | |