Comment #9 on issue 44 by danports: Update documentation for GetServers to
Justin's suggestions -- documenting them here for now until I get a chance
to review the code and make some changes:
in Impl/NativeMethods.cs, line 46:
/* no longer works for Server 2008 and above
[DllImport("Wtsapi32.dll", CharSet = CharSet.Auto, SetLastError =
true)]
public static extern int
WTSEnumerateServers([MarshalAs(UnmanagedType.LPTStr)] string pDomainName,
int reserved,
int version, out
IntPtr ppServerInfo, out int pCount);
*/
//[DllImport("Wtsapi32.dll", CharSet = CharSet.Auto, SetLastError =
true)]
[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);
in Impl/NativeMethodsHelper.cs, line 142:
public static IList<WTS_SERVER_INFO> EnumerateServers(string
domainName)
{
IntPtr ppServerInfo;
int count = 0;
int totalEntries = 0;
UInt32 MAX_PREFERRED_LENGTH = 0xFFFFFFFF;
//all servers
UInt32 ServerType = 0xFFFFFFFF;
//only valid up to 2008.
//if (NativeMethods.WTSEnumerateServers(domainName, 0, 1, out
ppServerInfo, out count) == 0)
int result = NativeMethods.NetServerEnum(null, 100, out
ppServerInfo, MAX_PREFERRED_LENGTH, ref count, ref totalEntries,
ServerType, domainName, IntPtr.Zero);
if (result == 234)
{
throw new Win32Exception();
}
try
{
return PtrToStructureList<WTS_SERVER_INFO>(ppServerInfo,
count);
}
finally
{
NativeMethods.WTSFreeMemory(ppServerInfo);