Quick Search for:  in language:    
SQL,Enumerate,servers,domain,specific,server,
   Code/Articles » |  Newest/Best » |  Community » |  Jobs » |  Other » |  Goto » | 
CategoriesSearch Newest CodeCoding ContestCode of the DayAsk A ProJobsUpload
.Net Stats

 Code: 140,189. lines
 Jobs: 376. postings

 How to support the site

 
Sponsored by:

 
You are in:
 

Does your code think in ink?
Login





Latest Code Ticker for .Net.
Click here to see a screenshot of this code!Ray Picking for 3d Objects
By Jay1_z on 11/23

(Screen Shot)

Embed Images into Xml Files
By Chris Richards on 11/23


Encryption and Alternate Data Stream
By Philip Pierce on 11/23


Click here to see a screenshot of this code!AddressBook
By Ekong George Ekong on 11/23

(Screen Shot)

Click here to see a screenshot of this code!Command Line Redirection
By kaze on 11/23

(Screen Shot)

Fader
By Ahmad Hammad on 11/23


Click here to see a screenshot of this code!Get content of a web page (simple)
By Tin Trung Dang on 11/22

(Screen Shot)

Retrieve the long path name for a short path name.
By Özgür Aytekin on 11/22


Easy Randomizer
By Christian Müller on 11/22


Click here to put this ticker on your site!


Add this ticker to your desktop!


Daily Code Email
To join the 'Code of the Day' Mailing List click here!

Affiliate Sites



 
 
   

NetServerEnum

Print
Email
 

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
Use this form to notify us if this entry should be deleted (i.e contains no code, is a virus, etc.).
Reason:
 
Your Vote!

What do you think of this article(in the Intermediate category)?
(The article with your highest vote will win this month's coding contest!)
Excellent  Good  Average  Below Average  Poor 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
Keep the Planet clean! If this comment was disrespectful, please report it:
Reason:

 
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.
Keep the Planet clean! If this comment was disrespectful, please report it:
Reason:

 
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!
Keep the Planet clean! If this comment was disrespectful, please report it:
Reason:

 
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.
Keep the Planet clean! If this comment was disrespectful, please report it:
Reason:

 
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!
Keep the Planet clean! If this comment was disrespectful, please report it:
Reason:

 
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
Keep the Planet clean! If this comment was disrespectful, please report it:
Reason:

 
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!
Keep the Planet clean! If this comment was disrespectful, please report it:
Reason:

 
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.
 
Name:
Comment:

 

Categories | Articles and Tutorials | Advanced Search | Recommended Reading | Upload | Newest Code | Code of the Month | Code of the Day | All Time Hall of Fame | Coding Contest | Search for a job | Post a Job | Ask a Pro Discussion Forum | Live Chat | Feedback | Customize | .Net Home | Site Home | Other Sites | About the Site | Feedback | Link to the Site | Awards | Advertising | Privacy

Copyright© 1997 by Exhedra Solutions, Inc. All Rights Reserved.  By using this site you agree to its Terms and Conditions.  Planet Source Code (tm) and the phrase "Dream It. Code It" (tm) are trademarks of Exhedra Solutions, Inc.