示例#1
0
        void Register()
        {
            if (_key == null)
            {
                return;
            }

            var watchSubTree = NotifyFilter.Has(RegistryNotifyFilter.WatchSubTree);
            var notifyFilter = NotifyFilter & ~RegistryNotifyFilter.WatchSubTree;

            var result = NativeMethods.AdvApi32.RegNotifyChangeKeyValue(_key.Handle, watchSubTree, notifyFilter, _waitEvent.SafeWaitHandle, true);

            if (NativeUtils.Failed(result))
            {
                throw new Win32Exception(result);
            }

            _registeredWaitHandle = ThreadPool.RegisterWaitForSingleObject(_waitEvent, _callback, null, -1, true);
        }
示例#2
0
        public static bool TryGetData <T>(this IDataObject dataObject, string format, out T data) where T : struct
        {
            var formatEtc = CreateFormatEtc(format, TYMED.TYMED_HGLOBAL);

            if (NativeUtils.Failed(dataObject.QueryGetData(ref formatEtc)))
            {
                data = default(T);
                return(false);
            }

            dataObject.GetData(ref formatEtc, out var medium);

            try
            {
                data = Marshal.PtrToStructure <T>(medium.unionmember);

                return(true);
            }
            finally
            {
                NativeMethods.Ole32.ReleaseStgMedium(ref medium);
            }
        }