XLL+ Class Library (6.3)

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_XLLFN3(NORMSDIST2, NORMSDIST2_4, NORMSDIST2_12, "BB", "BB", 
    L"NORMSDIST2", "Z", L"Z", "4", L"4", 
    "Returns the standard normal cumulative distribution function (has a mean " 
    "of zero and a standard deviation of one)", 
    L"Returns the standard normal cumulative distribution function (has a mean"
    L" of zero and a standard deviation of one)", 
    "Z is the value for which you want the distribution\000", 
    L"Z is the value for which you want the distribution\0", 0, 0, 
    L"{NORMSDIST2,,,Returns the standard normal cumulative distribution functi"
    L"on (has a mean of zero and a standard deviation of one),4,3,0,B,{{0,{Z,D"
    L"ouble,0,,Z is the value for which you want the distribution,,,,}}},{},3,"
    L",0,0}", 3)
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 details.

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_XLLFN3(...) macro:

CopyC++
IMPLEMENT_XLLFN3(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 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 >>