LoadLibrary call in AutoOpen

Sep 11, 2013 at 11:11 AM
Hi Govert,

I am trying to get Excel DNA's code working with xll (xll.codeplex.com). If I load my add-in through the Add-in Manager and then shut down Excel without unloading it, Excel crashes.

I have traced this to the LockModule, UnlockModule calls. If there is an outstanding LockModule call when Excel shuts down there is a problem. This has something to do with the destructors on static objects in xll but I am not sure which destructor is causing the problem and why.

Is the LockModule call necessary if I do not use the RTD functionality? I only need the Ribbon. Everything seems to be working when I remove the LockModule/UnlockModule calls but I wanted to be sure.

Regards,
Koustubh
Coordinator
Sep 11, 2013 at 11:32 AM
Hi Koustubh,

Why is this not an issue with the supported Excel-DNA version?
I think the explicit library load is there so that the load-time processing is done only once. In some circumstances Excel loads and unloads the library repeatedly, which had some cost I wanted to avoid - so it might just be an optimization . . .

But you're moving quite far from anything I can support.

Regards,
Govert
Sep 11, 2013 at 2:10 PM
Hi Govert,

I realize that I am on my own once I go down the path I have chosen! But since I am not changing the code significantly, I think I will be able to manage. I hope I can continue to ask conceptual questions like this one if I encounter something unexpected. In any case, thanks a lot for your excellent support.

I think this isn't an issue in the supported version because there are no static C++ class objects and no destructors. My best guess currently is that a destructor on a static object tries to callback into Excel (to unregister something?). Normally this succeeds. But because of the outstanding LoadLibrary call, the unloading of the dll and hence the destructor call is delayed to a time when Excel can no longer handle the callback, resulting in a crash. I will investigate further.

You are right about Excel loading and unloading repeatedly when the Add-in Manager is opened. Maybe there are other cases too. I will try to see if I can catch the Excel closing event (I think xll provides that) and do an UnlockModule there if the performance gets bad.

Regards,
Koustubh