From 3eccb98a509cb36dba03568242ab54ac6bb0a933 Mon Sep 17 00:00:00 2001 From: David Hall Date: Wed, 8 May 2019 18:58:46 -0600 Subject: [PATCH] Added CreateBindCtx method in ShellUtil and migrated existing code to use it --- PInvoke/Shell32/ShObjIdl.ShellUtil.cs | 21 +++++++++++++++++++++ Windows.Shell/ShellObjects/ShellItem.cs | 10 +++++----- 2 files changed, 26 insertions(+), 5 deletions(-) diff --git a/PInvoke/Shell32/ShObjIdl.ShellUtil.cs b/PInvoke/Shell32/ShObjIdl.ShellUtil.cs index c0780daf..280cb5fb 100644 --- a/PInvoke/Shell32/ShObjIdl.ShellUtil.cs +++ b/PInvoke/Shell32/ShObjIdl.ShellUtil.cs @@ -4,6 +4,8 @@ using System.Runtime.InteropServices; using System.Runtime.InteropServices.ComTypes; using System.Security; using System.Text; +using static Vanara.PInvoke.Ole32; +using BIND_OPTS = System.Runtime.InteropServices.ComTypes.BIND_OPTS; namespace Vanara.PInvoke { @@ -12,6 +14,25 @@ namespace Vanara.PInvoke /// Methods in this class will only work on Vista and above. public static class ShellUtil { + /// + /// + /// Represents flags that should be used when opening the file that contains the object identified by the moniker. + /// + /// + /// Indicates the amount of time (clock time in milliseconds) that the caller specified to complete the binding operation. + /// + /// Flags that control aspects of moniker binding operations. + public static IBindCtx CreateBindCtx(STGM openMode = STGM.STGM_READWRITE, TimeSpan timeout = default, BIND_FLAGS bindFlags = 0) + { + Ole32.CreateBindCtx(0, out var ctx).ThrowIfFailed(); + if (openMode != STGM.STGM_READWRITE || timeout != TimeSpan.Zero || bindFlags != 0) + { + var opts = new BIND_OPTS { cbStruct = Marshal.SizeOf(typeof(BIND_OPTS)), grfMode = (int)openMode, dwTickCountDeadline = (int)timeout.TotalMilliseconds, grfFlags = (int)bindFlags }; + ctx.SetBindOptions(ref opts); + } + return ctx; + } + /// Gets the KNOWNFOLDERID enum from a KNOWNFOLDERID Guid. /// The KNOWNFOLDERID Guid. /// The KNOWNFOLDERID enum. diff --git a/Windows.Shell/ShellObjects/ShellItem.cs b/Windows.Shell/ShellObjects/ShellItem.cs index a9fe34c1..9a5fbe96 100644 --- a/Windows.Shell/ShellObjects/ShellItem.cs +++ b/Windows.Shell/ShellObjects/ShellItem.cs @@ -336,7 +336,6 @@ namespace Vanara.Windows.Shell { internal static readonly bool IsMin7 = Environment.OSVersion.Version >= new Version(6, 1); internal static readonly bool IsMinVista = Environment.OSVersion.Version.Major >= 6; - internal static IBindCtx iBindCtx; internal IShellItem iShellItem; internal IShellItem2 iShellItem2; private static Dictionary bhidLookup; @@ -470,7 +469,7 @@ namespace Vanara.Windows.Shell /// Gets the system bind context. /// The bind context. - protected static IBindCtx BindContext => iBindCtx ?? (iBindCtx = new BindContext(STGM.STGM_READWRITE | STGM.STGM_SHARE_DENY_NONE, TimeSpan.FromMilliseconds(500))); + protected static IBindCtx BindContext => ShellUtil.CreateBindCtx(); /// Creates the most specialized derivative of ShellItem from an IShellItem object. /// The IShellItem object. @@ -621,10 +620,11 @@ namespace Vanara.Windows.Shell /// Gets the stream of the file contents. /// Flags that should be used when opening the file. /// The file stream. - public ComStream GetStream(STGM mode = STGM.STGM_READWRITE | STGM.STGM_SHARE_EXCLUSIVE) + public ComStream GetStream(STGM mode = STGM.STGM_READWRITE) { - using (var ctx = new BindContext(mode)) - return new ComStream(GetHandler(ctx)); + //using (var ctx = new BindContext(mode)) + var ctx = ShellUtil.CreateBindCtx(mode); + return new ComStream(GetHandler(ctx)); } /// Gets the formatted tool tip text associated with this item.