Added CreateBindCtx method in ShellUtil and migrated existing code to use it

pull/60/head
David Hall 2019-05-08 18:58:46 -06:00
parent e3ae1b5b55
commit 3eccb98a50
2 changed files with 26 additions and 5 deletions

View File

@ -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
/// <remarks>Methods in this class will only work on Vista and above.</remarks>
public static class ShellUtil
{
/// <summary></summary>
/// <param name="openMode">
/// Represents flags that should be used when opening the file that contains the object identified by the moniker.
/// </param>
/// <param name="timeout">
/// Indicates the amount of time (clock time in milliseconds) that the caller specified to complete the binding operation.
/// </param>
/// <param name="bindFlags">Flags that control aspects of moniker binding operations.</param>
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;
}
/// <summary>Gets the KNOWNFOLDERID enum from a KNOWNFOLDERID Guid.</summary>
/// <param name="knownFolder">The KNOWNFOLDERID Guid.</param>
/// <returns>The KNOWNFOLDERID enum.</returns>

View File

@ -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<Type, BHID> bhidLookup;
@ -470,7 +469,7 @@ namespace Vanara.Windows.Shell
/// <summary>Gets the system bind context.</summary>
/// <value>The bind context.</value>
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();
/// <summary>Creates the most specialized derivative of ShellItem from an IShellItem object.</summary>
/// <param name="iItem">The IShellItem object.</param>
@ -621,10 +620,11 @@ namespace Vanara.Windows.Shell
/// <summary>Gets the stream of the file contents.</summary>
/// <param name="mode">Flags that should be used when opening the file.</param>
/// <returns>The file stream.</returns>
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<IStream>(ctx));
//using (var ctx = new BindContext(mode))
var ctx = ShellUtil.CreateBindCtx(mode);
return new ComStream(GetHandler<IStream>(ctx));
}
/// <summary>Gets the formatted tool tip text associated with this item.</summary>