XLL+ Class Library (7.0)

Completing the code

Code generated by the wizard

As you return to Visual Studio, you will see that the wizard has inserted the following source code at the end of your C++ file.

CopyC++
// Function:    NORMSDIST2 
// Purpose:     Returns the standard normal cumulative distribution function (has a mean of zero and a standard deviation of one) 
 
//{{XLP_SRC(NORMSDIST2) 
    // NOTE - the FunctionWizard will add and remove mapping code here. 
    //    DO NOT EDIT what you see in these blocks of generated code! 
 
#pragma region NORMSDIST2 support code
IMPLEMENT_XLLFN4(NORMSDIST2, NORMSDIST2_4, NORMSDIST2_12, "BB", "BB", L"NORMSD"
    L"IST2", 0, L"Z", 0, L"4", 0, L"Returns the standard normal cumulative dis"
    L"tribution function (has a mean of zero and a standard deviation of one)", 
    0, L"Z is the value for which you want the distribution\0", 0, 0, L"{NORMS"
    L"DIST2,,,Returns the standard normal cumulative distribution function (ha"
    L"s a mean of zero and a standard deviation of one),4,1,0,B,{{0,{Z,Double,"
    L"0,,Z is the value for which you want the distribution,,,,}}},{},3,,0,0,,"
    L",,0,0}", 1, 0, 0)
double NORMSDIST2_Impl(double);
extern "C" __declspec(dllexport)
double NORMSDIST2_12(double Z)
{
    XLL_FIX_STATE;
    return NORMSDIST2_Impl(Z);
}
extern "C" __declspec(dllexport)
double NORMSDIST2_4(double Z)
{
    XLL_FIX_STATE;
    return NORMSDIST2_Impl(Z);
}

#pragma endregion

double NORMSDIST2_Impl(double Z)
{
    // End of generated code 
//}}XLP_SRC 
    // TODO - set the return value. 
    return 0;
}

In the following sections, we'll examine all this code.

Hidden code

You may find that much of the code is hidden; it sits between #pragma region and #pragma endregion, and the XLL+ Function Wizard has automatically hidden it for you.

You can make it visible by clicking on the little "+" symbol in the left margin. See Hidden regions for more information.

Delimiters

The first thing to notice are the XLP_SRC comments that delimit the generated code:

CopyC++
//{{XLP_SRC(NORMSDIST2) 
    // NOTE - the FunctionWizard will add and remove mapping code here. 
    //    DO NOT EDIT what you see in these blocks of generated code!
    ...
//}}XLP_SRC

The Function Wizard "owns" all the code between the delimiters. Everything between these comments is liable to be written over if you use the Function Wizard again to change the function in any way. Be very careful to make your changes outside these delimiters.

Macro

The next section of interest is the IMPLEMENT_XLLFN4(...) macro:

CopyC++
IMPLEMENT_XLLFN4(NORMSDIST2, NORMSDIST2_4, NORMSDIST2_12, "BB", "BB", ...

This macro contains all the information you typed into the Function Wizard. At run-time, the XLL+ runtime library will pass all this data to Excel, in order to register your function and to tell Excel about its arguments. (Again, be careful not to alter it yourself.)

If you need to come back to the function later to change it, then the XLL+ Function Wizard will read the macro and construct the function's definition from it.

Wrapper functions

A function that is exported to Excel 2007 must follow different rules to a function exported to Excel 2003 and below. The XLL+ Function Wizard generates two versions of each add-in function, one for Excel 2003 and below (named NORMSDIST2_4) and one for Excel 2007 and above (named NORMSDIST2_12). Both of these functions are wrappers, which call the same common function, NORMSDIST2_Impl.

The next section of code declares the common function, and defines the two wrapper functions.

CopyC++
double NORMSDIST2_Impl(double);

extern "C" __declspec(dllexport)
double NORMSDIST2_12(double Z)
{
    XLL_FIX_STATE;
    return NORMSDIST2_Impl(Z);
}
extern "C" __declspec(dllexport)
double NORMSDIST2_4(double Z)
{
    XLL_FIX_STATE;
    return NORMSDIST2_Impl(Z);
}

Definition of common function

Finally, we have the definition of the common function.

CopyC++
double NORMSDIST2_Impl(double Z)
{
    // End of generated code 
//}}XLP_SRC 
    // TODO - set the return value. 
    return 0;
}

We have a complete skeleton of the function; now we need to add some useful code.

Adding some useful code

Add code to the add-in function to call our stand-alone function, as shown below:

CopyC++
double NORMSDIST2_Impl(double Z)
{
    // End of generated code 
//}}XLP_SRC 
    return CumNormal(Z);                     
}

That's the lot. We're now ready to build and test our add-in function.

Next: Building your add-in library >>