XLL+ Class Library (7.0)

Global data overview

Application level data

An add-in will often need to contain data that has application-level scope. For example, an add-in might have a connection to a database that is only open as long as the add-in is in use.

Or it might keep a cache of the results of long slow calculations, so that if the same calculation is required more than once, the answer will be cost-free after the first call. (See Results cache for a built-in utility that will do this for you.)

CXllApp

XLL+ projects include a single instance of a class descended from CXllApp. This class and all its code is generated initially by the AppWizard when the project is created.

The CXllApp object represents the add-in library. It is in existence for as long as the library is open, and its lifetime ends when the add-in is closed.

It is a convenient place to keep data and objects that must exist at application scope.

Accessing the CXllApp instance

You can get a pointer to your CXllApp instance at any time by calling the static method CXllApp::Instance() or the global functions XllGetApp() and XllGetTypedApp().

Events

CXllApp has a number of events which you can trap by implementing virtual functions. The table below lists the most important events.

Event Description
OnXllOpenEx Called by the framework when Excel opens the XLL. The overridden function can return FALSE to halt further loading of the XLL.
OnXllClose Called by the framework when Excel closes the XLL

You should perform any initialization for your global objects in OnXllOpenEx(), and return FALSE if it fails. Similarly, any termination code should be in OnXllClose().

Adding global data to the application class

The application class is declared in the main header file for the project, e.g. tutorial1.h.

You can add your own data by declaring it as a member variable of the class:

CopyC++
class CTutorial1App : public CXllApp
{
public:
    CTutorial1App();

// Names 
public:
    static LPCTSTR m_pszDefName;

// Data                                 
    MyGlobalObject m_thingummy;         
 
// Overrides 
    virtual BOOL OnXllOpenEx();
    virtual void OnXllClose();

    ...
};

Keeping global data outside the application class

On the other hand, it may not be convenient to keep the global instance of your object inside the application class, particularly if the same object is used in multiple add-ins, or if the code is provided by a 3rd party.

In this case you can still hook your object up to the XllOpen and XllClose events. See Open & Close events for a discussion of this technique.

Next: Events >>