problem with NetServerEnum

Please support our C# advertiser: Intel Parallel Studio Home
Thread Solved

Join Date: Oct 2008
Posts: 52
Reputation: Bhoot is an unknown quantity at this point 
Solved Threads: 1
Bhoot's Avatar
Bhoot Bhoot is offline Offline
Junior Poster in Training

problem with NetServerEnum

 
0
  #1
May 29th, 2009
Inorder to list all the workstations on a LAN, i have used NetServerEnum() after importing it.

On running the program, it seemed to work fine. The two existing machines were detected correctly.
However, i want the list to be refreshed whenever required (some refresh button). So i detached the wire of the other computer from the switch, rendering only a single computer on the LAN.
Now, when i ran the program, it still lists out the disconnected machine.

How to solve this out?

The code is as follows :
  1. namespace LanIpAddresses
  2. {
  3. class NetApi
  4. {
  5. [DllImport ( "Netapi32.dll", EntryPoint = "NetServerEnum" )]
  6. public static extern Int32 NetServerEnum (
  7. [MarshalAs (UnmanagedType.LPWStr)] String serverName,
  8. Int32 level,
  9. out IntPtr bufferPtr,
  10. UInt32 prefMaxLen,
  11. ref Int32 entriesRead,
  12. ref Int32 totalEntries,
  13. UInt32 serverType,
  14. [MarshalAs (UnmanagedType.LPWStr)] String domain,
  15. IntPtr handle );
  16.  
  17. [DllImport ( "Netapi32.dll", EntryPoint = "NetApiBufferFree" )]
  18. public static extern UInt32 NetApiBufferFree ( IntPtr buffer );
  19. }
  20.  
  21.  
  22. class EnumerateLanMachines
  23. {
  24. public const UInt32 SUCCESS = 0;
  25. public const UInt32 FAIL = 234;
  26. public const UInt32 MAX_PREFERRED_LENGTH = 0xFFFFFFFF;
  27. //public ArrayList machines = new ArrayList ( );
  28.  
  29. enum ServerTypes : uint
  30. {
  31. WorkStation = 0x00000001,
  32. Server = 0x00000002
  33. }
  34.  
  35. [StructLayout ( LayoutKind.Sequential, CharSet = CharSet.Auto )]
  36. public struct MachineInfo
  37. {
  38. [MarshalAs ( UnmanagedType.U4 )]
  39. public UInt32 platformId;
  40.  
  41. [MarshalAs ( UnmanagedType.LPWStr )]
  42. public String serverName;
  43. }
  44.  
  45. public enum Platform
  46. {
  47. PLATFORM_ID_DOS = 300,
  48. PLATFORM_ID_OS2 = 400,
  49. PLATFORM_ID_NT = 500,
  50. PLATFORM_ID_OSF = 600,
  51. PLATFORM_ID_VMS = 700
  52. }
  53.  
  54. public void enumerateMachines ( )
  55. {
  56. IntPtr buffer = new IntPtr();
  57. int totalEntries = 0;
  58. int entriesRead = 0;
  59. int result;
  60.  
  61. result = NetApi.NetServerEnum ( null, 100, out buffer, MAX_PREFERRED_LENGTH, ref entriesRead, ref totalEntries, (uint) ServerTypes.WorkStation, null, IntPtr.Zero );
  62.  
  63. MachineInfo machineInfo;
  64.  
  65. if (result != FAIL)
  66. {
  67. Console.WriteLine ( "Succeeded!" );
  68. Console.WriteLine ( entriesRead );
  69. for (int i = 0; i < entriesRead; ++i)
  70. {
  71. machineInfo = (MachineInfo) Marshal.PtrToStructure ( buffer, typeof ( MachineInfo ) );
  72.  
  73. //machines.Add ( machineInfo );
  74. Console.WriteLine ( machineInfo.serverName );
  75.  
  76. buffer = (IntPtr) ( (ulong) buffer + (ulong) Marshal.SizeOf ( machineInfo ) );
  77. }
  78.  
  79. NetApi.NetApiBufferFree ( buffer );
  80. }
  81. }
  82. }
  83. }

  1. namespace LanIpAddresses
  2. {
  3. class Program
  4. {
  5. private static IPHostEntry ipHost;
  6. static ArrayList ipList;
  7.  
  8. static void Main ( string[ ] args )
  9. {
  10. EnumerateLanMachines enumerate = new EnumerateLanMachines ( );
  11.  
  12. enumerate.enumerateMachines ( );
  13.  
  14. /*foreach (EnumerateLanMachines.MachineInfo o in enumerate.machines)
  15. {
  16. Console.WriteLine ( o.platformId + " " + o.serverName );
  17. }*/
  18.  
  19. Console.ReadLine ( );
  20. }
  21. }
  22. }
Bhoot
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the C# Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC