XLL+ Class Library (6.3)

Events

Handling the XllOpen and XllClose events

A typical use of the XllOpen and XllClose events is to manage security. The user should log in to a system when the XLL is opened, and log out when it is closed. If the login fails, then the XLL should be disabled.

Look at the following code for an example of handling a login.

CopyC++
BOOL CTutorial1App::Authorize()
{
    // Show a login dialog 
    int attempts = 3;
    while (true)
    {
        // Put up a dialog
        CXlOper xloLogin;
        BOOL quit = !CXllApp::XlInputBox(xloLogin, 
            _T("Please enter a password"), XlInputBoxTypeText, 
            _T("Tutorial1"));

        // Check the password
        BOOL ok = !quit && (xloLogin.ToString() == _T("123"));

        // Succeed, try again or give up 
        if (ok)
            return TRUE;
        if (quit || --attempts == 0)
        {
            CXllApp::XlMessageBox(_T("Logon for Tutorial1 failed"), 
                XlMessageBoxTypeInformation);
            return FALSE;
        }
    }
}

BOOL CTutorial1App::OnXllOpenEx()
{
    if (!Authorize())
        return FALSE;
    // TODO: Allocate any application-level resources 
    return TRUE;
}

The login is obviously rather simplistic, but it shows the pattern.

If FALSE is returned by OnXllOpenEx(), then the XLL will not be opened and none of its add-in functions and commands will be available.

Note: You should remove the call to Authorize() from OnXllOpenEx() as soon as you've finished testing it. It gets very tedious entering the password every time you open the XLL!

Next: Ensuring global data is thread-safe >>