StephaneLenclud@117: using System; StephaneLenclud@117: using System.Collections.Generic; StephaneLenclud@117: using System.Linq; StephaneLenclud@117: using System.Text; StephaneLenclud@117: using System.Threading.Tasks; StephaneLenclud@117: using System.Net.NetworkInformation; StephaneLenclud@117: using System.Runtime.InteropServices; StephaneLenclud@117: using System.Runtime.InteropServices.ComTypes; StephaneLenclud@117: using System.Diagnostics; StephaneLenclud@117: using NETWORKLIST; StephaneLenclud@117: StephaneLenclud@117: namespace SharpDisplayManager StephaneLenclud@117: { StephaneLenclud@117: public class NetworkManager: INetworkListManagerEvents, IDisposable StephaneLenclud@117: { StephaneLenclud@117: public delegate void OnConnectivityChangedDelegate(NetworkManager aNetworkManager, NLM_CONNECTIVITY aConnectivity); StephaneLenclud@117: public event OnConnectivityChangedDelegate OnConnectivityChanged; StephaneLenclud@117: StephaneLenclud@117: private int iCookie = 0; StephaneLenclud@117: private IConnectionPoint iConnectionPoint; StephaneLenclud@117: private INetworkListManager iNetworkListManager; StephaneLenclud@117: StephaneLenclud@117: StephaneLenclud@117: public NetworkManager() StephaneLenclud@117: { StephaneLenclud@117: iNetworkListManager = new NetworkListManager(); StephaneLenclud@117: ConnectToNetworkListManagerEvents(); StephaneLenclud@117: } StephaneLenclud@117: StephaneLenclud@117: public void Dispose() StephaneLenclud@117: { StephaneLenclud@117: //Not sure why this is not working form here StephaneLenclud@117: //Possibly because something is doing automatically before we get there StephaneLenclud@117: //DisconnectFromNetworkListManagerEvents(); StephaneLenclud@117: } StephaneLenclud@117: StephaneLenclud@117: StephaneLenclud@117: public INetworkListManager NetworkListManager StephaneLenclud@117: { StephaneLenclud@117: get { return iNetworkListManager; } StephaneLenclud@117: } StephaneLenclud@117: StephaneLenclud@117: public void ConnectivityChanged(NLM_CONNECTIVITY newConnectivity) StephaneLenclud@117: { StephaneLenclud@117: //Fire our event StephaneLenclud@117: OnConnectivityChanged(this, newConnectivity); StephaneLenclud@117: } StephaneLenclud@117: StephaneLenclud@117: public void ConnectToNetworkListManagerEvents() StephaneLenclud@117: { StephaneLenclud@117: Debug.WriteLine("Subscribing to INetworkListManagerEvents"); StephaneLenclud@117: IConnectionPointContainer icpc = (IConnectionPointContainer)iNetworkListManager; StephaneLenclud@117: //similar event subscription can be used for INetworkEvents and INetworkConnectionEvents StephaneLenclud@117: Guid tempGuid = typeof(INetworkListManagerEvents).GUID; StephaneLenclud@117: icpc.FindConnectionPoint(ref tempGuid, out iConnectionPoint); StephaneLenclud@117: iConnectionPoint.Advise(this, out iCookie); StephaneLenclud@117: StephaneLenclud@117: } StephaneLenclud@117: StephaneLenclud@117: public void DisconnectFromNetworkListManagerEvents() StephaneLenclud@117: { StephaneLenclud@117: Debug.WriteLine("Un-subscribing to INetworkListManagerEvents"); StephaneLenclud@117: iConnectionPoint.Unadvise(iCookie); StephaneLenclud@117: } StephaneLenclud@117: } StephaneLenclud@117: }