XLL+ Class Library (6.3)

MyDaysInMonth Example

This example shows how to return an array of numbers from an add-in function.

CopyC++
// Function:    MyDaysInMonth 
// Returns:     LPXLOPER 
// Description: Returns an array of all the days in a given month 
 
//{{XLP_SRC(MyDaysInMonth) 
    // NOTE - the FunctionWizard will add and remove mapping code here. 
    //    DO NOT EDIT what you see in these blocks of generated code!
IMPLEMENT_XLLFN3(MyDaysInMonth, MyDaysInMonth_4, MyDaysInMonth_12, "RH", "UH", 
    L"MyDaysInMonth", 0, L"Month", 0, L"14", 0, L"Returns an array of all the "
    L"days in a given month", 0, L"Month number (1-12)\0", 0, 0, L"{MyDaysInMo"
    L"nth,,,Returns an array of all the days in a given month,14,1,0,U,{{0,{Mo"
    L"nth,Unsigned Short Int,0,,Month number (1-12),,,,}}},{},3,,0,0}", 1)
CXlOper* MyDaysInMonth_Impl(CXlOper&, unsigned short int);

extern "C" __declspec(dllexport)
LPXLOPER12 MyDaysInMonth_12(unsigned short int Month)
{
    XLL_FIX_STATE;
    CXlOper xloResult;
    try {
        CXlStructuredExceptionHandler _seh_;
        xloResult.HandleResult(MyDaysInMonth_Impl(xloResult, Month));
    }
    catch(const CXlRuntimeException& ex) {
        CXllApp::Instance()->DisplayException(xloResult, ex);
    }
    return xloResult.Ret12();
}
extern "C" __declspec(dllexport)
LPXLOPER4 MyDaysInMonth_4(unsigned short int Month)
{
    XLL_FIX_STATE;
    CXlOper xloResult;
    try {
        CXlStructuredExceptionHandler _seh_;
        xloResult.HandleResult(MyDaysInMonth_Impl(xloResult, Month));
    }
    catch(const CXlRuntimeException& ex) {
        CXllApp::Instance()->DisplayException(xloResult, ex);
    }
    return xloResult.Ret4();
}

CXlOper* MyDaysInMonth_Impl(CXlOper& xloResult, unsigned short int Month)
{
    // End of generated code 
//}}XLP_SRC 
 
    if ( Month < 1 || Month > 12 )
        xloResult = xlerrValue;
    else
    {
        // How many days in the month ? 
        int cDays;
        switch ( Month ) {
        case 2:
            cDays = 28;
            break;
        case 4: case 6: case 9: case 11:
            cDays = 30;
            break;
        default:
            cDays = 31;
            break;
        }

        // Allocate and fill array 
        double* adDays = new double[cDays];
        for ( int i = 0; i < cDays; i++ )
            adDays[i] = (double)(i + 1);

        // Apply to xloResult
        xloResult.FromDoubleArray(adDays, cDays);

        // Clean up array 
        delete[] adDays;
    }

    return xloResult.Ret();
}

Uses

CXlOper::FromDoubleArray