title: IDeskBand2 implementation crashes
body: <p>I have created a sample template for a future project that implements IDeskBand2.</p>
  
  <p>The registration with <strong>gacuti</strong> and <strong>regasm</strong> works, it is also displayed in the toolbar. If I want to display / execute it, the explorer crashes.</p>
  
  <p>For the creation I used the documentation of Mircosoft and pinvoke.</p>
  
  <p>I compared my project with <a href="https://github.com/navhaxs/media-control-deskband/blob/master/README.md" rel="nofollow noreferrer">media-control-deskband</a> and tried some things (COMInterop file in my project, compare code etc.)
  But can't find the cause, guess it's because of a faulty implementation.</p>
  
  <p>Here's my implementation.</p>
  
  <p>IDeskBand2 Interface:</p>
  
  <pre><code>/// &lt;summary&gt;
  /// Gets information about a band object.
  /// &lt;/summary&gt;
  [ComImport]
  [InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
  [Guid("EB0FE172-1A3A-11D0-89B3-00A0C90A90AC")]
  public interface IDeskBand : IDockingWindow
  {
      /// &lt;summary&gt;
      /// Gets state information for a band object.
      /// &lt;/summary&gt;
      /// &lt;param name="dwBandID"&gt;The identifier of the band, assigned by the container. The band object can retain this value if it is required.&lt;/param&gt;
      /// &lt;param name="dwViewMode"&gt;The view mode of the band object. One of the following values: DBIF_VIEWMODE_NORMAL, DBIF_VIEWMODE_VERTICAL, DBIF_VIEWMODE_FLOATING, DBIF_VIEWMODE_TRANSPARENT.&lt;/param&gt;
      /// &lt;param name="pdbi"&gt;Pointer to a DESKBANDINFO structure that receives the band information for the object. The dwMask member of this structure indicates the specific information that is being requested.&lt;/param&gt;
      /// &lt;returns&gt;&lt;/returns&gt;
      [PreserveSig]
      int GetBandInfo(UInt32 dwBandID, DeskBandInfoViewMode dwViewMode, ref DESKBANDINFO pdbi);
  }
  
  
  /// &lt;summary&gt;
  /// Exposes methods to enable and query translucency effects in a deskband object.
  /// &lt;/summary&gt;
  [ComImport]
  [InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
  [Guid("79D16DE4-ABEE-4021-8D9D-9169B261D657")]
  public interface IDeskBand2 : IDeskBand
  {
      /// &lt;summary&gt;
      /// Indicates the deskband's ability to be displayed as translucent.
      /// &lt;/summary&gt;
      /// &lt;param name="pfCanRenderComposited"&gt;When this method returns, contains a BOOL indicating ability.&lt;/param&gt;
      /// &lt;returns&gt;If this method succeeds, it returns S_OK. Otherwise, it returns an HRESULT error code.&lt;/returns&gt;
      [PreserveSig]
      int CanRenderComposited(out bool pfCanRenderComposited);
  
      /// &lt;summary&gt;
      /// Gets the composition state.
      /// &lt;/summary&gt;
      /// &lt;param name="pfCompositionEnabled"&gt;When this method returns, contains a BOOL that indicates state.&lt;/param&gt;
      /// &lt;returns&gt;If this method succeeds, it returns S_OK. Otherwise, it returns an HRESULT error code.&lt;/returns&gt;
      [PreserveSig]
      int GetCompositionState(out bool pfCompositionEnabled);
  
      /// &lt;summary&gt;
      /// Sets the composition state.
      /// &lt;/summary&gt;
      /// &lt;param name="fCompositionEnabled"&gt;TRUE to enable the composition state; otherwise, FALSE.&lt;/param&gt;
      /// &lt;returns&gt;If this method succeeds, it returns S_OK. Otherwise, it returns an HRESULT error code.&lt;/returns&gt;
      [PreserveSig]
      int SetCompositionState(bool fCompositionEnabled);
  }
  </code></pre>
  
  <p>IDeskBand2 in use: (<strong>GetBandInfo</strong> function is tested and works correctly.)</p>
  
  <pre><code>    /// &lt;summary&gt;
  /// Basic class for a DeskBand object
  /// &lt;/summary&gt;
  /// &lt;example&gt;
  /// [Guid("00000000-0000-0000-0000-000000000000")]
  /// [DeskBandInfo("Beispiel Erweiterung", "Diese ist eine Beispiel Erweiterung für die Taskleiste.")]
  /// public class SampleExtension : DeskBand
  /// { /*...*/ }
  /// &lt;/example&gt;
  public class DeskBand : UserControl, IDeskBand2
  {
      #region Constants
  
      private const int S_OK = 0;
      private const int E_NOTIMPL = unchecked((int)0x80004001);
  
      #endregion
  
      #region Properties
  
      /// &lt;summary&gt;
      /// Title of the band object, displayed by default on the left or top of the object.
      /// &lt;/summary&gt;
      [Browsable(true)]
      [DefaultValue("")]
      public String Title { get; set; }
  
      /// &lt;summary&gt;
      /// Minimum size of the band object. Default value of -1 sets no minimum constraint.
      /// &lt;/summary&gt;
      [Browsable(true)]
      [DefaultValue(typeof(Size), "-1,-1")]
      public Size MinSize { get; set; }
  
      /// &lt;summary&gt;
      /// Maximum size of the band object. Default value of -1 sets no maximum constraint.
      /// &lt;/summary&gt;
      [Browsable(true)]
      [DefaultValue(typeof(Size), "-1,-1")]
      public Size MaxSize { get; set; }
  
      /// &lt;summary&gt;
      /// Minimum vertical size of the band object. Default value of -1 sets no maximum constraint. (Used when the taskbar is aligned horinzortal.)
      /// &lt;/summary&gt;
      [Browsable(true)]
      [DefaultValue(typeof(Size), "-1,-1")]
      public Size MinSizeVertical { get; set; }
  
      /// &lt;summary&gt;
      /// Says that band object's size must be multiple of this size. Defauilt value of -1 does not set this constraint.
      /// &lt;/summary&gt;
      [Browsable(true)]
      [DefaultValue(typeof(Size), "-1,-1")]
      public Size IntegralSize { get; set; }
  
      #endregion
  
      #region IDeskBand2
  
      public virtual int CanRenderComposited(out bool pfCanRenderComposited)
      {
          pfCanRenderComposited = true;
          return S_OK;
      }
  
      public int GetCompositionState(out bool pfCompositionEnabled)
      {
          pfCompositionEnabled = false;
          return S_OK;
      }
  
      public int SetCompositionState(bool fCompositionEnabled)
      {
          fCompositionEnabled = true;
          return S_OK;
      }
  
      public int GetBandInfo(uint dwBandID, DeskBandInfoViewMode dwViewMode, ref DESKBANDINFO pdbi)
      {
          if (pdbi.dwMask.HasFlag(DeskBandInfoMasks.DBIM_MINSIZE))
          {
              // Support for a vertical taskbar
              // Most examples have no support for a vertical taskbar. Who in hell uses their taskbar vertically? Me! Very practical on a 21:9 monitor.
              if (dwViewMode.HasFlag(DeskBandInfoViewMode.DBIF_VIEWMODE_FLOATING) || dwViewMode.HasFlag(DeskBandInfoViewMode.DBIF_VIEWMODE_VERTICAL))
              {
                  pdbi.ptMinSize.Y = this.MinSizeVertical.Width;
                  pdbi.ptMinSize.X = this.MinSizeVertical.Height;
              }
              else
              {
                  pdbi.ptMinSize.X = this.MinSize.Width;
                  pdbi.ptMinSize.Y = this.MinSize.Height;
              }
          }
          if (pdbi.dwMask.HasFlag(DeskBandInfoMasks.DBIM_MAXSIZE))
          {
              if (dwViewMode.HasFlag(DeskBandInfoViewMode.DBIF_VIEWMODE_FLOATING) || dwViewMode.HasFlag(DeskBandInfoViewMode.DBIF_VIEWMODE_VERTICAL))
              {
                  pdbi.ptMaxSize.Y = this.MaxSize.Width;
                  pdbi.ptMaxSize.X = this.MaxSize.Height;
              }
              else
              {
                  pdbi.ptMaxSize.X = this.MaxSize.Width;
                  pdbi.ptMaxSize.Y = this.MaxSize.Height;
              }
          }
          if (pdbi.dwMask.HasFlag(DeskBandInfoMasks.DBIM_INTEGRAL))
          {
              if (dwViewMode.HasFlag(DeskBandInfoViewMode.DBIF_VIEWMODE_FLOATING) || dwViewMode.HasFlag(DeskBandInfoViewMode.DBIF_VIEWMODE_VERTICAL))
              {
                  pdbi.ptIntegral.Y = this.IntegralSize.Width;
                  pdbi.ptIntegral.X = this.IntegralSize.Height;
              }
              else
              {
                  pdbi.ptIntegral.X = this.IntegralSize.Width;
                  pdbi.ptIntegral.Y = this.IntegralSize.Height;
              }
          }
  
          if (pdbi.dwMask.HasFlag(DeskBandInfoMasks.DBIM_ACTUAL))
          {
              if (dwViewMode.HasFlag(DeskBandInfoViewMode.DBIF_VIEWMODE_FLOATING) || dwViewMode.HasFlag(DeskBandInfoViewMode.DBIF_VIEWMODE_VERTICAL))
              {
                  pdbi.ptActual.Y = this.Size.Width;
                  pdbi.ptActual.X = this.Size.Height;
              }
              else
              {
                  pdbi.ptActual.X = this.Size.Width;
                  pdbi.ptActual.Y = this.Size.Height;
              }
          }
  
          if (pdbi.dwMask.HasFlag(DeskBandInfoMasks.DBIM_TITLE))
          {
              pdbi.wszTitle = this.Title;
          }
  
          pdbi.dwModeFlags = DeskBandInfoModeFlag.DBIMF_ALWAYSGRIPPER | DeskBandInfoModeFlag.DBIMF_NORMAL | DeskBandInfoModeFlag.DBIMF_VARIABLEHEIGHT;
          pdbi.dwMask = pdbi.dwMask | DeskBandInfoMasks.DBIM_BKCOLOR | DeskBandInfoMasks.DBIM_TITLE; // Testen
  
          return S_OK;
      }
  
      public int CloseDW([In] uint dwReserved)
      {
          Dispose(true);
          return S_OK;
      }
  
      public int ResizeBorderDW(IntPtr prcBorder, [In, MarshalAs(UnmanagedType.IUnknown)] object punkToolbarSite, bool fReserved)
      {
          return E_NOTIMPL;
      }
  
      public int ShowDW([In] bool fShow)
      {
          if (fShow)
              Show();
          else
              Hide();
  
          return S_OK;
      }
  
      public int GetWindow(out IntPtr phwnd)
      {
          phwnd = Handle;
          return S_OK;
      }
  
      public int ContextSensitiveHelp(bool fEnterMode)
      {
          return S_OK;
      }
  
      #endregion
  
      #region COM
  
      [ComRegisterFunctionAttribute]
      public static void Register(Type t)
      {
          string guid = t.GUID.ToString("B");
  
          RegistryKey rkClass = Registry.ClassesRoot.CreateSubKey(@"CLSID\" + guid);
          RegistryKey rkCat = rkClass.CreateSubKey("Implemented Categories");
  
          DeskBandInfoAttribute[] deskBandInfo = (DeskBandInfoAttribute[])
              t.GetCustomAttributes(typeof(DeskBandInfoAttribute), false);
  
          string _displayName = t.Name;
          string _helpText = t.Name;
  
          if (deskBandInfo.Length == 1)
          {
              if (deskBandInfo[0].DisplayName != null)
              {
                  _displayName = deskBandInfo[0].DisplayName;
              }
  
              if (deskBandInfo[0].HelpText != null)
              {
                  _helpText = deskBandInfo[0].HelpText;
              }
          }
  
          rkClass.SetValue(null, _displayName);
          rkClass.SetValue("MenuText", _displayName);
          rkClass.SetValue("HelpText", _helpText);
  
          // TaskBar
          rkCat.CreateSubKey("{00021492-0000-0000-C000-000000000046}");
      }
  
      [ComUnregisterFunctionAttribute]
      public static void Unregister(Type t)
      {
          string guid = t.GUID.ToString("B");
  
          DeskBandInfoAttribute[] deskBandInfo = (DeskBandInfoAttribute[])
              t.GetCustomAttributes(typeof(DeskBandInfoAttribute), false);
  
          Registry.ClassesRoot.CreateSubKey(@"CLSID").DeleteSubKeyTree(guid);
      }
  
      #endregion
  
      public DeskBand()
      {
          InitializeComponent();
      }
  
      private void InitializeComponent()
      {
          this.Name = "DeskBand";
      }
  }
  }
  </code></pre>
  
  <p>The actual extension without Designer Code:</p>
  
  <pre><code>namespace TaskbarSampleExt
  {
      [Guid("2D53D9CC-0288-4511-A387-B330044C63EA")]
      [DeskBandInfo("Beispiel Erweiterung", "Diese ist eine Beispiel Erweiterung für die Taskleiste.")]
      public partial class SampleExtension : DeskBand
      {
  
          public SampleExtension()
          {
              this.MinSize = new Size(90, 40);
              this.MinSizeVertical = new Size(90, 40);
              this.Title = "Beispiel Erweiterung";
  
              InitializeComponent();
          }
  
      }
  }
  </code></pre>
  
  <p>Thank you for your time</p>
  
  <p><a href="https://share.bec-wolke.de/?datei=VGFza2JhclNhbXBsZUV4dC0wMi0wOS0yMDE3LnppcA==" rel="nofollow noreferrer">Brocken files</a></p>