using System;
using System.Collections;
using System.IO;
using System.Net;
using System.Net.Sockets;
using System.Runtime.InteropServices;
using Vanara.InteropServices;
using FILETIME = System.Runtime.InteropServices.ComTypes.FILETIME;
[assembly: Guid("DCB00D01-570F-4A9B-8D69-199FDBA5723B")]
#if !NETSTANDARD2_0
[assembly: TypeLibVersion(1, 0)]
[assembly: ImportedFromTypeLib("NETWORKLIST")]
#endif
[assembly: PrimaryInteropAssembly(1, 0)]
namespace Vanara.PInvoke.NetListMgr
{
/// The enumeration specifies a set of cost levels and cost flags supported in Windows 8 Cost APIs.
[Flags]
[PInvokeData("Netlistmgr.h")]
public enum NLM_CONNECTION_COST : uint
{
/// The cost is unknown.
NLM_CONNECTION_COST_UNKNOWN = 0x0,
/// The connection is unlimited and is considered to be unrestricted of usage charges and capacity constraints.
NLM_CONNECTION_COST_UNRESTRICTED = 0x1,
/// The use of this connection is unrestricted up to a specific data transfer limit.
NLM_CONNECTION_COST_FIXED = 0x2,
/// This connection is regulated on a per byte basis.
NLM_CONNECTION_COST_VARIABLE = 0x4,
/// The connection is currently in an OverDataLimit state as it has exceeded the carrier specified data transfer limit.
NLM_CONNECTION_COST_OVERDATALIMIT = 0x10000,
/// The network is experiencing high traffic load and is congested.
NLM_CONNECTION_COST_CONGESTED = 0x20000,
/// The connection is roaming outside the network and affiliates of the home provider.
NLM_CONNECTION_COST_ROAMING = 0x40000,
/// The connection is approaching the data limit specified by the carrier.
NLM_CONNECTION_COST_APPROACHINGDATALIMIT = 0x80000
}
/// The NLM_CONNECTION PROPERTY_CHANGE enumeration is a set of flags that define changes made to the properties of a network connection.
[PInvokeData("Netlistmgr.h")]
public enum NLM_CONNECTION_PROPERTY_CHANGE
{
/// The Authentication (Domain Type) of this Network Connection has changed.
NLM_CONNECTION_PROPERTY_CHANGE_AUTHENTICATION = 1
}
/// The NLM_Connectivity enumeration is a set of flags that provide notification whenever connectivity related parameters have changed.
[Flags]
[PInvokeData("Netlistmgr.h")]
public enum NLM_CONNECTIVITY
{
/// The underlying network interfaces have no connectivity to any network.
NLM_CONNECTIVITY_DISCONNECTED = 0x0000,
/// There is connectivity to a network, but the service cannot detect any IPv4 Network Traffic.
NLM_CONNECTIVITY_IPV4_NOTRAFFIC = 0x0001,
/// There is connectivity to a network, but the service cannot detect any IPv6 Network Traffic.
NLM_CONNECTIVITY_IPV6_NOTRAFFIC = 0x0002,
/// There is connectivity to the local subnet using the IPv4 protocol.
NLM_CONNECTIVITY_IPV4_SUBNET = 0x0010,
/// There is connectivity to a routed network using the IPv4 protocol.
NLM_CONNECTIVITY_IPV4_LOCALNETWORK = 0x0020,
/// There is connectivity to the Internet using the IPv4 protocol.
NLM_CONNECTIVITY_IPV4_INTERNET = 0x0040,
/// There is connectivity to the local subnet using the IPv6 protocol.
NLM_CONNECTIVITY_IPV6_SUBNET = 0x0100,
/// There is connectivity to a local network using the IPv6 protocol.
NLM_CONNECTIVITY_IPV6_LOCALNETWORK = 0x0200,
/// There is connectivity to the Internet using the IPv6 protocol.
NLM_CONNECTIVITY_IPV6_INTERNET = 0x0400
}
/// The NLM_DOMAIN_TYPE enumeration is a set of flags that specify the domain type of a network.
[PInvokeData("Netlistmgr.h")]
public enum NLM_DOMAIN_TYPE
{
/// The Network is not an Active Directory Network.
NLM_DOMAIN_TYPE_NON_DOMAIN_NETWORK = 0x0,
/// The Network is an Active Directory Network, but this machine is not authenticated against it.
NLM_DOMAIN_TYPE_DOMAIN_NETWORK = 0x01,
/// The Network is an Active Directory Network, and this machine is authenticated against it.
NLM_DOMAIN_TYPE_DOMAIN_AUTHENTICATED = 0x02
}
/// The NLM_ENUM_NETWORK enumeration contains a set of flags that specify what types of networks are enumerated.
[PInvokeData("Netlistmgr.h")]
public enum NLM_ENUM_NETWORK
{
/// Returns connected networks
NLM_ENUM_NETWORK_CONNECTED = 0x01,
/// Returns disconnected networks
NLM_ENUM_NETWORK_DISCONNECTED = 0x02,
/// Returns connected and disconnected networks
NLM_ENUM_NETWORK_ALL = 0x03
}
#pragma warning disable CS1591 // Missing XML comment for publicly visible type or member
[Flags]
[PInvokeData("Netlistmgr.h")]
public enum NLM_INTERNET_CONNECTIVITY
{
NLM_INTERNET_CONNECTIVITY_WEBHIJACK = 0x1,
NLM_INTERNET_CONNECTIVITY_PROXIED = 0x2,
NLM_INTERNET_CONNECTIVITY_CORPORATE = 0x4
}
#pragma warning restore CS1591 // Missing XML comment for publicly visible type or member
/// The NLM_NETWORK_CATEGORY enumeration is a set of flags that specify the category type of a network.
[PInvokeData("Netlistmgr.h")]
public enum NLM_NETWORK_CATEGORY
{
/// The network is a public (untrusted) network.
NLM_NETWORK_CATEGORY_PUBLIC,
/// The network is a private (trusted) network.
NLM_NETWORK_CATEGORY_PRIVATE,
/// The network is authenticated against an Active Directory domain.
NLM_NETWORK_CATEGORY_DOMAIN_AUTHENTICATED
}
#pragma warning disable CS1591 // Missing XML comment for publicly visible type or member
[Flags]
[PInvokeData("Netlistmgr.h")]
public enum NLM_NETWORK_CLASS
{
NLM_NETWORK_IDENTIFYING = 0x1,
NLM_NETWORK_IDENTIFIED = 0x2,
NLM_NETWORK_UNIDENTIFIED = 0x3
}
#pragma warning restore CS1591 // Missing XML comment for publicly visible type or member
/// The NLM_NETWORK_PROPERTY_CHANGE enumeration is a set of flags that define changes made to the properties of a network.
[PInvokeData("Netlistmgr.h")]
public enum NLM_NETWORK_PROPERTY_CHANGE
{
/// The category of the network has changed.
NLM_NETWORK_PROPERTY_CHANGE_CATEGORY_VALUE = 0x10,
/// A connection to this network has been added or removed.
NLM_NETWORK_PROPERTY_CHANGE_CONNECTION = 1,
/// The description of the network has changed.
NLM_NETWORK_PROPERTY_CHANGE_DESCRIPTION = 2,
/// The icon of the network has changed.
NLM_NETWORK_PROPERTY_CHANGE_ICON = 8,
/// The name of the network has changed.
NLM_NETWORK_PROPERTY_CHANGE_NAME = 4
}
///
/// The IEnumNetworkConnections interface provides a standard enumerator for network connections. It enumerates active, disconnected, or all network
/// connections within a network. This interface can be obtained from the INetwork interface.
///
///
[ComImport, TypeLibType(TypeLibTypeFlags.FDispatchable | TypeLibTypeFlags.FDual), Guid("DCB00006-570F-4A9B-8D69-199FDBA5723B")]
[PInvokeData("Netlistmgr.h", MSDNShortId = "aa370706")]
public interface IEnumNetworkConnections : IEnumerable
{
/// Returns an enumerator that iterates through a collection.
/// An object that can be used to iterate through the collection.
[DispId(-4)]
#if NETFRAMEWORK
[return: MarshalAs(UnmanagedType.CustomMarshaler, MarshalType = "", MarshalTypeRef = typeof(System.Runtime.InteropServices.CustomMarshalers.EnumeratorToEnumVariantMarshaler), MarshalCookie = "")]
new IEnumerator GetEnumerator();
#else
object _NewEnum { get; }
#endif
/// Gets the next specified number of elements in the enumeration sequence.
/// Number of elements requested.
/// Pointer to a list of pointers returned by INetworkConnection.
/// Pointer to the number of elements supplied. May be NULL if celt is one.
[DispId(1), PreserveSig]
HRESULT Next(uint celt, [Out, MarshalAs(UnmanagedType.LPArray, ArraySubType = UnmanagedType.Interface, SizeParamIndex = 0)] INetworkConnection[] rgelt, out uint pceltFetched);
/// The Skip method skips over the next specified number of elements in the enumeration sequence.
/// Number of elements to skip over in the enumeration.
[DispId(2), PreserveSig]
HRESULT Skip([In] uint celt);
/// The Reset method resets the enumeration sequence to the beginning.
[DispId(3), PreserveSig]
HRESULT Reset();
/// The Clone method creates an enumerator that contains the same enumeration state as the enumerator currently in use.
/// Pointer to new IEnumNetworkConnections interface instance.
[return: MarshalAs(UnmanagedType.Interface)]
[DispId(4)]
IEnumNetworkConnections Clone();
}
/// Enumerates all networks available on the server. This interface can be obtained from the INetwork interface.
///
[ComImport, TypeLibType(TypeLibTypeFlags.FDispatchable | TypeLibTypeFlags.FDual), Guid("DCB00003-570F-4A9B-8D69-199FDBA5723B")]
[PInvokeData("Netlistmgr.h", MSDNShortId = "aa370735")]
public interface IEnumNetworks : IEnumerable
{
/// Returns an enumerator that iterates through a collection.
/// An object that can be used to iterate through the collection.
[DispId(-4)]
#if NETFRAMEWORK
[return: MarshalAs(UnmanagedType.CustomMarshaler, MarshalType = "", MarshalTypeRef = typeof(System.Runtime.InteropServices.CustomMarshalers.EnumeratorToEnumVariantMarshaler), MarshalCookie = "")]
new IEnumerator GetEnumerator();
#else
object _NewEnum { get; }
#endif
/// Gets the next specified number of elements in the enumeration sequence.
/// Number of elements requested.
/// Pointer to a list of pointers returned by INetworkConnection.
/// Pointer to the number of elements supplied. May be NULL if celt is one.
[DispId(1), PreserveSig]
HRESULT Next(uint celt, [Out, MarshalAs(UnmanagedType.LPArray, ArraySubType = UnmanagedType.Interface, SizeParamIndex = 0)] INetwork[] rgelt, out uint pceltFetched);
/// The Skip method skips over the next specified number of elements in the enumeration sequence.
/// Number of elements to skip over in the enumeration.
[DispId(2), PreserveSig]
HRESULT Skip([In] uint celt);
/// The Reset method resets the enumeration sequence to the beginning.
[DispId(3), PreserveSig]
HRESULT Reset();
/// The Clone method creates an enumerator that contains the same enumeration state as the enumerator currently in use.
/// Pointer to new IEnumNetworks interface instance.
[return: MarshalAs(UnmanagedType.Interface)]
[DispId(4)]
IEnumNetworks Clone();
}
///
/// The INetwork interface represents a network on the local machine. It can also represent a collection of network connections with a similar network signature.
///
[ComImport, TypeLibType(TypeLibTypeFlags.FDispatchable | TypeLibTypeFlags.FDual), Guid("DCB00002-570F-4A9B-8D69-199FDBA5723B")]
[PInvokeData("Netlistmgr.h", MSDNShortId = "aa370750")]
public interface INetwork
{
/// Returns the name of the network.
/// Pointer to the name of the network.
[return: MarshalAs(UnmanagedType.BStr)]
[DispId(1)]
string GetName();
/// Sets or renames the network. This change occurs immediately.
/// Zero-terminated string that contains the new name of the network.
[DispId(2)]
void SetName([In, MarshalAs(UnmanagedType.BStr)] string szNetworkNewName);
/// Returns a description string for the network.
/// A string that specifies the text description of the network. This value must be freed using the SysFreeString API.
[return: MarshalAs(UnmanagedType.BStr)]
[DispId(3)]
string GetDescription();
/// Sets a new description for the network.
/// Zero-terminated string that contains the description of the network.
[DispId(4)]
void SetDescription([In, MarshalAs(UnmanagedType.BStr)] string szDescription);
/// Returns the unique identifier of a network.
/// Pointer to a GUID that specifies the network ID.
[DispId(5)]
Guid GetNetworkId();
/// Returns the type of network.
/// An NLM_DOMAIN_TYPE enumeration value that specifies the domain type of the network.
[DispId(6)]
NLM_DOMAIN_TYPE GetDomainType();
///
/// Returns an enumeration of all network connections for a network. A network can have multiple connections to it from different interfaces or
/// different links from the same interface.
///
/// An IEnumNetworkConnections interface instance that enumerates the list of local connections to this network.
[return: MarshalAs(UnmanagedType.Interface)]
[DispId(7)]
IEnumNetworkConnections GetNetworkConnections();
/// Returns the local date and time when the network was created and connected.
/// Pointer to a datetime when the network was created. Specifically, it contains the low DWORD of .
/// Pointer to a datetime when the network was created. Specifically, it contains the high DWORD of .
///
/// Pointer to a datetime when the network was last connected to. Specifically, it contains the low DWORD of .
///
///
/// Pointer to a datetime when the network was last connected to. Specifically, it contains the high DWORD of .
///
[DispId(8)]
void GetTimeCreatedAndConnected(out uint pdwLowDateTimeCreated, out uint pdwHighDateTimeCreated,
out uint pdwLowDateTimeConnected, out uint pdwHighDateTimeConnected);
/// Specifies if the network has internet connectivity.
/// If TRUE, this network has connectivity to the internet; if FALSE, it does not.
[DispId(9)]
bool IsConnectedToInternet { [DispId(9)] get; }
/// Specifies if the network has any network connectivity.
/// If TRUE, this network is connected; if FALSE, it is not.
[DispId(10)]
bool IsConnected { [DispId(10)] get; }
/// Returns the connectivity state of the network.
/// A NLM_CONNECTIVITY enumeration value that contains a bitmask that specifies the connectivity state of this network.
[DispId(11)]
NLM_CONNECTIVITY GetConnectivity();
/// Returns the category of a network.
/// A NLM_NETWORK_CATEGORY enumeration value that specifies the category information for the network.
[DispId(12)]
NLM_NETWORK_CATEGORY GetCategory();
/// Sets the category of a network. Administrative privileges are needed for this API call.
/// The new category.
[DispId(13)]
void SetCategory([In] NLM_NETWORK_CATEGORY NewCategory);
}
/// The INetworkConnection interface represents a single network connection.
[ComImport, TypeLibType(TypeLibTypeFlags.FDispatchable | TypeLibTypeFlags.FDual), Guid("DCB00005-570F-4A9B-8D69-199FDBA5723B")]
[PInvokeData("Netlistmgr.h", MSDNShortId = "aa370751")]
public interface INetworkConnection
{
/// Returns the associated network for the connection.
/// An INetwork interface instance that specifies the network associated with the connection.
[return: MarshalAs(UnmanagedType.Interface)]
[DispId(1)]
INetwork GetNetwork();
/// Specifies if the associated network connection has internet connectivity.
/// If TRUE, this network connection has connectivity to the internet; if FALSE, it does not.
[DispId(2)]
bool IsConnectedToInternet { [DispId(2)] get; }
/// Specifies if the associated network connection has any network connectivity.
/// If TRUE, this network connection has connectivity; if FALSE, it does not.
[DispId(3)]
bool IsConnected { [DispId(3)] get; }
/// Returns the connectivity state of the network.
/// A NLM_CONNECTIVITY enumeration value that contains a bitmask that specifies the connectivity of this network connection.
[DispId(4)]
NLM_CONNECTIVITY GetConnectivity();
/// Returns the Connection ID associated with this network connection.
/// A GUID that specifies the Connection ID associated with this network connection.
[DispId(5)]
Guid GetConnectionId();
/// Returns the ID of the network adapter used by this connection. There may multiple connections using the same adapter ID.
/// A GUID that specifies the adapter ID of the TCP/IP interface used by this network connection.
[DispId(6)]
Guid GetAdapterId();
/// Returns the type of network connection.
/// An NLM_DOMAIN_TYPE enumeration value that specifies the domain type of the network.
[DispId(7)]
NLM_DOMAIN_TYPE GetDomainType();
}
/// Use this interface to query current network cost and data plan status associated with a connection.
[ComImport, InterfaceType(ComInterfaceType.InterfaceIsIUnknown), Guid("DCB0000A-570F-4A9B-8D69-199FDBA5723B")]
[PInvokeData("Netlistmgr.h", MSDNShortId = "hh448251")]
public interface INetworkConnectionCost
{
/// Retrieves the network cost associated with a connection.
///
/// A DWORD value that represents the network cost of the connection. The lowest 16 bits represent the cost level and the highest 16 bits represent
/// the cost flags. Possible values are defined by the enumeration.
///
NLM_CONNECTION_COST GetCost();
/// Gets the data plan status.
///
/// An NLM_DATAPLAN_STATUS structure that describes the status of the data plan associated with the connection. The caller supplies the memory of
/// this structure.
///
NLM_DATAPLAN_STATUS GetDataPlanStatus();
}
/// Use this interface to notify an application of cost and data plan status change events for a connection.
[ComImport, Guid("DCB0000B-570F-4A9B-8D69-199FDBA5723B"), InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
[PInvokeData("Netlistmgr.h")]
public interface INetworkConnectionCostEvents
{
/// The ConnectionCostChanged method notifies an application of a network cost change for a connection.
/// A unique ID that identifies the connection on which the cost change event occurred.
///
/// A DWORD value that represents the new cost of the connection. The lowest 16 bits represent the cost level, and the highest 16 bits represent the
/// flags. Possible values are defined by the enumeration.
///
[PreserveSig]
HRESULT ConnectionCostChanged([In] Guid connectionId, [In] NLM_CONNECTION_COST newCost);
/// The ConnectionDataPlanStatusChanged method notifies an application of a data plan status change on a connection.
/// A unique ID that identifies the connection on which the data plan status change event occurred.
[PreserveSig]
HRESULT ConnectionDataPlanStatusChanged([In] Guid connectionId);
}
///
/// The INetworkConnectionEvents interface is a message sink interface that a client implements to get network connection-related events. Applications
/// that are interested in lower-level events (such as authentication changes) must implement this interface.
///
[ComImport, Guid("DCB00007-570F-4A9B-8D69-199FDBA5723B"), InterfaceType(ComInterfaceType.InterfaceIsIUnknown), TypeLibType(TypeLibTypeFlags.FOleAutomation)]
[PInvokeData("Netlistmgr.h")]
public interface INetworkConnectionEvents
{
///
/// The NetworkConnectionConnectivityChanged method notifies a client when connectivity change events occur on a network connection level.
///
/// A GUID that identifies the network connection on which the event occurred.
/// NLM_CONNECTIVITY enumeration value that specifies the new connectivity for this network connection.
[PreserveSig]
HRESULT NetworkConnectionConnectivityChanged([In] Guid connectionId, [In] NLM_CONNECTIVITY newConnectivity);
///
/// The NetworkConnectionPropertyChanged method notifies a client when property change events related to a specific network connection occur.
///
/// A GUID that identifies the network connection on which the event occurred.
/// The NLM_CONNECTION_PROPERTY_CHANGE flags for this connection.
[PreserveSig]
HRESULT NetworkConnectionPropertyChanged([In] Guid connectionId, [In] NLM_CONNECTION_PROPERTY_CHANGE Flags);
}
///
/// Use this interface to query for machine-wide cost and data plan status information associated with either a connection used for machine-wide Internet
/// connectivity, or the first-hop of routing to a specific destination on a connection. Additionally, this interface enables applications to specify
/// destination IP addresses to receive cost or data plan status change notifications for.
///
[ComImport, Guid("DCB00008-570F-4A9B-8D69-199FDBA5723B"), InterfaceType(ComInterfaceType.InterfaceIsIUnknown), CoClass(typeof(NetworkListManager))]
[PInvokeData("Netlistmgr.h", MSDNShortId = "hh448257")]
public interface INetworkCostManager
{
///
/// The GetCost method retrieves the current cost of either a machine-wide internet connection, or the first-hop of routing to a specific destination
/// on a connection. If destIPaddr is NULL, this method instead returns the cost of the network used for machine-wide Internet connectivity.
///
///
/// A DWORD value that indicates the cost of the connection. The lowest 16 bits represent the cost level, and the highest 16 bits represent the
/// flags. Possible values are defined by the enumeration.
///
///
/// An structure containing the destination IPv4/IPv6 address. If NULL, this method will instead return the cost associated with the
/// preferred connection used for machine Internet connectivity.
///
void GetCost(out NLM_CONNECTION_COST pCost, [In, Optional] NLM_SOCKADDR pDestIPAddr);
///
/// The GetDataPlanStatus retrieves the data plan status for either a machine-wide internet connection , or the first-hop of routing to a specific
/// destination on a connection. If an IPv4/IPv6 address is not specified, this method returns the data plan status of the connection used for
/// machine-wide Internet connectivity.
///
///
/// Pointer to an NLM_DATAPLAN_STATUS structure that describes the data plan status associated with a connection used to route to a destination. If
/// destIPAddr specifies a tunnel address, the first available data plan status in the interface stack is returned.
///
///
/// An structure containing the destination IPv4/IPv6 or tunnel address. If NULL, this method returns the cost associated with the
/// preferred connection used for machine Internet connectivity.
///
void GetDataPlanStatus(out NLM_DATAPLAN_STATUS pDataPlanStatus, [In, Optional] NLM_SOCKADDR pDestIPAddr);
///
/// The SetDestinationAddresses method registers specified destination IPv4/IPv6 addresses to receive cost or data plan
/// status change notifications.
///
/// The number of destination IPv4/IPv6 addresses in the list.
///
/// A NLM_SOCKADDR structure containing a list of destination IPv4/IPv6 addresses to register for cost or data plan status change notification.
///
///
/// If true, pDestIPAddrList will be appended to the existing address list; otherwise the existing list will be overwritten.
///
///
/// Returns S_OK on success, otherwise an HRESULT error code is returned.
///
///
/// Return code
/// Description
///
/// -
/// E_INVALIDARG
///
/// Returned if one of the following occurs:
///
/// -
/// length is 0.
///
/// -
/// length is larger than NLM_MAX_ADDRESS_LIST_SIZE(10)
///
/// -
///
/// bAppend is VARIANT_TRUE, but including the number of subscribed destinations in the existing list with the value of length
/// exceeds NLM_MAX_ADDRESS_SIZE.
///
///
/// -
/// A destination address in the supplied list is invalid.
///
///
///
///
/// -
/// E_POINTER
/// destIPAddrList is NULL.
///
/// -
/// HRESULT_FROM_WIN32(ERROR_NOT_SUPPORTED)
///
/// The request is not supported. This error is returned if either an IPv4 or IPv6 stack is not present on the local computer but
/// either an IPv4 or IPv6 address was specified bydestIPAddr.
///
///
/// -
/// HRESULT_FROM_WIN32(ERROR_ALREADY_INITIALIZED)
///
/// This method was called after registering for INetworkCostManagerEvents by calling IConnectionPoint::Advise. See Remark for more information.
///
///
///
///
///
///
/// This method must be called before IConnectionPoint::Advise. Once IConnectionPoint::Advise is called, this method will not
/// complete successfully until last sink calls IConnectionPoint::UnAdvise. However, this method can be called multiple times prior
/// to the call to IConnectionPoint::Advise.
///
///
/// If a list of destination addresses indicated by pDestIPAddrList contains duplicate addresses, only one of each will be used to
/// notify cost changes. Callers can clear a list of destinations by calling this function with length set to 0, destIPAddrList set
/// NULL, and bAppend set FALSE.
///
///
// https://docs.microsoft.com/en-us/windows/win32/api/netlistmgr/nf-netlistmgr-inetworkcostmanager-setdestinationaddresses
// HRESULT SetDestinationAddresses( UINT32 length, NLM_SOCKADDR *pDestIPAddrList, VARIANT_BOOL bAppend );
void SetDestinationAddresses([In] uint length, [In, Optional, MarshalAs(UnmanagedType.LPArray)] NLM_SOCKADDR[] pDestIPAddrList, [In] bool bAppend);
}
/// Use this interface to notify an application of machine-wide cost and data plan related events.
[ComImport, Guid("DCB00009-570F-4A9B-8D69-199FDBA5723B"), InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
[PInvokeData("Netlistmgr.h")]
public interface INetworkCostManagerEvents
{
///
/// The CostChanged method is called to indicates a cost change for either machine-wide Internet connectivity, or the first-hop of routing to a
/// specific destination on a connection.
///
///
/// A DWORD that represents the new cost of the connection. The lowest 16 bits represent the cost level, and the highest 16 bits represent the flags.
/// Possible values are defined by the enumeration.
///
///
/// An structure containing an IPv4/IPv6 address that identifies the destination on which the event occurred. If destAddr is NULL, the
/// change is a machine-wide Internet connectivity change.
///
[PreserveSig]
HRESULT CostChanged([In] NLM_CONNECTION_COST newCost, [In, Optional] NLM_SOCKADDR pDestAddr);
///
/// The DataPlanStatusChanged method is called to indicate a change to the status of a data plan associated with either a connection used for
/// machine-wide Internet connectivity, or the first-hop of routing to a specific destination on a connection.
///
///
/// An structure containing an IPv4/IPv6 address that identifies the destination for which the event occurred. If destAddr is NULL, the
/// change is a machine-wide Internet connectivity change.
///
[PreserveSig]
HRESULT DataPlanStatusChanged([In, Optional] NLM_SOCKADDR pDestAddr);
}
///
/// INetworkEvents is a notification sink interface that a client implements to get network related events. These APIs are all callback functions that
/// are called automatically when the respective events are raised.
///
[ComImport, Guid("DCB00004-570F-4A9B-8D69-199FDBA5723B"), InterfaceType(ComInterfaceType.InterfaceIsIUnknown), TypeLibType(TypeLibTypeFlags.FOleAutomation)]
[PInvokeData("Netlistmgr.h")]
public interface INetworkEvents
{
/// The NetworkAdded method is called when a new network is added. The GUID of the new network is provided.
/// A GUID that specifies the new network that was added.
[PreserveSig]
HRESULT NetworkAdded([In] Guid networkId);
/// The NetworkDeleted method is called when a network is deleted.
/// GUID that contains the network ID of the network that was deleted.
[PreserveSig]
HRESULT NetworkDeleted([In] Guid networkId);
/// The NetworkConnectivityChanged method is called when network connectivity related changes occur.
/// A GUID that specifies the new network that was added.
/// NLM_CONNECTIVITY enumeration value that contains the new connectivity of this network
[PreserveSig]
HRESULT NetworkConnectivityChanged([In] Guid networkId, [In] NLM_CONNECTIVITY newConnectivity);
/// The NetworkPropertyChanged method is called when a network property change is detected.
/// GUID that specifies the network on which this event occurred.
/// NLM_NETWORK_PROPERTY_CHANGE enumeration value that specifies the network property that changed.
[PreserveSig]
HRESULT NetworkPropertyChanged([In] Guid networkId, [In] NLM_NETWORK_PROPERTY_CHANGE Flags);
}
/// The INetworkListManager interface provides a set of methods to perform network list management functions.
[TypeLibType(TypeLibTypeFlags.FDual | TypeLibTypeFlags.FDispatchable)]
[ComImport, Guid("DCB00000-570F-4A9B-8D69-199FDBA5723B"), CoClass(typeof(NetworkListManager))]
[PInvokeData("Netlistmgr.h", MSDNShortId = "aa370769")]
public interface INetworkListManager
{
/// Retrieves networks based on the supplied Network IDs.
/// NLM_ENUM_NETWORK enumeration value that specifies the flags for the network (specifically, connected or not connected).
/// An IEnumNetworks interface instance that contains the enumerator for the list of available networks.
[return: MarshalAs(UnmanagedType.Interface)]
[DispId(1)]
IEnumNetworks GetNetworks([In] NLM_ENUM_NETWORK Flags);
/// Retrieves a network based on a supplied Network ID.
/// GUID that specifies the network ID.
/// The INetwork interface instance for this network.
[return: MarshalAs(UnmanagedType.Interface)]
[DispId(2)]
INetwork GetNetwork([In] Guid gdNetworkId);
/// Gets an enumerator that contains a complete list of the network connections that have been made.
/// An IEnumNetworkConnections interface instance that enumerates all network connections on the machine.
[return: MarshalAs(UnmanagedType.Interface)]
[DispId(3)]
IEnumNetworkConnections GetNetworkConnections();
/// Retrieves a network based on a supplied Network Connection ID.
/// A GUID that specifies the Network Connection ID.
/// A INetworkConnection object associated with the supplied gdNetworkConnectionId.
[return: MarshalAs(UnmanagedType.Interface)]
[DispId(4)]
INetworkConnection GetNetworkConnection([In] Guid gdNetworkConnectionId);
/// Specifies if the machine has Internet connectivity.
/// If TRUE, the local machine is connected to the internet; if FALSE, it is not.
[DispId(5)]
bool IsConnectedToInternet { [DispId(5)] get; }
/// Specifies if the local machine has network connectivity.
///
/// If TRUE , the network has at least local connectivity via ipv4 or ipv6 or both. The network may also have internet connectivity. Thus, the
/// network is connected. If FALSE, the network does not have local or internet connectivity.The network is not connected.
///
[DispId(6)]
bool IsConnected { [DispId(6)] get; }
/// Returns the connectivity state of all the networks on a machine.
/// An NLM_CONNECTIVITY enumeration value that contains a bitmask that specifies the network connectivity of this machine.
[DispId(7)]
NLM_CONNECTIVITY GetConnectivity();
///
/// Applies a specific set of connection profile values to the internet connection profile in support of the simulation of specific metered internet
/// connection conditions.
///
/// The simulation only applies in an RDP Child Session and does not affect the primary user session. The simulated internet connection profile is
/// returned via the Windows Runtime API GetInternetConnectionProfile.
///
///
///
/// Specific connection profile values to simulate on the current internet connection profile when calling GetInternetConnectionProfile from an RDP
/// Child Session
///
[DispId(8)]
void SetSimulatedProfileInfo(in NLM_SIMULATED_PROFILE_INFO pSimulatedInfo);
///
/// Clears the connection profile values previously applied to the internet connection profile by SetSimulatedProfileInfo. The next internet
/// connection query, via GetInternetConnectionProfile, will use system information.
///
[DispId(9)]
void ClearSimulatedProfileInfo();
}
///
/// INetworkListManagerEvents is a message sink interface that a client implements to get overall machine state related events. Applications that are
/// interested on higher-level events, for example internet connectivity, implement this interface.
///
[ComImport, Guid("DCB00001-570F-4A9B-8D69-199FDBA5723B"), InterfaceType(ComInterfaceType.InterfaceIsIUnknown), TypeLibType(TypeLibTypeFlags.FOleAutomation)]
[PInvokeData("Netlistmgr.h")]
public interface INetworkListManagerEvents
{
/// Called when network connectivity related changes occur.
/// An NLM_CONNECTIVITY enumeration value that contains the new connectivity settings of the machine.
[PreserveSig]
HRESULT ConnectivityChanged([In] NLM_CONNECTIVITY newConnectivity);
}
/// The NetworkListManager class is the base CoClass for all interfaces.
[ComImport, Guid("DCB00C01-570F-4A9B-8D69-199FDBA5723B"), ClassInterface(ClassInterfaceType.None)]
[PInvokeData("Netlistmgr.h", MSDNShortId = "aa370769")]
public class NetworkListManager { }
/// Valid property names strings for use with IPropertyBag interface calls.
public static class NetworkPropertyName
{
/// Specifies that a domain network is not able to authenticate against the domain controller.
[CorrespondingType(typeof(bool))]
public const string NA_DomainAuthenticationFailed = "NA_DomainAuthenticationFailed";
/// Specifies the class of network.
[CorrespondingType(typeof(uint))]
public const string NA_NetworkClass = "NA_NetworkClass";
/// The name of the network has been set by group policy.
[CorrespondingType(typeof(bool))]
public const string NA_NameSetByPolicy = "NA_NameSetByPolicy";
/// The icon of the network has been set by group policy.
[CorrespondingType(typeof(bool))]
public const string NA_IconSetByPolicy = "NA_IconSetByPolicy";
/// The description of the network has been set by group policy.
[CorrespondingType(typeof(bool))]
public const string NA_DescriptionSetByPolicy = "NA_DescriptionSetByPolicy";
/// The category of the network has been set by group policy.
[CorrespondingType(typeof(bool))]
public const string NA_CategorySetByPolicy = "NA_CategorySetByPolicy";
/// The name of the network is read only.
[CorrespondingType(typeof(bool))]
public const string NA_NameReadOnly = "NA_NameReadOnly";
/// The icon of the network is read only.
[CorrespondingType(typeof(bool))]
public const string NA_IconReadOnly = "NA_IconReadOnly";
/// The description of the network is read only.
[CorrespondingType(typeof(bool))]
public const string NA_DescriptionReadOnly = "NA_DescriptionReadOnly";
/// The category of the network is read only.
[CorrespondingType(typeof(bool))]
public const string NA_CategoryReadOnly = "NA_CategoryReadOnly";
/// The network can be merged with another network.
[CorrespondingType(typeof(bool))]
public const string NA_AllowMerge = "NA_AllowMerge";
/// Provides details regarding IPv4 or IPv6 network connectivity.
[CorrespondingType(typeof(uint))]
public const string NA_InternetConnectivityV4 = "NA_InternetConnectivityV4";
/// Provides details regarding IPv4 or IPv6 network connectivity.
[CorrespondingType(typeof(uint))]
public const string NA_InternetConnectivityV6 = "NA_InternetConnectivityV6";
}
/// The NLM_DATAPLAN_STATUS structure stores the current data plan status information supplied by the carrier.
[StructLayout(LayoutKind.Sequential, Pack = 4)]
[PInvokeData("Netlistmgr.h", MSDNShortId = "hh448265")]
public struct NLM_DATAPLAN_STATUS
{
/// Default value for unavailable field in data plan status structure.
public const uint NLM_UNKNOWN_DATAPLAN_STATUS = 0xFFFFFFFF;
///
/// The unique ID of the interface associated with the data plan. This GUID is determined by the system when a data plan is first used by a system connection.
///
public Guid InterfaceGuid;
///
/// An NLM_USAGE_DATA structure containing current data usage value expressed in megabytes, as well as the system time at the moment this value was
/// last synced.
///
/// If this value is not supplied, NLM_USAGE_DATA will indicate NLM_UNKNOWN_DATAPLAN_STATUS for UsageInMegabytes and a value of '0' will be set for LastSyncTime.
///
///
public NLM_USAGE_DATA UsageData;
///
/// The data plan usage limit expressed in megabytes. If this value is not supplied, a default value of NLM_UNKNOWN_DATAPLAN_STATUS is set.
///
public uint DataLimitInMegabytes;
///
/// The maximum inbound connection bandwidth expressed in kbps. If this value is not supplied, a default value of NLM_UNKNOWN_DATAPLAN_STATUS is set.
///
public uint InboundBandwidthInKbps;
///
/// The maximum outbound connection bandwidth expressed in kbps. If this value is not supplied, a default value of NLM_UNKNOWN_DATAPLAN_STATUS is set.
///
public uint OutboundBandwidthInKbps;
/// The start time of the next billing cycle. If this value is not supplied, a default value of '0' is set.
public FILETIME NextBillingCycle;
///
/// The maximum suggested transfer size for this network expressed in megabytes. If this value is not supplied, a default value of
/// NLM_UNKNOWN_DATAPLAN_STATUS is set.
///
public uint MaxTransferSizeInMegabytes;
/// Reserved for future use.
public uint Reserved;
}
///
/// Used to specify values that are used by SetSimulatedProfileInfo to override current internet connection profile values in an RDP Child Session to
/// support the simulation of specific metered internet connection conditions.
///
[StructLayout(LayoutKind.Sequential, Pack = 4, CharSet = CharSet.Unicode)]
[PInvokeData("Netlistmgr.h", MSDNShortId = "dn280985")]
public struct NLM_SIMULATED_PROFILE_INFO
{
/// Name for the simulated profile.
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 256)] public string ProfileName;
/// The network cost.
public NLM_CONNECTION_COST cost;
/// The data usage.
public uint UsageInMegabytes;
/// The data limit of the plan.
public uint DataLimitInMegabytes;
}
/// The structure contains the IPv4/IPv6 destination address.
[StructLayout(LayoutKind.Sequential, Pack = 1)]
[PInvokeData("Netlistmgr.h", MSDNShortId = "hh448266")]
public sealed class NLM_SOCKADDR
{
private const int dataSize = 128;
/// An IPv4/IPv6 destination address.
[MarshalAs(UnmanagedType.ByValArray, SizeConst = dataSize)] public byte[] data = new byte[dataSize];
/// Creates a from an instance.
/// The IP address to encapsulate.
/// A instance with its data field set to either the IPv4 or IPv6 address supplied by .
public static NLM_SOCKADDR FromIPAddress(IPAddress address)
{
const ushort AF_INET = 2;
const ushort AF_INET6 = 23;
if (address == null) return null;
var sockAddr = new NLM_SOCKADDR { data = new byte[dataSize] };
// Seems to be compatible with SOCKADDR_STORAGE, which in turn is compatible with SOCKADDR_IN and SOCKADDR_IN6
using (var writer = new BinaryWriter(new MemoryStream(sockAddr.data)))
{
if (address.AddressFamily == AddressFamily.InterNetwork)
{
// AF_INT
writer.Write(AF_INET);
// Port
writer.Write((ushort)0);
// Flow Info
writer.Write((uint)0);
// Address
writer.Write(address.GetAddressBytes());
}
else
{
// AF_INT6
writer.Write(AF_INET6);
// Port
writer.Write((ushort)0);
// Flow Info
writer.Write((uint)0);
// Address
writer.Write(address.GetAddressBytes());
// Scope ID
writer.Write((ulong)address.ScopeId);
}
}
return sockAddr;
}
}
/// The NLM_USAGE_DATA structure stores information that indicates the data usage of a plan.
[StructLayout(LayoutKind.Sequential, Pack = 4)]
[PInvokeData("Netlistmgr.h", MSDNShortId = "hh448268")]
public struct NLM_USAGE_DATA
{
/// The data usage of a plan, represented in megabytes.
public uint UsageInMegabytes;
/// The timestamp of last time synced with carriers about the data usage stored in this structure.
public FILETIME LastSyncTime;
}
#if NETSTANDARD2_0
[Serializable, Flags, ComVisible(true)]
internal enum TypeLibTypeFlags
{
FAggregatable = 0x400,
FAppObject = 1,
FCanCreate = 2,
FControl = 0x20,
FDispatchable = 0x1000,
FDual = 0x40,
FHidden = 0x10,
FLicensed = 4,
FNonExtensible = 0x80,
FOleAutomation = 0x100,
FPreDeclId = 8,
FReplaceable = 0x800,
FRestricted = 0x200,
FReverseBind = 0x2000
}
[AttributeUsage(AttributeTargets.Interface | AttributeTargets.Enum | AttributeTargets.Struct | AttributeTargets.Class, Inherited = false), ComVisible(true)]
internal sealed class TypeLibTypeAttribute : Attribute
{
internal TypeLibTypeFlags _val;
public TypeLibTypeAttribute(short flags) => _val = (TypeLibTypeFlags)flags;
public TypeLibTypeAttribute(TypeLibTypeFlags flags) => _val = flags;
// Properties
public TypeLibTypeFlags Value => _val;
}
#endif
}