XLL+ Class Library (7.0)

XllOpen & XllClose events

XllOpen & XllClose are the events you'll most often need to catch. The virtual functions CXllApp::OnXllOpenEx and CXllApp::OnXllClose are provided to make these events easier to handle. Usually, putting your XLL initialization code into OnXllOpen, and your termination code into OnXllClose is the easiest and simplest way to go.

Occasionally, however, you may prefer to use the observer model for these events. For example, The framework uses the event model to "wire up" some methods that support more complex function features, such as Asynchronous functions. (This is largely because the XLL+ Function Wizard inserts and controls code in the cpp file, but does not have control of the header file.)

A pattern for Open/Close events

The code below is a typical pattern for using the open & close events to manage a global resource, such as a database connection.

CopyC++
CMyResource theResource;

class CMyResourceOpen : public CXllOpenEventStaticObserver {
    virtual void Update(CXllOpenEventArgs* e)
    {
        try
        {
            theResource.Open();
        }
        catch(...)
        {
            // Inform user
            CXllApp::MessageBox("Failed to open resource...", 1);
            // Cancel the open event
            e->SetCancel(true);
        }
    }
};
class CMyResourceClose : public CXllCloseEventStaticObserver {
    virtual void Update(CXllOpenEventArgs* e)
    {
        theResource.Close();
    }
};

Next: Volatile functions >>