Excel 2007 crashes after unloading Add-In
I've seem to run into an interesting problem with an Add-In that I originally wrote for Excel 2003 and below, and then tried to run with Excel 2007. The problem is that even though my Add-In works perfectly while Excel is running, when I close Excel, shortly after the Addin's are unloaded and the GUI goes away I get what appears to be a Null Reference exception.
The Add-In is written in ATL C++ and uses the IDispatchImpl and IDispEventSimpleImpl interfaces to communicate events with Excel.
I've tried to debug the code to find out what is happening, but there are no exceptions thrown anywhere in my code. I can go all the way to the point where Excel calls my OnDisconnection() and subsequently OnBeginShutdown() functions at which point I return S_OK. It is then, if I walk through some of the assembly code in Excel I get to a point where a NULL reference exception is encountered.
The only thing my Add-In does is create a new Command Bar and adds a button to it. By the process of commenting out all of the code inside my functions and then slowly enabling some I've found that the code that is somehow causing this problem is the code that creates my Command Bar and/or gets its position before the Add-In closes.
The code I use to create the command bar is fired in the OnStartupComplete() event of the IDispatch interface and does this:
//The collection of Command Bars used to add my new one
CComQIPtr<Office::_CommandBars> oCommandBars;
//Setup the application
//Note: m_pApplication is an LDispatch* that is given by Excel
//at StartupComplete()
CApplication spApp(m_pApplication);
//Get the collection of command bars
oCommandBars = spApp.get_CommandBars();
//Now add a new toolband to which we'll add 2 buttons
CComVariant vName("MyBar");
CComVariant vEmpty(DISP_E_PARAMNOTFOUND, VT_ERROR);
CComVariant vVisible(VARIANT_TRUE);
CComQIPtr<Office::CommandBar> oMyBar;
//Loop through the collection of command bars and see if our bar
//already exists
BSTR tmp;
int count;
oCommandBars->get_Count(&count);
for(int i = 1; i < count; i++)
{
CComVariant vIndex(i);
oCommandBars->get_Item(vIndex, &oMyBar);
oMyBar->get_Name(&tmp);
if(strcmp(ConvertBSTRToString(tmp), "MyBar"))
{
oMyBar.Detach();
}
else
break;
}
//free the string
::SysFreeString(tmp);
//If we didn't find the bar already there, create it
if(!oMyBar)
{
//position it below all toolbands
//MsoBarPosition::msoBarTop = 1
CComVariant vPos(1);
CComVariant vTemp(VARIANT_TRUE); //menu is temporary
//Add a new toolband through Add method
//vMenuTemp holds an unspecified parameter
//oMyBar points to the newly created toolband
oCommandBars->Add(vName, vPos, vEmpty, vTemp, &oMyBar);
}
Instead of using the regular ATL convention of ComQIPtr<Excel::_Application>, I used a MFC class creator for COM interfaces to create the wrapper class (CApplication) to make the code simpler. This has worked fine for me before, and I would change it but I'd have to wade through a bunch of conflicts while including the Excel type library directly.
Any help or direction on this would be greatly appreciated! Also, please let me know what additional information I can give, as I'm open for trying almost anything.

