public sealed class NetworkBrowser
{
[DllImport("Netapi32", CharSet = CharSet.Auto, SetLastError = true), SuppressUnmanagedCodeSecurityAttribute]
public static extern int NetServerEnum(
string serverName,
int dwLevel,
ref IntPtr pBuf,
int dwPrefMaxLen,
out int dwEntriesRead,
out int dwTotalEntries,
int dwServerType,
string domain,
out int dwResumeHandle
);
[DllImport("Netapi32", SetLastError = true), SuppressUnmanagedCodeSecurity]
public static extern int NetApiBufferFree(IntPtr pBuf);
[StructLayout(LayoutKind.Sequential)]
public struct ServerInfo100
{
internal int sv100_platform_id;
[MarshalAs(UnmanagedType.LPWStr)]
internal string sv100_name;
}
public static ArrayList GetNetworkComputers()
{
ArrayList networkComputers = new ArrayList();
const int MAX_PREFERRED_LENGTH = -1;
int SV_TYPE_WORKSTATION = 1;
int SV_TYPE_SERVER = 2;
IntPtr buffer = IntPtr.Zero;
IntPtr tmpBuffer = IntPtr.Zero;
int entriesRead;
int totalEntries;
int resHandle;
int sizeofInfo = Marshal.SizeOf(typeof(ServerInfo100));
try
{
int ret = NetServerEnum(null, 100, ref buffer,
MAX_PREFERRED_LENGTH, out entriesRead, out totalEntries,
SV_TYPE_WORKSTATION | SV_TYPE_SERVER, null, out resHandle);
if (ret == 0)
{
for (int i = 0; i < totalEntries; i++)
{
tmpBuffer = new IntPtr((int)buffer +(i * sizeofInfo));
ServerInfo100 svrInfo = (ServerInfo100)
Marshal.PtrToStructure(tmpBuffer,
typeof(ServerInfo100));
networkComputers.Add(svrInfo.sv100_name);
}
}
}
catch (Exception ex)
{
return null;
}
finally
{
NetApiBufferFree(buffer);
}
return networkComputers;
}
}
Я много искал в Google, но не нашел решения для этого сценария.
Я пытаюсь найти все компьютеры в сети. Следующий код работает нормально для Win7-32bit, но выдает следующую ошибку для Win7-64bit.
[code]NetServerEnum()[/code] возвращает код -6118.
[code]public sealed class NetworkBrowser { [DllImport("Netapi32", CharSet = CharSet.Auto, SetLastError = true), SuppressUnmanagedCodeSecurityAttribute] public static extern int NetServerEnum( string serverName, int dwLevel, ref IntPtr pBuf, int dwPrefMaxLen, out int dwEntriesRead, out int dwTotalEntries, int dwServerType, string domain, out int dwResumeHandle );
[DllImport("Netapi32", SetLastError = true), SuppressUnmanagedCodeSecurity] public static extern int NetApiBufferFree(IntPtr pBuf);
[StructLayout(LayoutKind.Sequential)] public struct ServerInfo100 { internal int sv100_platform_id; [MarshalAs(UnmanagedType.LPWStr)] internal string sv100_name; }
public static ArrayList GetNetworkComputers() { ArrayList networkComputers = new ArrayList(); const int MAX_PREFERRED_LENGTH = -1; int SV_TYPE_WORKSTATION = 1; int SV_TYPE_SERVER = 2; IntPtr buffer = IntPtr.Zero; IntPtr tmpBuffer = IntPtr.Zero; int entriesRead; int totalEntries; int resHandle; int sizeofInfo = Marshal.SizeOf(typeof(ServerInfo100));
try { int ret = NetServerEnum(null, 100, ref buffer, MAX_PREFERRED_LENGTH, out entriesRead, out totalEntries, SV_TYPE_WORKSTATION | SV_TYPE_SERVER, null, out resHandle);
if (ret == 0) { for (int i = 0; i < totalEntries; i++) { tmpBuffer = new IntPtr((int)buffer +(i * sizeofInfo));