2009-05-29 101 views
1

我正在開發一個應用程序,它需要列出所有當前的LAN機器。NetServerEnum()在c#中的問題

爲了列出局域網上的所有工作站,我在導入它後使用了NetServerEnum()。

在運行程序時,它似乎工作正常。兩臺現有機器被正確檢測到。 但是,我希望在需要時刷新列表(某些刷新按鈕)。所以我把另一臺電腦的線從交換機上拆下來,只在局域網上渲染一臺計算機。 現在,當我運行程序時,它仍然列出了斷開的機器。

如何解決這個問題?

的代碼如下:

namespace LanIpAddresses 
{ 
    class NetApi 
    { 
     [DllImport ("Netapi32.dll", EntryPoint = "NetServerEnum")] 
     public static extern Int32 NetServerEnum (
      [MarshalAs (UnmanagedType.LPWStr)] String serverName, 
      Int32 level, 
      out IntPtr bufferPtr, 
      UInt32 prefMaxLen, 
      ref Int32 entriesRead, 
      ref Int32 totalEntries, 
      UInt32 serverType, 
      [MarshalAs (UnmanagedType.LPWStr)] String domain, 
      IntPtr handle); 

     [DllImport ("Netapi32.dll", EntryPoint = "NetApiBufferFree")] 
     public static extern UInt32 NetApiBufferFree (IntPtr buffer); 
    } 


    class EnumerateLanMachines 
    { 
     public const UInt32 SUCCESS = 0; 
     public const UInt32 FAIL = 234; 
     public const UInt32 MAX_PREFERRED_LENGTH = 0xFFFFFFFF; 
     //public ArrayList machines = new ArrayList (); 

     enum ServerTypes : uint 
     { 
      WorkStation = 0x00000001, 
      Server = 0x00000002 
     } 

     [StructLayout (LayoutKind.Sequential, CharSet = CharSet.Auto)] 
     public struct MachineInfo 
     { 
      [MarshalAs (UnmanagedType.U4)] 
      public UInt32 platformId; 

      [MarshalAs (UnmanagedType.LPWStr)] 
      public String serverName; 
     } 

     public enum Platform 
     { 
      PLATFORM_ID_DOS = 300, 
      PLATFORM_ID_OS2 = 400, 
      PLATFORM_ID_NT = 500, 
      PLATFORM_ID_OSF = 600, 
      PLATFORM_ID_VMS = 700 
     } 

     public void enumerateMachines () 
     { 
      IntPtr buffer = new IntPtr(); 
      int totalEntries = 0; 
      int entriesRead = 0; 
      int result; 

      result = NetApi.NetServerEnum (null, 100, out buffer, MAX_PREFERRED_LENGTH, ref entriesRead, ref totalEntries, (uint) ServerTypes.WorkStation, null, IntPtr.Zero); 

      MachineInfo machineInfo; 

      if (result != FAIL) 
      { 
       Console.WriteLine ("Succeeded!"); 
       Console.WriteLine (entriesRead); 
       for (int i = 0; i < entriesRead; ++i) 
       { 
        machineInfo = (MachineInfo) Marshal.PtrToStructure (buffer, typeof (MachineInfo)); 

        //machines.Add (machineInfo); 
        Console.WriteLine (machineInfo.serverName); 

        buffer = (IntPtr) ((ulong) buffer + (ulong) Marshal.SizeOf (machineInfo)); 
       } 

       NetApi.NetApiBufferFree (buffer); 
      } 
     } 
    } 
} 
namespace LanIpAddresses 
{ 
    class Program 
    { 
     private static IPHostEntry ipHost; 
     static ArrayList ipList; 

     static void Main (string[ ] args) 
     { 
      EnumerateLanMachines enumerate = new EnumerateLanMachines (); 

      enumerate.enumerateMachines (); 

      /*foreach (EnumerateLanMachines.MachineInfo o in enumerate.machines) 
      { 
       Console.WriteLine (o.platformId + " " + o.serverName); 
      }*/ 

      Console.ReadLine (); 
     } 
    } 
} 

回答

0

我不解決這個問題。但是,我選擇了另一種方式 - ping整個可能的IP範圍並連接到機器。

不過,我歡迎這仍然揮之不去的問題的任何答案:)

1

當使用NetServerEnum再一個就是使用Windows瀏覽器服務。主瀏覽器維護網絡中的計算機列表。如果計算機在網絡上自行發佈並關閉,則主瀏覽器會將該計算機保存在其列表中。

http://support.microsoft.com/kb/188001

如果改爲詢問有關網絡中的計算機的Active Directory中,那麼你就還可以獲得註冊的計算機列表,但是這並不意味着在列表中的所有計算機是否在線。