Skip to content

Instantly share code, notes, and snippets.

@CosmosKey
Created November 21, 2016 21:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save CosmosKey/9052d4b4f42661838fa72a9762c2cb25 to your computer and use it in GitHub Desktop.
Save CosmosKey/9052d4b4f42661838fa72a9762c2cb25 to your computer and use it in GitHub Desktop.
$LUP_DEEP = 0x0001 # Queries deep as opposed to just the first level.
$LUP_CONTAINERS = 0x0002 # Returns containers only.
$LUP_NOCONTAINERS = 0x0004 # Do not return containers.
$LUP_NEAREST = 0x0008 # If possible, returns results in the order of distance. The measure of distance is provider specific.
$LUP_RETURN_NAME = 0x0010 # Retrieves the name as lpszServiceInstanceName.
$LUP_RETURN_TYPE = 0x0020 # Retrieves the type as lpServiceClassId.
$LUP_RETURN_VERSION = 0x0040 # Retrieves the version as lpVersion.
$LUP_RETURN_COMMENT = 0x0080 # Retrieves the comment as lpszComment.
$LUP_RETURN_ADDR = 0x0100 # Retrieves the addresses as lpcsaBuffer.
$LUP_RETURN_BLOB = 0x0200 # Retrieves the private data as lpBlob.
$LUP_RETURN_ALIASES = 0x0400 # Any available alias information is to be returned in successive calls to WSALookupServiceNext, and each alias returned will have the RESULT_IS_ALIAS flag set.
$LUP_RETURN_QUERY_STRING = 0x0800 # Retrieves the query string used for the request.
$LUP_RETURN_ALL = 0x0FF0 # A set of flags that retrieves all of the LUP_RETURN_* values.
$LUP_FLUSHPREVIOUS = 0x1000 # Used as a value for the dwControlFlags parameter in WSALookupServiceNext. Setting this flag instructs the provider to discard the last result set, which was too large for the specified buffer, and move on to the next result set.
$LUP_FLUSHCACHE = 0x2000 # If the provider has been caching information, ignores the cache, and queries the namespace itself.
$LUP_RES_SERVICE = 0x8000 # This indicates whether prime response is in the remote or local part of CSADDR_INFO structure. The other part needs to be usable in either case.
$sig = @"
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto)]
public struct WSAQUERYSET
{
public Int32 dwSize;
public String szServiceInstanceName;
public Guid lpServiceClassId;
public IntPtr lpVersion;
public String lpszComment;
public Int32 dwNameSpace;
public Guid lpNSProviderId;
public String lpszContext;
public Int32 dwNumberOfProtocols;
public IntPtr lpafpProtocols;
public String lpszQueryString;
public Int32 dwNumberOfCsAddrs;
public IntPtr lpcsaBuffer;
public Int32 dwOutputFlags;
public IntPtr Blob;
}
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)]
public struct WSAData
{
public Int16 version;
public Int16 highVersion;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 257)] public String description;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 129)] public String systemStatus;
public Int16 maxSockets;
public Int16 maxUdpDg;
public IntPtr vendorInfo;
}
[DllImport("ws2_32.dll", CharSet = CharSet.Auto, SetLastError=true)]
public static extern Int32 WSAStartup(Int16 wVersionRequested, out WSAData wsaData);
[DllImport("ws2_32.dll", CharSet = CharSet.Auto, SetLastError=true)]
public static extern Int32 WSALookupServiceBegin(WSAQUERYSET lpqsRestrictions, UInt32 dwControlFlags, ref IntPtr lphLookup);
[DllImport("ws2_32.dll", CharSet = CharSet.Auto, SetLastError=true)]
public static extern Int32 WSALookupServiceNext(IntPtr hLookup, UInt32 dwControlFlags,ref Int32 lpdwBufferLength, IntPtr pqsResults);
[DllImport("ws2_32.dll", CharSet = CharSet.Auto, SetLastError=true)]
public static extern Int32 WSALookupServiceEnd(IntPtr hLookup);
[DllImport("ws2_32.dll", CharSet = CharSet.Auto, SetLastError=true)]
public static extern Int32 WSAGetLastError();
"@
$i = 10
$newTypes = Add-Type -MemberDefinition $sig -Name "BT$i" -Namespace "BTLAB" -PassThru
$type = $newTypes | Where-Object name -eq "BT$i"
$restrictions = New-Object -TypeName "$($type.FullName)+WSAQUERYSET"
$wsaData = New-Object -TypeName "$($type.FullName)+WSAData"
[guid]$SVCID_HOSTNAME = "0002a800-0000-0000-c000-000000000046"
$restrictions.lpServiceClassId = $SVCID_HOSTNAME
[int]$lookup = 0
[int16]$versionRequested = 0x0202 # 0x0202 for Winsock 2.2, 0x0102 for 2.1 ...
$type::WSAStartup($versionRequested,[ref]$wsaData)
$type::WSALookupServiceBegin($restrictions,$LUP_RETURN_NAME,[ref]$lookup)
$type::WSAGetLastError()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment