943,929 Members | Top Members by Rank

Ad:
  • C# Discussion Thread
  • Marked Solved
  • Views: 872
  • C# RSS
May 29th, 2009
0

problem with NetServerEnum

Expand Post »
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 :
C# Syntax (Toggle Plain Text)
  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. }

C# Syntax (Toggle Plain Text)
  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. }
Similar Threads
Reputation Points: 10
Solved Threads: 1
Junior Poster in Training
Bhoot is offline Offline
53 posts
since Oct 2008

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C# Forum Timeline: Cannot implicitly convert type 'object' to 'char[]'.
Next Thread in C# Forum Timeline: Drag and drop issue





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC