示例#1
0
        /// <summary>
        /// Release the native and managed objects
        /// </summary>
        /// <param name="disposing">Indicates that this is being called from Dispose(), rather than the finalizer.</param>
        protected virtual void Dispose(bool disposing)
        {
            if (disposing)
            {
                internalName        = null;
                internalParsingName = null;
                properties          = null;
                thumbnail           = null;
                parentShellObject   = null;
            }

            if (internalPIDL != IntPtr.Zero)
            {
                ShellNativeMethods.ILFree(internalPIDL);
                internalPIDL = IntPtr.Zero;
            }

            if (nativeShellItem != null)
            {
                Marshal.ReleaseComObject(nativeShellItem);
                nativeShellItem = null;
            }

            if (NativePropertyStore != null)
            {
                Marshal.ReleaseComObject(NativePropertyStore);
                NativePropertyStore = null;
            }
        }
示例#2
0
        private static void ShowManageLibraryUI(ShellLibrary shellLibrary, IntPtr windowHandle, string title, string instruction, bool allowAllLocations)
        {
            int hr = 0;

            Thread staWorker = new Thread(() =>
            {
                hr = ShellNativeMethods.SHShowManageLibraryUI(
                    shellLibrary.NativeShellItem,
                    windowHandle,
                    title,
                    instruction,
                    allowAllLocations ?
                    ShellNativeMethods.LibraryManageDialogOptions.NonIndexableLocationWarning :
                    ShellNativeMethods.LibraryManageDialogOptions.Default);
            });

            staWorker.SetApartmentState(ApartmentState.STA);
            staWorker.Start();
            staWorker.Join();

            if (!CoreErrorHelper.Succeeded(hr))
            {
                throw new ShellException(hr);
            }
        }
示例#3
0
        /// <summary>
        /// Creates a shell library in a given local folder,
        /// using the given shell library name.
        /// </summary>
        /// <param name="libraryName">The name of this library</param>
        /// <param name="folderPath">The path to the local folder</param>
        /// <param name="overwrite">Override an existing library with the same name</param>
        public ShellLibrary(string libraryName, string folderPath, bool overwrite)
        {
            CoreHelpers.ThrowIfNotWin7();

            if (String.IsNullOrEmpty(libraryName))
            {
                throw new ArgumentNullException("libraryName", "libraryName cannot be empty.");
            }

            if (!Directory.Exists(folderPath))
            {
                throw new DirectoryNotFoundException("Folder path not found.");
            }

            this.Name = libraryName;

            ShellNativeMethods.LIBRARYSAVEFLAGS flags =
                overwrite ?
                ShellNativeMethods.LIBRARYSAVEFLAGS.LSF_OVERRIDEEXISTING :
                ShellNativeMethods.LIBRARYSAVEFLAGS.LSF_FAILIFTHERE;

            Guid guid = new Guid(ShellIIDGuid.IShellItem);

            IShellItem shellItemIn = null;

            ShellNativeMethods.SHCreateItemFromParsingName(folderPath, IntPtr.Zero, ref guid, out shellItemIn);

            nativeShellLibrary = (INativeShellLibrary) new ShellLibraryCoClass();
            nativeShellLibrary.Save(shellItemIn, libraryName, flags, out nativeShellItem);
        }
示例#4
0
        private static void ShowManageLibraryUI(ShellLibrary shellLibrary, IntPtr windowHandle, string title, string instruction, bool allowAllLocations)
        {
            int hr = 0;

            Thread staWorker = new Thread(() =>
            {
                hr = ShellNativeMethods.SHShowManageLibraryUI(
                    shellLibrary.NativeShellItem,
                    windowHandle,
                    title,
                    instruction,
                    allowAllLocations ?
                    ShellNativeMethods.LIBRARYMANAGEDIALOGOPTIONS.LMD_NOUNINDEXABLELOCATIONWARNING :
                    ShellNativeMethods.LIBRARYMANAGEDIALOGOPTIONS.LMD_DEFAULT);
            });

            staWorker.SetApartmentState(ApartmentState.STA);
            staWorker.Start();
            staWorker.Join();

            if (!CoreErrorHelper.Succeeded(hr))
            {
                Marshal.ThrowExceptionForHR(hr);
            }
        }
示例#5
0
        internal static IntPtr PidlFromUnknown(IntPtr unknown)
        {
            IntPtr pidl;
            int    retCode = ShellNativeMethods.SHGetIDListFromObject(unknown, out pidl);

            return(CoreErrorHelper.Succeeded(retCode) ? pidl : IntPtr.Zero);
        }
示例#6
0
        /// <summary>
        /// Start the watcher and begin receiving change notifications.
        /// <remarks>
        /// If the watcher is running, has no effect.
        /// Registration for notifications should be done before this is called.
        /// </remarks>
        /// </summary>
        public void Start()
        {
            if (Running)
            {
                return;
            }

            #region Registration
            ShellNativeMethods.SHChangeNotifyEntry entry = new ShellNativeMethods.SHChangeNotifyEntry {
                recursively = _recursive,

                pIdl = _shellObject.PIDL
            };

            _registrationId = ShellNativeMethods.SHChangeNotifyRegister(
                _listenerHandle,
                ShellNativeMethods.ShellChangeNotifyEventSource.ShellLevel | ShellNativeMethods.ShellChangeNotifyEventSource.InterruptLevel | ShellNativeMethods.ShellChangeNotifyEventSource.NewDelivery,
                _manager.RegisteredTypes, //ShellObjectChangeTypes.AllEventsMask,
                _message,
                1,
                ref entry);

            if (_registrationId == 0)
            {
                throw new Win32Exception(LocalizedMessages.ShellObjectWatcherRegisterFailed);
            }
            #endregion

            Running = true;
        }
示例#7
0
        /// <summary>
        /// Creates a shell library in a given local folder,
        /// using the given shell library name.
        /// </summary>
        /// <param name="libraryName">The name of this library</param>
        /// <param name="folderPath">The path to the local folder</param>
        /// <param name="overwrite">Override an existing library with the same name</param>
        public ShellLibrary(string libraryName, string folderPath, bool overwrite)
            : this()
        {
            if (string.IsNullOrEmpty(libraryName))
            {
                throw new ArgumentException(LocalizedMessages.ShellLibraryEmptyName, "libraryName");
            }

            if (!Directory.Exists(folderPath))
            {
                throw new DirectoryNotFoundException(LocalizedMessages.ShellLibraryFolderNotFound);
            }

            Name = libraryName;

            ShellNativeMethods.LibrarySaveOptions flags = overwrite ?
                                                          ShellNativeMethods.LibrarySaveOptions.OverrideExisting :
                                                          ShellNativeMethods.LibrarySaveOptions.FailIfThere;

            Guid guid = new Guid(ShellIIDGuid.IShellItem);

            ShellNativeMethods.SHCreateItemFromParsingName(folderPath, IntPtr.Zero, ref guid, out IShellItem shellItemIn);

            nativeShellLibrary = (INativeShellLibrary) new ShellLibraryCoClass();
            nativeShellLibrary.Save(shellItemIn, libraryName, flags, out nativeShellItem);
        }
        public static Bitmap CopyHBitmapToBitmap(IntPtr nativeHBitmap)
        {
            // Get width, height and the address of the pixel data for the native HBitmap
            BITMAP bitmapStruct = new BITMAP();

            ShellNativeMethods.GetObjectBitmap(nativeHBitmap, Marshal.SizeOf(bitmapStruct), ref bitmapStruct);

            // Create a managed bitmap that has its pixel data pointing to the pixel data of the native HBitmap
            // No memory is allocated for its pixel data
            Bitmap managedBitmapPointer = new Bitmap(
                bitmapStruct.bmWidth, bitmapStruct.bmHeight, bitmapStruct.bmWidth * 4, PixelFormat.Format32bppArgb, bitmapStruct.bmBits);

            // Create a managed bitmap and allocate memory for pixel data
            Bitmap managedBitmapReal = new Bitmap(bitmapStruct.bmWidth, bitmapStruct.bmHeight, PixelFormat.Format32bppArgb);

            // Copy the pixels of the native HBitmap into the canvas of the managed bitmap
            Graphics graphics = Graphics.FromImage(managedBitmapReal);

            graphics.DrawImage(managedBitmapPointer, 0, 0);
            graphics.Dispose();
            // Delete the native HBitmap object and free memory
            ShellNativeMethods.DeleteObject(nativeHBitmap);

            // Return the managed bitmap, clone of the native HBitmap, with correct transparency
            return(managedBitmapReal);
        }
示例#9
0
        /// <summary>
        /// Load the library using a number of options
        /// </summary>
        /// <param name="libraryName">The name of the library</param>
        /// <param name="isReadOnly">If <B>true</B>, loads the library in read-only mode.</param>
        /// <returns>A ShellLibrary Object</returns>
        public static ShellLibrary Load(string libraryName, bool isReadOnly)
        {
            CoreHelpers.ThrowIfNotWin7();

            IKnownFolder kf = KnownFolders.Libraries;
            string       librariesFolderPath = (kf != null) ? kf.Path : string.Empty;

            Guid   guid          = new Guid(ShellIIDGuid.IShellItem);
            string shellItemPath = System.IO.Path.Combine(librariesFolderPath, libraryName + FileExtension);
            int    hr            = ShellNativeMethods.SHCreateItemFromParsingName(shellItemPath, IntPtr.Zero, ref guid, out IShellItem nativeShellItem);

            if (!CoreErrorHelper.Succeeded(hr))
            {
                throw new ShellException(hr);
            }

            INativeShellLibrary nativeShellLibrary = (INativeShellLibrary) new ShellLibraryCoClass();
            AccessModes         flags = isReadOnly ?
                                        AccessModes.Read :
                                        AccessModes.ReadWrite;

            nativeShellLibrary.LoadLibraryFromItem(nativeShellItem, flags);

            ShellLibrary library = new ShellLibrary(nativeShellLibrary);

            try {
                library.nativeShellItem = (IShellItem2)nativeShellItem;
                library.Name            = libraryName;

                return(library);
            } catch {
                library.Dispose();
                throw;
            }
        }
        internal static IntPtr PidlFromParsingName(string name)
        {
            var retCode = ShellNativeMethods.SHParseDisplayName(
                name, IntPtr.Zero, out var pidl, 0,
                out var sfgao);

            return(CoreErrorHelper.Succeeded(retCode) ? pidl : IntPtr.Zero);
        }
示例#11
0
        /// <summary>
        /// Creates a ShellObjectCollection from an IDataObject passed during Drop operation.
        /// </summary>
        /// <param name="dataObject">An object that implements the IDataObject COM interface.</param>
        /// <returns>ShellObjectCollection created from the given IDataObject</returns>
        public static ShellObjectCollection FromDataObject(System.Runtime.InteropServices.ComTypes.IDataObject dataObject)
        {
            IShellItemArray shellItemArray;
            Guid            iid = new Guid(ShellIIDGuid.IShellItemArray);

            ShellNativeMethods.SHCreateShellItemArrayFromDataObject(dataObject, ref iid, out shellItemArray);
            return(new ShellObjectCollection(shellItemArray, true));
        }
示例#12
0
        public static ShellObjectCollection FromShellItem(IShellItem dataObject)
        {
            IShellItemArray shellItemArray;
            Guid            iid = new Guid(ShellIIDGuid.IShellItemArray);

            ShellNativeMethods.SHCreateShellItemArrayFromShellItem(dataObject, ref iid, out shellItemArray);
            return(new ShellObjectCollection(shellItemArray, true));
        }
示例#13
0
        internal static IntPtr PidlFromShellItem(IShellItem nativeShellItem)
        {
            IntPtr shellItem = Marshal.GetComInterfaceForObject(nativeShellItem, typeof(IShellItem));
            IntPtr pidl;

            int retCode = ShellNativeMethods.SHGetIDListFromObject(shellItem, out pidl);

            return(CoreErrorHelper.Succeeded(retCode) ? pidl : IntPtr.Zero);
        }
        /// <summary>
        /// Returns a known folder given its shell namespace parsing name, such as
        /// <c>::{645FF040-5081-101B-9F08-00AA002F954E}</c> for the Recycle Bin.
        /// </summary>
        /// <param name="parsingName">The parsing name (or path) for the requested known folder.</param>
        /// <returns>A known folder representing the specified name.</returns>
        /// <exception cref="System.ArgumentException">Thrown if the given parsing name is invalid.</exception>
        public static IKnownFolder FromParsingName(string parsingName)
        {
            if (parsingName == null)
            {
                throw new ArgumentNullException(nameof(parsingName));
            }

            IntPtr pidl  = IntPtr.Zero;
            IntPtr pidl2 = IntPtr.Zero;

            try
            {
                pidl = ShellHelper.PidlFromParsingName(parsingName);

                if (pidl == IntPtr.Zero)
                {
                    throw new ArgumentException(LocalizedMessages.KnownFolderParsingName, nameof(parsingName));
                }

                // It's probably a special folder, try to get it
                IKnownFolderNative knownFolderNative = KnownFolderHelper.FromPIDL(pidl);
                if (knownFolderNative != null)
                {
                    IKnownFolder kf = KnownFolderHelper.GetKnownFolder(knownFolderNative);
                    if (kf == null)
                    {
                        throw new ArgumentException(LocalizedMessages.KnownFolderParsingName, nameof(parsingName));
                    }

                    return(kf);
                }

                // No physical storage was found for this known folder
                // We'll try again with a different name

                // try one more time with a trailing \0
                pidl2 = ShellHelper.PidlFromParsingName(parsingName.PadRight(1, '\0'));

                if (pidl2 == IntPtr.Zero)
                {
                    throw new ArgumentException(LocalizedMessages.KnownFolderParsingName, nameof(parsingName));
                }

                IKnownFolder kf2 = KnownFolderHelper.GetKnownFolder(KnownFolderHelper.FromPIDL(pidl));
                if (kf2 == null)
                {
                    throw new ArgumentException(LocalizedMessages.KnownFolderParsingName, nameof(parsingName));
                }

                return(kf2);
            }
            finally
            {
                ShellNativeMethods.ILFree(pidl);
                ShellNativeMethods.ILFree(pidl2);
            }
        }
示例#15
0
        public ChangeNotifyLock(Message message)
        {
            IntPtr pidl;
            IntPtr lockId = ShellNativeMethods.SHChangeNotification_Lock(
                message.WParam, (int)message.LParam, out pidl, out _event);

            try
            {
                Trace.TraceInformation("Message: {0}", (ShellObjectChangeTypes)_event);

                var notifyStruct = pidl.MarshalAs <ShellNativeMethods.ShellNotifyStruct>();

                Guid guid = new Guid(ShellIIDGuid.IShellItem2);
                if (notifyStruct.item1 != IntPtr.Zero &&
                    (((ShellObjectChangeTypes)_event) & ShellObjectChangeTypes.SystemImageUpdate) == ShellObjectChangeTypes.None)
                {
                    IShellItem2 nativeShellItem;
                    if (CoreErrorHelper.Succeeded(ShellNativeMethods.SHCreateItemFromIDList(
                                                      notifyStruct.item1, ref guid, out nativeShellItem)))
                    {
                        string name;
                        nativeShellItem.GetDisplayName(ShellNativeMethods.ShellItemDesignNameOptions.FileSystemPath,
                                                       out name);
                        ItemName = name;

                        Trace.TraceInformation("Item1: {0}", ItemName);
                    }
                }
                else
                {
                    ImageIndex = notifyStruct.item1.ToInt32();
                }

                if (notifyStruct.item2 != IntPtr.Zero)
                {
                    IShellItem2 nativeShellItem;
                    if (CoreErrorHelper.Succeeded(ShellNativeMethods.SHCreateItemFromIDList(
                                                      notifyStruct.item2, ref guid, out nativeShellItem)))
                    {
                        string name;
                        nativeShellItem.GetDisplayName(ShellNativeMethods.ShellItemDesignNameOptions.FileSystemPath,
                                                       out name);
                        ItemName2 = name;

                        Trace.TraceInformation("Item2: {0}", ItemName2);
                    }
                }
            }
            finally
            {
                if (lockId != IntPtr.Zero)
                {
                    ShellNativeMethods.SHChangeNotification_Unlock(lockId);
                }
            }
        }
示例#16
0
        internal static IntPtr PidlFromParsingName(string name)
        {
            IntPtr pidl;

            ShellNativeMethods.ShellFileGetAttributesOptions sfgao;
            int retCode = ShellNativeMethods.SHParseDisplayName(
                name, IntPtr.Zero, out pidl, (ShellNativeMethods.ShellFileGetAttributesOptions) 0,
                out sfgao);

            return(CoreErrorHelper.Succeeded(retCode) ? pidl : IntPtr.Zero);
        }
示例#17
0
        private Bitmap GetBitmap(System.Windows.Size size)
        {
            IntPtr hBitmap = GetHBitmap(size);

            // return a System.Drawing.Bitmap from the hBitmap
            Bitmap returnValue = Bitmap.FromHbitmap(hBitmap);

            // delete HBitmap to avoid memory leaks
            ShellNativeMethods.DeleteObject(hBitmap);

            return(returnValue);
        }
        void TagManagedMenuItems(Menu menu, int tag)
        {
            MENUINFO info = new MENUINFO();

            info.cbSize     = Marshal.SizeOf(info);
            info.fMask      = MIM.MIM_MENUDATA;
            info.dwMenuData = tag;

            foreach (MenuItem item in menu.MenuItems)
            {
                ShellNativeMethods.SetMenuInfo(item.Handle, ref info);
            }
        }
示例#19
0
        private BitmapSource GetBitmapSource(System.Windows.Size size)
        {
            IntPtr hBitmap = GetHBitmap(size);

            // return a System.Media.Imaging.BitmapSource
            // Use interop to create a BitmapSource from hBitmap.
            BitmapSource returnValue = Imaging.CreateBitmapSourceFromHBitmap(hBitmap, IntPtr.Zero, System.Windows.Int32Rect.Empty, BitmapSizeOptions.FromEmptyOptions());

            // delete HBitmap to avoid memory leaks
            ShellNativeMethods.DeleteObject(hBitmap);

            return(returnValue);
        }
示例#20
0
 /// <summary>
 /// Stop the watcher and prevent further notifications from being received.
 /// <remarks>If the watcher is not running, this has no effect.</remarks>
 /// </summary>
 public void Stop()
 {
     if (!Running)
     {
         return;
     }
     if (_registrationId > 0)
     {
         ShellNativeMethods.SHChangeNotifyDeregister(_registrationId);
         _registrationId = 0;
     }
     Running = false;
 }
示例#21
0
        // This is a work around for the STA thread bug.  This will execute the call on a non-sta thread, then return the result
        private static bool IsVirtualKnownFolder(IShellItem2 nativeShellItem2)
        {
            IntPtr pidl = IntPtr.Zero;

            try
            {
                IKnownFolderNative nativeFolder = null;
                KnownFoldersSafeNativeMethods.NativeFolderDefinition definition = new KnownFoldersSafeNativeMethods.NativeFolderDefinition();

                // We found a bug where the enumeration of shell folders was
                // not reliable when called from a STA thread - it would return
                // different results the first time vs the other times.
                //
                // This is a work around.  We call FindFolderFromIDList on a
                // worker MTA thread instead of the main STA thread.
                //
                // Ultimately, it would be a very good idea to replace the 'getting shell object' logic
                // to get a list of pidl's in 1 step, then look up their information in a 2nd, rather than
                // looking them up as we get them.  This would replace the need for the work around.
                object padlock = new object();
                lock (padlock)
                {
                    IntPtr unknown = Marshal.GetIUnknownForObject(nativeShellItem2);

                    ThreadPool.QueueUserWorkItem(obj =>
                    {
                        lock (padlock)
                        {
                            pidl = ShellHelper.PidlFromUnknown(unknown);

                            new KnownFolderManagerClass().FindFolderFromIDList(pidl, out nativeFolder);

                            if (nativeFolder != null)
                            {
                                nativeFolder.GetFolderDefinition(out definition);
                            }

                            Monitor.Pulse(padlock);
                        }
                    });

                    Monitor.Wait(padlock);
                }

                return(nativeFolder != null && definition.category == FolderCategory.Virtual);
            }
            finally
            {
                ShellNativeMethods.ILFree(pidl);
            }
        }
        /// <summary>Constructs a new Shell object from IDList pointer</summary>
        /// <param name="idListPtr"></param>
        /// <param name="parent"></param>
        /// <returns></returns>
        internal static ShellObject Create(IntPtr idListPtr, ShellContainer parent)
        {
            var retCode = ShellNativeMethods.SHCreateShellItem(
                IntPtr.Zero,
                parent.NativeShellFolder,
                idListPtr, out var nativeShellItem);

            if (!CoreErrorHelper.Succeeded(retCode))
            {
                return(null);
            }

            return(ShellObjectFactory.Create(nativeShellItem));
        }
示例#23
0
        /// <summary>
        /// Enumerates through contents of the ShellObjectContainer
        /// </summary>
        /// <returns>Enumerated contents</returns>
        public IEnumerator <ShellObject> GetEnumerator()
        {
            if (NativeShellFolder == null)
            {
                if (desktopFolderEnumeration == null)
                {
                    ShellNativeMethods.SHGetDesktopFolder(out desktopFolderEnumeration);
                }

                nativeShellFolder = desktopFolderEnumeration;
            }

            return(new ShellFolderItems(this));
        }
示例#24
0
        private static IKnownFolderNative GetNativeKnownFolder(IShellItem nativeShellItem, out bool isVirtual)
        {
            IntPtr pidl = IntPtr.Zero;

            try
            {
                // Get the PIDL for the ShellItem
                pidl = ShellHelper.PidlFromShellItem(nativeShellItem);

                if (pidl == IntPtr.Zero)
                {
                    isVirtual = false;
                    return(null);
                }

                IKnownFolderNative knownFolderNative = KnownFolderHelper.FromPIDL(pidl);

                if (knownFolderNative != null)
                {
                    // If we have a valid IKnownFolder, try to get its category
                    KnownFoldersSafeNativeMethods.NativeFolderDefinition nativeFolderDefinition;
                    knownFolderNative.GetFolderDefinition(out nativeFolderDefinition);

                    // Get the category type and see if it's virtual
                    if (nativeFolderDefinition.category == FolderCategory.Virtual)
                    {
                        isVirtual = true;
                    }
                    else
                    {
                        isVirtual = false;
                    }

                    return(knownFolderNative);
                }
                else
                {
                    // KnownFolderHelper.FromPIDL could not create a valid KnownFolder from the given PIDL.
                    // Return null to indicate the given IShellItem is not a KnownFolder. Also set our out parameter
                    // to default value.
                    isVirtual = false;
                    return(null);
                }
            }
            finally
            {
                ShellNativeMethods.ILFree(pidl);
            }
        }
        /// <summary>Constructs a new Shell object from IDList pointer</summary>
        /// <param name="idListPtr"></param>
        /// <returns></returns>
        internal static ShellObject Create(IntPtr idListPtr)
        {
            // Throw exception if not running on Win7 or newer.
            CoreHelpers.ThrowIfNotVista();

            var guid = new Guid(ShellIIDGuid.IShellItem2);

            var retCode = ShellNativeMethods.SHCreateItemFromIDList(idListPtr, ref guid, out var nativeShellItem);

            if (!CoreErrorHelper.Succeeded(retCode))
            {
                return(null);
            }
            return(ShellObjectFactory.Create(nativeShellItem));
        }
示例#26
0
#pragma warning disable CS1591 // Missing XML comment for publicly visible type or member
        public IEnumerator <ShellObject> GetItems(ShellNativeMethods.ShellFolderEnumerationOptions shellFolderEnumerationOptions)
#pragma warning restore CS1591 // Missing XML comment for publicly visible type or member
        {
            if (NativeShellFolder == null)
            {
                if (desktopFolderEnumeration == null)
                {
                    ShellNativeMethods.SHGetDesktopFolder(out desktopFolderEnumeration);
                }

                nativeShellFolder = desktopFolderEnumeration;
            }

            return(new ShellFolderItems(this, shellFolderEnumerationOptions));
        }
        void RemoveShellMenuItems(Menu menu)
        {
            const int    tag      = 0xAB;
            List <int>   remove   = new List <int>();
            int          count    = ShellNativeMethods.GetMenuItemCount(menu.Handle);
            MENUINFO     menuInfo = new MENUINFO();
            MENUITEMINFO itemInfo = new MENUITEMINFO();

            menuInfo.cbSize = Marshal.SizeOf(menuInfo);
            menuInfo.fMask  = MIM.MIM_MENUDATA;
            itemInfo.cbSize = (uint)Marshal.SizeOf(itemInfo);
            itemInfo.fMask  = (uint)MIIM.MIIM_ID | (uint)MIIM.MIIM_SUBMENU;

            // First, tag the managed menu items with an arbitary
            // value (0xAB).
            TagManagedMenuItems(menu, tag);

            for (int n = 0; n < count; ++n)
            {
                ShellNativeMethods.GetMenuItemInfo(menu.Handle, n, true, ref itemInfo);

                if (itemInfo.hSubMenu == IntPtr.Zero)
                {
                    // If the item has no submenu we can't get the tag, so
                    // check its ID to determine if it was added by the shell.
                    if (itemInfo.wID >= m_CmdFirst)
                    {
                        remove.Add(n);
                    }
                }
                else
                {
                    ShellNativeMethods.GetMenuInfo(itemInfo.hSubMenu, ref menuInfo);
                    if (menuInfo.dwMenuData != tag)
                    {
                        remove.Add(n);
                    }
                }
            }

            // Remove the unmanaged menu items.
            remove.Reverse();
            foreach (int position in remove)
            {
                ShellNativeMethods.DeleteMenu(menu.Handle, position, MF.MF_BYPOSITION);
            }
        }
示例#28
0
        /// <summary>
        /// Creates a ShellObject given a parsing name
        /// </summary>
        /// <param name="parsingName"></param>
        /// <returns>A newly constructed ShellObject object</returns>
        internal static ShellObject Create(string parsingName)
        {
            if (string.IsNullOrEmpty(parsingName))
            {
                throw new ArgumentNullException("parsingName");
            }

            // Create a native shellitem from our path
            IShellItem2 nativeShellItem;
            Guid        guid    = new Guid(ShellIIDGuid.IShellItem2);
            int         retCode = ShellNativeMethods.SHCreateItemFromParsingName(parsingName, IntPtr.Zero, ref guid, out nativeShellItem);

            if (!CoreErrorHelper.Succeeded(retCode))
            {
                throw new ShellException(LocalizedMessages.ShellObjectFactoryUnableToCreateItem, Marshal.GetExceptionForHR(retCode));
            }
            return(ShellObjectFactory.Create(nativeShellItem));
        }
示例#29
0
 /// <summary>
 /// Returns the hash code of the object.
 /// </summary>
 /// <returns></returns>
 public override int GetHashCode( )
 {
     if (!hashValue.HasValue)
     {
         uint size = ShellNativeMethods.ILGetSize(PIDL);
         if (size != 0)
         {
             byte[] pidlData = new byte[size];
             Marshal.Copy(PIDL, pidlData, 0, (int)size);
             byte[] hashData = ShellObject.hashProvider.ComputeHash(pidlData);
             hashValue = BitConverter.ToInt32(hashData, 0);
         }
         else
         {
             hashValue = 0;
         }
     }
     return(hashValue.Value);
 }
示例#30
0
        /// <summary>
        /// Constructs a new Shell object from IDList pointer
        /// </summary>
        /// <param name="idListPtr"></param>
        /// <param name="parent"></param>
        /// <returns></returns>
        internal static ShellObject Create(IntPtr idListPtr, ShellContainer parent)
        {
            IShellItem nativeShellItem;

            int retCode = ShellNativeMethods.SHCreateShellItem(
                IntPtr.Zero,
                parent.NativeShellFolder,
                idListPtr, out nativeShellItem);

            if (CoreErrorHelper.Succeeded(retCode))
            {
                return(ShellObjectFactory.Create(nativeShellItem));
            }
            else
            {
                // Since this is an internal method, return null instead of throwing an exception.
                // Let the caller know we weren't able to create a valid ShellObject with the given PIDL
                return(null);
            }
        }
示例#31
0
 internal override ShellNativeMethods.FileOpenOptions GetDerivedOptionFlags(ShellNativeMethods.FileOpenOptions flags)
 {
     if (overwritePrompt)
     {
         flags |= ShellNativeMethods.FileOpenOptions.OverwritePrompt;
     }
     if (createPrompt)
     {
         flags |= ShellNativeMethods.FileOpenOptions.CreatePrompt;
     }
     if (!isExpandedMode)
     {
         flags |= ShellNativeMethods.FileOpenOptions.DefaultNoMiniMode;
     }
     if (alwaysAppendDefaultExtension)
     {
         flags |= ShellNativeMethods.FileOpenOptions.StrictFileTypes;
     }
     return flags;
 }
 public HResult GetAttributes(ShellNativeMethods.ShellItemAttributeOptions dwAttribFlags, ShellNativeMethods.ShellFileGetAttributesOptions sfgaoMask, out ShellNativeMethods.ShellFileGetAttributesOptions psfgaoAttribs)
 {
     throw new NotSupportedException();
 }
 public void OnOverwrite(IFileDialog pfd, IShellItem psi, out ShellNativeMethods.FDE_OVERWRITE_RESPONSE pResponse)
 {
     // Don't accept or reject the dialog, keep default settings
     pResponse = ShellNativeMethods.FDE_OVERWRITE_RESPONSE.FDEOR_DEFAULT;
 }
 internal abstract ShellNativeMethods.FileOpenOptions GetDerivedOptionFlags(ShellNativeMethods.FileOpenOptions flags);
 public void OnOverwrite(IFileDialog pfd, IShellItem psi, out ShellNativeMethods.FileDialogEventOverwriteResponse pResponse)
 {
     // Don't accept or reject the dialog, keep default settings
     pResponse = ShellNativeMethods.FileDialogEventOverwriteResponse.Default;
 }
 public void OnShareViolation(
     IFileDialog pfd,
     IShellItem psi,
     out ShellNativeMethods.FileDialogEventShareViolationResponse pResponse)
 {
     // Do nothing: we will ignore share violations, 
     // and don't register
     // for them, so this method should never be called.
     pResponse = ShellNativeMethods.FileDialogEventShareViolationResponse.Accept;
 }
 public void OnShareViolation(
     IFileDialog pfd,
     IShellItem psi,
     out ShellNativeMethods.FDE_SHAREVIOLATION_RESPONSE pResponse)
 {
     // Do nothing: we will ignore share violations,
     // and don't register
     // for them, so this method should never be called.
     pResponse = ShellNativeMethods.FDE_SHAREVIOLATION_RESPONSE.FDESVR_ACCEPT;
 }
 internal abstract ShellNativeMethods.FOS GetDerivedOptionFlags(ShellNativeMethods.FOS flags);
示例#39
0
 public HRESULT GetAttributes(ShellNativeMethods.SIATTRIBFLAGS dwAttribFlags, ShellNativeMethods.SFGAO sfgaoMask, out ShellNativeMethods.SFGAO psfgaoAttribs)
 {
     throw new NotSupportedException();
 }
        internal override ShellNativeMethods.FileOpenOptions GetDerivedOptionFlags(ShellNativeMethods.FileOpenOptions flags)
        {
            if (multiselect)
            {
                flags |= ShellNativeMethods.FileOpenOptions.AllowMultiSelect;
            }
            if (isFolderPicker)
            {
                flags |= ShellNativeMethods.FileOpenOptions.PickFolders;
            }
            
            if (!allowNonFileSystem)
            {
                flags |= ShellNativeMethods.FileOpenOptions.ForceFilesystem;
            }
            else if (allowNonFileSystem)
            {
                flags |= ShellNativeMethods.FileOpenOptions.AllNonStorageItems;
            }

            return flags;
        }
        internal override ShellNativeMethods.FOS GetDerivedOptionFlags(ShellNativeMethods.FOS flags)
        {
            if (multiselect)
                flags |= ShellNativeMethods.FOS.FOS_ALLOWMULTISELECT;
            if (isFolderPicker)
                flags |= ShellNativeMethods.FOS.FOS_PICKFOLDERS;
            if (!allowNonFileSystem)
                flags |= ShellNativeMethods.FOS.FOS_FORCEFILESYSTEM;
            if (allowNonFileSystem)
                flags |= ShellNativeMethods.FOS.FOS_ALLNONSTORAGEITEMS;

            return flags;
        }
 internal override ShellNativeMethods.FOS GetDerivedOptionFlags(ShellNativeMethods.FOS flags)
 {
     if (overwritePrompt)
         flags |= ShellNativeMethods.FOS.FOS_OVERWRITEPROMPT;
     if (createPrompt)
         flags |= ShellNativeMethods.FOS.FOS_CREATEPROMPT;
     if (!isExpandedMode)
         flags |= ShellNativeMethods.FOS.FOS_DEFAULTNOMINIMODE;
     if (alwaysAppendSelectedExtension)
         flags |= ShellNativeMethods.FOS.FOS_STRICTFILETYPES;
     return flags;
 }