private void OnEnumerate(object state)
{
IntPtr pInfo;
int entriesRead = 0;
int totalEntries = 0;
int resumeHandle = 0;
int result = ERROR_MORE_DATA;
GCHandle dwResumeHandle = GCHandle.Alloc(resumeHandle, GCHandleType.Pinned);
try
{
while (m_stopped == 0 && result == ERROR_MORE_DATA)
{
// enumerate the first batch of servers.
result = NetServerEnum(
IntPtr.Zero,
LEVEL_SERVER_INFO_100,
out pInfo,
MAX_PREFERRED_LENGTH,
out entriesRead,
out totalEntries,
SV_TYPE_WORKSTATION | SV_TYPE_SERVER,
m_domain,
dwResumeHandle.AddrOfPinnedObject());
// check for fatal error.
if ((result != NERR_Success) && (result != ERROR_MORE_DATA))
{
Utils.Trace("Could not enumerate hosts on network. Error = {0}", result);
return;
}
// copy host names from the returned structures.
string[] hostnames = new string[entriesRead];
IntPtr pos = pInfo;
for (int ii = 0; ii < entriesRead; ii++)
{
SERVER_INFO_100 info = (SERVER_INFO_100)Marshal.PtrToStructure(pos, typeof(SERVER_INFO_100));
hostnames[ii] = info.sv100_name;
pos = (IntPtr)(pos.ToInt64() + Marshal.SizeOf(typeof(SERVER_INFO_100)));
}
NetApiBufferFree(pInfo);
// raise an event.
if (m_stopped == 0 && m_HostsDiscovered != null)
{
try
{
m_HostsDiscovered(this, new HostEnumeratorEventArgs(hostnames));
}
catch (Exception e)
{
Utils.Trace(e, "Unexpected exception raising HostsDiscovered event.");
}
}
}
}
catch (Exception e)
{
Utils.Trace(e, "Unexpected exception calling NetServerEnum.");
}
finally
{
if (dwResumeHandle.IsAllocated)
{
dwResumeHandle.Free();
}
}
}
#endregion