简体   繁体   中英

Error when programmatically adding a DeskBand

I'm trying to add a DeskBand object to the system tray programmatically using this code:

[ComImport, Guid("6D67E846-5B9C-4db8-9CBC-DDE12F4254F1"),
InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
public interface ITrayDeskband
{
    [PreserveSig]
    int ShowDeskBand([In, MarshalAs(UnmanagedType.Struct)] ref Guid clsid);
    [PreserveSig]
    int HideDeskBand([In, MarshalAs(UnmanagedType.Struct)] ref Guid clsid);
    [PreserveSig]
    int IsDeskBandShown([In, MarshalAs(UnmanagedType.Struct)] ref Guid clsid);
    [PreserveSig]
    int DeskBandRegistrationChanged();
} 

private void ShowDeskBand()
{
    ITrayDeskband obj = null;
    Type trayDeskbandType = System.Type.GetTypeFromCLSID(new Guid("E6442437-6C68-4f52-94DD-2CFED267EFB9"));
    try
    {
        Guid deskbandGuid = new Guid("FE0829F8-EDFA-46B6-87E0-636C8D953E33"); // My Deskband | TestToolbar

        obj = (ITrayDeskband)Activator.CreateInstance(trayDeskbandType);
        obj.DeskBandRegistrationChanged();

        int hr = obj.ShowDeskBand(ref deskbandGuid);

        if (hr != 0)
        {
            throw new Exception("Error while trying to show deskband: " + hr);
        }

        obj.DeskBandRegistrationChanged();
    }
    catch (Exception e)
    {
        MessageBox.Show(e.Message);
    }
    finally
    {
        if (obj != null && Marshal.IsComObject(obj))
            Marshal.ReleaseComObject(obj);
    }
}

Source: http://www.pinvoke.net/default.aspx/Interfaces/ITrayDeskband.html

My DeskBand object is created from the "BandObjectsLib" from this sample: http://www.codeproject.com/Articles/14141/Band-Objects-NET-2-0-Redux

It looks like this:

[Guid("FE0829F8-EDFA-46B6-87E0-636C8D953E33")]
[BandObject("TestToolbar", BandObjectStyle.Horizontal | BandObjectStyle.TaskbarToolBar, HelpText = "Testing this toolbar")]
public class TestToolbar : BandObject
{
    public TestToolbar()
    {
        ProgressBar pb = new ProgressBar();
        pb.Maximum = 100;
        pb.Minimum = 0;
        pb.Value = 50;

        this.Controls.AddRange(new System.Windows.Forms.Control[] { pb });
    }
}

However, when trying to add the DeskBand object all I get is my exception telling me the error code: -2147467259 This error code translates to a 0x80004005 and according to MSDN that is an E_FAIL: http://msdn.microsoft.com/en-us/library/windows/desktop/aa378137.aspx

I'm basicly stuck here since this is honestly not my area of expertise. I was hoping that there was someone who might be more experienced and could help me out? Thanks in advance!

您必须注册DLL(安装MSI或使用regasm)才能使代码正常工作。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM