HOW TO: How do I decide, at run time, which functions to register?

Reference: Q0006

Article last modified on 3-Jul-2002


The information in this article applies to:

  • XLL+ for Visual Studio 6 - 3, 4.1, 4.2, 4.3.1
  • XLL+ for Visual Studio .NET - 3, 4.1, 4.2, 4.3.1
  • Excel - all versions

How do I decide, at run time, which functions to register?

Issue

Any add-in function that has been made available to Excel using the IMPLEMENT_XLLFN2() macro is automatically registered when the XLL is opened. Sometimes it is useful to disable this behavior, and only register selected functions.

Summary

  1. The list of automatically registered functions is constructed by a call to AddStaticFns() during the CXllApp::InitInstance() virtual function.
  2. Call CXllApp::RemoveFn(exported_function_name) after the call to AddStaticFns().
  3. The argument to RemoveFn() should be the exported name of the function (as it appears in Excel).

Example

In the example below, a decision is made at run time (based on the time) to exclude a list of functions from being exposed in Excel.

BOOL CPartialRegApp::InitInstance() 
{
    // Call the base class
    if ( !CXllApp::InitInstance() )
        return FALSE;

    // Set the name of the library to the default value
    m_stName = m_pszDefName;

    // Add the statically defined function specifications
    AddStaticFns();

    // Based on the time, decide whether or not to exclude 
    // some functions
    time_t tm1;
    struct tm* tm2;
    tm1 = time(0);
    tm2 = localtime(&tm1);
    BOOL bExclude = (tm2->tm_min % 2 == 0);

    // Optionally exclude some functions
    const char* apszOptionalFns[] = {
        "OptionalFn1",
        "OptionalFn2",
        0
    };

    if (bExclude) {
        for (int i = 0; apszOptionalFns[i]; i++)
            RemoveFn(apszOptionalFns[i]);
    }

    return TRUE;
}