using HANDLE = System.IntPtr;
using DWORD = System.UInt32;
[DllImport("netapi32.dll", SetLastError=true)]
internal static extern DWORD DsGetDcNext(
HANDLE GetDcContextHandle,
IntPtr SockAddressCount,
IntPtr SockAddresses, //must free this if using (SocketAddressCount > 1)
out IntPtr DnsHostName //will have to marshal this one manually
);
Declare Function DsGetDcNext Lib "netapi32.dll" (TODO) As TODO
None.
in Contains the domain controller enumeration context handle provided by the DsGetDcOpen function.
[out, optional] Pointer to a ULONG value that receives the number of elements in the SockAddresses array. If this parameter is NULL, socket addresses are not retrieved.
[out, optional] Pointer to an array of SOCKET_ADDRESS structures that receives the socket address data for the domain controller. SockAddressCount receives the number of elements in this array.
All returned addresses will be of type AF_INET or AF_INET6. The sin_port member contains the port from the server record. A port of 0 indicates no port is available from DNS.
The caller must free this memory when it is no longer required by calling LocalFree.
This parameter is ignored if SockAddressCount is NULL.
[out, optional] Pointer to a string pointer that receives the DNS name of the domain controller. This parameter receives NULL if no host name is known. The caller must free this memory when it is no longer required by calling NetApiBufferFree.
Please add some!
bool endLoop = false;
ArrayList al = new ArrayList();
while (!endLoop)
{
IntPtr pDnsHostName = IntPtr.Zero;
try
{
rc = NativeMethods.DsGetDcNext(
pDcContext,
IntPtr.Zero,
IntPtr.Zero,
out pDnsHostName
);
//check the return code
switch (rc)
{
case 259: //ERROR_NO_MORE_ITEMS
endLoop = true;
break;
case 0: //ERROR_SUCCESS
al.Add(Marshal.PtrToStringAuto(pDnsHostName));
break;
case ERROR_FILEMARK_DETECTED: //127 - I think...
break;
default:
throw new Win32Exception(); //internally calls GetLastWin32Error()
break;
}
}
finally
{
if (!Win32.IsNullHandle(pDnsHostName)) //pNnsHostName != IntPtr.Zero;
NativeMethods.NetApiBufferFree(pDnsHostName);
}
}
Do you know one? Please contribute it!