正在嘗試查找網絡上的所有計算機。以下代碼對Win7-32bit工作正常,但Win7-64bit給出以下錯誤。使用NetServerEnum()查找網絡上的所有計算機()
NetServerEnum()
正在返回代碼-6118。
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;
}
}
我對谷歌搜索了很多,但沒有找到任何解決方案,這種情況。
我試圖建立使用所有平臺目標 - 任何CPU,x86。但它仍然沒有工作。 – Akshay