private void CreateInterfaceChangedObserver()
        {
            try
            {
                unsafe
                {
                    iObserver = InterfaceChanged;
                    iCallout  = Marshal.GetFunctionPointerForDelegate(iObserver);
                    iContext  = new SCDynamicStoreContext();
                    iStore    = SCDynamicStoreCreate(IntPtr.Zero, kAddIPAddressListChangeCallbackSCF, iCallout, iContext);
                    if (iStore == IntPtr.Zero)
                    {
                        throw new ApplicationException("Failed to invoke SCDynamicStoreCreate");
                    }

                    iPattern = SCDynamicStoreKeyCreateNetworkServiceEntity(IntPtr.Zero, kSCDynamicStoreDomainState, kSCCompAnyRegex, kSCEntNetIPv4);
                    if (iPattern == IntPtr.Zero)
                    {
                        throw new ApplicationException("Failed to invoke SCDynamicStoreKeyCreateNetworkServiceEntity");
                    }

                    iPatternList = CFArrayCreate(IntPtr.Zero, new IntPtr[1] {
                        iPattern
                    }, 1, IntPtr.Zero);
                    if (iPatternList == IntPtr.Zero)
                    {
                        throw new ApplicationException("Failed to invoke CFArrayCreate");
                    }
                    bool setNotificationKeys = SCDynamicStoreSetNotificationKeys(iStore, IntPtr.Zero, iPatternList);
                    if (!setNotificationKeys)
                    {
                        throw new ApplicationException("Failed to invoke SCDynamicStoreSetNotificationKeys");
                    }
                    iRunLoopSource = SCDynamicStoreCreateRunLoopSource(IntPtr.Zero, iStore, 0);
                    if (iRunLoopSource == IntPtr.Zero)
                    {
                        throw new ApplicationException("Failed to invoke SCDynamicStoreCreateRunLoopSource");
                    }
                    iRunLoop = CFRunLoopGetMain();
                    if (iRunLoop == IntPtr.Zero)
                    {
                        throw new ApplicationException("Failed to invoke CFRunLoopGetMain");
                    }

                    IntPtr  bundle    = CFBundleGetBundleWithIdentifier(kBundleIdentifier);
                    IntPtr *stringRef = (IntPtr *)CFBundleGetDataPointerForName(bundle, kCFRunLoopCommonModes);
                    iModeString = *stringRef;

                    CFRunLoopAddSource(iRunLoop, iRunLoopSource, iModeString);
                    CFRelease(iPattern);
                    CFRelease(iPatternList);
                }
            }
            catch (Exception ex)
            {
                string message = "Error creating NetworkChangeWatcher: " + ex;
                UserLog.WriteLine(message);
                Trace.WriteLine(Trace.kCore, message);

                if (iPattern != IntPtr.Zero)
                {
                    CFRelease(iPattern);
                }

                if (iPatternList != IntPtr.Zero)
                {
                    CFRelease(iPatternList);
                }
                if (iStore != IntPtr.Zero)
                {
                    CFRelease(iStore);
                }
                if (iRunLoopSource != IntPtr.Zero)
                {
                    CFRelease(iRunLoopSource);
                }
            }
        }
 private static extern IntPtr SCDynamicStoreCreate(IntPtr allocator, IntPtr name, IntPtr callout, SCDynamicStoreContext context);