public static Hashtable QueryConfig() { Hashtable items = new Hashtable(); int requiredBufferLength = 0; HttpApi.Error error = QueryServiceConfig(IntPtr.Zero, 0, out requiredBufferLength); if ((error != HttpApi.Error.ERROR_FILE_NOT_FOUND) && (error != HttpApi.Error.ERROR_NOT_FOUND)) { if (error != HttpApi.Error.ERROR_INSUFFICIENT_BUFFER) { throw new HttpApiException(error, "HttpQueryServiceConfiguration (IPLISTEN) failed. Error = " + error); } IntPtr pOutput = Marshal.AllocHGlobal(requiredBufferLength); try { HttpApi.ZeroMemory(pOutput, requiredBufferLength); error = QueryServiceConfig(pOutput, requiredBufferLength, out requiredBufferLength); if (error != HttpApi.Error.NO_ERROR) { throw new HttpApiException(error, "HttpQueryServiceConfiguration (IPLISTEN) failed. Error = " + error); } HttpApi.HTTP_SERVICE_CONFIG_IP_LISTEN_QUERY output = (HttpApi.HTTP_SERVICE_CONFIG_IP_LISTEN_QUERY)Marshal.PtrToStructure(pOutput, typeof(HttpApi.HTTP_SERVICE_CONFIG_IP_LISTEN_QUERY)); int addrSize = Marshal.SizeOf(typeof(HttpApi.SOCKADDR_STORAGE)); IntPtr pAddress = HttpApi.IncIntPtr(pOutput, 8); // Increment past the addrcount member and padding for (int i = 0; i < output.AddrCount; i++) { IpListenConfigItem item = new IpListenConfigItem(); item.Address = new IPAddress((long)Marshal.ReadInt32(pAddress, 4) & 0x00000000ffffffff); item.Status = ModifiedStatus.Unmodified; items.Add(item.Key, item); pAddress = HttpApi.IncIntPtr(pAddress, addrSize); // Increment to the next IP address } } finally { Marshal.FreeHGlobal(pOutput); } } return(items); }
public static IntPtr BuildSockaddr(short family, ushort port, IPAddress address) { int sockaddrSize = Marshal.SizeOf(typeof(HttpApi.sockaddr)); IntPtr pSockaddr = Marshal.AllocHGlobal(sockaddrSize); HttpApi.ZeroMemory(pSockaddr, sockaddrSize); Marshal.WriteInt16(pSockaddr, family); ushort p = (ushort)IPAddress.HostToNetworkOrder((short)port); Marshal.WriteInt16(pSockaddr, 2, (short)p); byte[] addr = address.GetAddressBytes(); IntPtr pAddr = HttpApi.IncIntPtr(pSockaddr, 4); Marshal.Copy(addr, 0, pAddr, addr.Length); return(pSockaddr); }