Click here to Skip to main content
15,892,809 members
Home / Discussions / C#
   

C#

 
GeneralRe: how can I get a reference type object memory size Pin
fftongzhi29-Dec-02 19:02
fftongzhi29-Dec-02 19:02 
GeneralRe: how can I get a reference type object memory size Pin
leppie30-Dec-02 6:57
leppie30-Dec-02 6:57 
GeneralRe: how can I get a reference type object memory size Pin
grv57526-Dec-02 22:15
grv57526-Dec-02 22:15 
Questionhow to fire or raise an event Pin
Mardawi25-Dec-02 2:31
Mardawi25-Dec-02 2:31 
AnswerRe: how to fire or raise an event Pin
leppie25-Dec-02 6:55
leppie25-Dec-02 6:55 
GeneralMouse events Pin
hecc25-Dec-02 0:30
hecc25-Dec-02 0:30 
GeneralEnumerating Network Resources Pin
mkomasi24-Dec-02 23:42
mkomasi24-Dec-02 23:42 
GeneralRe: Enumerating Network Resources Pin
Rob Graham25-Dec-02 8:00
Rob Graham25-Dec-02 8:00 
The following is incomplete, but the part you need is there. It is based on code provided in a CP article by Leppie (sql server combo box, I think), refactored into a utility class with a few minor corrections.
public enum ServerTypes : 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 or equivalent */
  ALTERNATE_XPORT      	= 0x20000000,  /* return list for alternate transport */
  LOCAL_LIST_ONLY      	= 0x40000000,  /* Return local list only */
  DOMAIN_ENUM          	= 0x80000000   /* Nt domains */
}

public class ServerList
{
/// <summary>
/// NetServerEnum: enumerate "servers" in the local or specified domain
/// </summary>
 [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);
/// <summary>
/// NetApiBufferFree: free buffer internally allocated by NetAPI calls
/// </summary>
[DllImport("netapi32.dll")]
unsafe private static extern uint NetApiBufferFree([MarshalAs(UnmanagedType.AsAny)] object bufptr);
/// <summary>
/// SERVER_INFO_101 structure used by NetAPI calls
/// </summary>
[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;
 }
 public ServerList()
 {
 }

/// <summary>
/// returns a possibly empty string array containting the names of all NT workstations or servers in the
/// specified NT domain, the primary (current node) domain is used if this is empty or null 
/// Rethrows any exceptions that occur during call to NetApi32.dll
/// </summary>
/// <returns>An array of strings (the names)
/// </returns>
 public static string[] GetNtNodeNames(string ntDomainName)
 {
   return (GetServers(ntDomainName, (uint)(ServerTypes.SERVER | ServerTypes.WORKSTATION)));
 }
/// <summary>
///  returns a possibly empty string array containting the names
///  of all SQL Servers in the specified domain
///  Rethrows any exceptions that occur during call to NetApi32.dll
/// </summary>
/// <param name="ntDomainName">Desired domain (null = primary local domain)</param>
/// <returns>An array of strings (the names)</returns>
 public static string[] GetSqlServerNames(string ntDomainName)
 {
  return (GetServers(ntDomainName, (uint)(ServerTypes.SQLSERVER)));
 }
/// <summary>
/// Priveate general function to return arrays of server names by criteria
/// </summary>
/// <param name="ntDomainName">Desired domain (null = primary local domain)</param>
/// <param name="serverTypemask">ServerTypes Enum Mask
///     for server types to include in list</param>
/// <returns>An array of strings (the names)</returns>
        public static string[] GetServers(string ntDomainName, uint serverTypemask)
        {
            string[] servers = new string[0];
            string serverName = null;
            string domainName = null; 
            if (ntDomainName != null)
                if (ntDomainName.Length > 0)
                    domainName = ntDomainName;
            uint level = 101, prefmaxlen = 0xFFFFFFFF, entriesread = 0, totalentries = 0; 
            uint resume_handle = 0;
            uint stat = 0;
   unsafe
   {
                //get a pointer to the server info structure
                IntPtr si = IntPtr.Zero;
                //temp pointer for use when looping through returned (pointer) array

                SERVER_INFO_101* pTmp;	 // all nt type nodes servers and workstations
                uint _serverType = serverTypemask;             
     try
     {


        //this api requires a pointer to a byte array
        //...which is actually a pointer to an array
        //of SERVER_INFO_101 structures
       stat = NetServerEnum(serverName, level, (uint *) &si, prefmaxlen, ref entriesread,
                  ref totalentries, (uint)_serverType, domainName, resume_handle);		
       if (stat == 0)
        {
          // array to receive name strings (possibly zero length)
         servers = new string[entriesread];
          // if netapi returned a valid pointer
           if ((IntPtr)(si) != IntPtr.Zero)			
           {
             // copy source pointer as a SERVER_INFO_101 Structure pointer
              pTmp = (SERVER_INFO_101*)si;
              for (int i = 0; i < entriesread; i++)	//loop through the entries
              {
                  try
                  {
                        // get the string name from the SERVER_INFO_101 structure
                       servers[i] = Marshal.PtrToStringAuto(pTmp->lpszServerName);
                  }
                  catch (Exception e)
                  {
                        //rethrow, let caller handle: will be caught again in 
                        // outer try/catch block so buffer can be released
                       throw e;
                  }
                  pTmp++;//increment the pointer...i.e. move to the next structure in the array
               }
            }
          }
     }
     catch (Exception e)
     {
        // just retrow, let caller handle
          throw e;
     }
     finally
     {
        //tell NetApi to releasethe buffer
        if (si != IntPtr.Zero)
           stat = NetApiBufferFree(si);
     }
      return servers;
   }
 }

}

</small>


Hope this helps & Merry Xmas.
Smile | :)

Some ideas are so stupid that only an intellectual could have thought of them - George Orwell

GeneralRe: Enumerating Network Resources Pin
Rob Graham26-Dec-02 8:57
Rob Graham26-Dec-02 8:57 
Questionwhy are classes sealed? Pin
Marc Clifton24-Dec-02 9:17
mvaMarc Clifton24-Dec-02 9:17 
AnswerRe: why are classes sealed? Pin
Nish Nishant24-Dec-02 9:46
sitebuilderNish Nishant24-Dec-02 9:46 
GeneralRe: why are classes sealed? Pin
Marc Clifton24-Dec-02 9:54
mvaMarc Clifton24-Dec-02 9:54 
GeneralRe: why are classes sealed? Pin
Nish Nishant24-Dec-02 11:23
sitebuilderNish Nishant24-Dec-02 11:23 
AnswerRe: why are classes sealed? Pin
leppie24-Dec-02 10:13
leppie24-Dec-02 10:13 
AnswerRe: why are classes sealed? Pin
Christian Graus24-Dec-02 11:05
protectorChristian Graus24-Dec-02 11:05 
GeneralRe: why are classes sealed? Pin
Nish Nishant24-Dec-02 11:23
sitebuilderNish Nishant24-Dec-02 11:23 
GeneralRe: why are classes sealed? Pin
Christian Graus24-Dec-02 12:42
protectorChristian Graus24-Dec-02 12:42 
GeneralRe: why are classes sealed? Pin
Marc Clifton24-Dec-02 14:09
mvaMarc Clifton24-Dec-02 14:09 
GeneralRe: why are classes sealed? Pin
Christian Graus24-Dec-02 14:18
protectorChristian Graus24-Dec-02 14:18 
AnswerRe: why are classes sealed? Pin
James T. Johnson24-Dec-02 15:30
James T. Johnson24-Dec-02 15:30 
GeneralRe: why are classes sealed? Pin
JasonSmith25-Dec-02 16:04
JasonSmith25-Dec-02 16:04 
GeneralRe: why are classes sealed? Pin
James T. Johnson26-Dec-02 10:35
James T. Johnson26-Dec-02 10:35 
AnswerRe: why are classes sealed? Pin
James T. Johnson26-Dec-02 10:40
James T. Johnson26-Dec-02 10:40 
GeneralRe: why are classes sealed? Pin
Marc Clifton26-Dec-02 10:41
mvaMarc Clifton26-Dec-02 10:41 
QuestionHow to call GetIPictureFromPicture(Image image) method using C#. Pin
sns24-Dec-02 2:26
sns24-Dec-02 2:26 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.