COM Add-in toolbar click events only fire once if window is initialized

C

Carlos

This is a very bizare problem. So, I have a C# COM Add-in for Excel
which creates a toolbar, then pops-up meaningful windows when you
click the toolbar buttons. Everything works fine the first time.
After that, my event handlers do not get called when I click the
toolbar handlers. I've isolated the problem down to initializing a
dialog window (never actually showing the window). Explicitely, the
VisualStudio IDE is initializing the first component, failing on:

this.instructionLabel.Location = ((System.Drawing.Point)
(resources.GetObject("instructionLabel.Location")));

in InitializeComponent(). If I comment out the rest of the
initialization code from this line to "this.ResumeLayout(false);", the
event will fire every time. Note that there are plenty of lines
before this with plenty of equally 'severe' calls:

System.Resources.ResourceManager resources = new
System.Resources.ResourceManager(typeof(AuthenticationPopup));
this.instructionLabel = new System.Windows.Forms.Label();
this.userIdLabel = new System.Windows.Forms.Label();
...

this.SuspendLayout();
...

this.instructionLabel.Anchor = ((System.Windows.Forms.AnchorStyles)
(resources.GetObject("instructionLabel.Anchor")));
...

Unfortunately, this isn't a clear relationship, as I can't duplicate
it in a simple sample app where I create the add-in, toolbar, form
(localized), and one simple label. Other things happening:
- If I comment out all of the form stuff, there is also a networking
class that basically does XML-RPC http client calls that causes the
same problem. Commenting out the 2 together allows for multiple
firings.
- I was previously developing this app as a simple COM interop
application launching it from some Excel VB code. This issue wasn't
present in that mode.


Thanks,
Carlos
versions: .NET 1.1, OfficeXP, VisualStudio.NET 2003 (latest service
packs)
 
C

Carlos

Problem solved thanks to one of my brilliant collegues. Turns out
that the sample app makes the seemingly innocuous action of
maintaining a reference to the toolbar button once it is created. If
this is not done (as was the case in my application), the C#/.NET
structures get garbage collected. The thing is COM is supposed to
maintain the same sort of reference counting (the ugly thing with COM
is that you have to do it all yourself if I remember correctly). This
suggests that it's office that only creates the COM programming
structures when asked, but maintains only Excel/VBE structures
independently. It's ugly, but important to understand.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top