internal static extern int NetServerEnum([MarshalAs(UnmanagedType.LPWStr)] string servername, int level, out IntPtr bufptr, int prefmaxlen, ref int entriesread, ref int totalentries, NetApi32.SV_101_TYPES servertype, [MarshalAs(UnmanagedType.LPWStr)] string domain, IntPtr resumeHandle);
public static ArrayList GetServerList(NetApi32.SV_101_TYPES ServerType) { int entriesread = 0, totalentries = 0; ArrayList alServers = new ArrayList(); do { // Buffer to store the available servers // Filled by the NetServerEnum function IntPtr buf = new IntPtr(); SERVER_INFO_101 server; int ret = NetServerEnum(null, 101, out buf, -1, ref entriesread, ref totalentries, ServerType, null, IntPtr.Zero); // if the function returned any data, fill the tree view if (ret == ERROR_SUCCESS || ret == ERROR_MORE_DATA || entriesread > 0) { IntPtr ptr = buf; for (int i = 0; i < entriesread; i++) { // cast pointer to a SERVER_INFO_101 structure server = (SERVER_INFO_101)Marshal.PtrToStructure(ptr, typeof(SERVER_INFO_101)); //Cast the pointer to a ulong so this addition will work on 32-bit or 64-bit systems. ptr = (IntPtr)((ulong)ptr + (ulong)Marshal.SizeOf(server)); // add the machine name and comment to the arrayList. //You could return the entire structure here if desired alServers.Add(server); } } // free the buffer NetApiBufferFree(buf); }while ( entriesread < totalentries && entriesread != 0 ); return(alServers); }