如何从另一个XLL中调用一个XLL中的加载项功能

时间:2013-12-17 13:54:43

标签: c++ winapi visual-studio-2012 dll xll

我已经创建了一个XLL +用户定义函数sum1,并且在添加此加载项后它可以在Excel中顺利运行。但现在我正在构建另一个XLL,比如sum2。我希望我可以在sum2中添加sum1。由于sum1和sum2在不同的XLL中,因此不能直接调用它们并且需要一些代码来执行此操作。有没有人遇到过这个问题,有什么好办法吗?

我搜索了问题并找到了下面的代码,这是由evaluate和UDF函数完成的。但似乎代码非常陈旧,适用于visual studio 2005但不适用于2012年,我运行时可能会出现#Name错误。

如果有人可以帮助我,我将非常感激。非常感谢你。

// Function:    CalDaysInYear2
// Purpose:     Calls a function in another XLL

//{{XLP_SRC(CalDaysInYear2)
    // NOTE - the FunctionWizard will add and remove mapping code here.
    //    DO NOT EDIT what you see in these blocks of generated code!
IMPLEMENT_XLLFN2(CalDaysInYear2, "RI", "CalDaysInYear2", 
    "DayCount", "Date & Time", "Calls a function in another XLL,"
    " which returns number of days in a year according to the"
    " day count", "Day count convention\000", "\0appscope=1\0",
    1)

extern "C" __declspec( dllexport )
LPXLOPER CalDaysInYear2(short DayCount)
{
    XLL_FIX_STATE;
    CXlOper xloResult;
//}}XLP_SRC

    static int xlfEvaluate = 257;
    static int xlUDF = 255;

    CXlOper xloName, xloRef;
    int rc = 0;
    xloName = "CalDaysInYear";`enter code here`
    if (!rc)
        rc = xloRef.Excel(xlfEvaluate, 1, &xloName);
    if (!rc)
        rc = xloResult.Excel(xlUDF, 2, &xloRef, &CXlOper(DayCount, 0));

    return xloResult.Ret();
}

1 个答案:

答案 0 :(得分:0)

需要注意的第一个要点是 XLL是DLL

构建XLL(DLL)时Visual Studio还应该创建一个具有相同基本名称的LIB文件(即如果第一个名为project1.XLL,那么它也应该创建project1.LIB)。

如果将具有相应函数定义的头文件添加到第二个项目中,并将第一个项目创建的库添加到第二个项目中,那么VS应该处理调用GetProcAddress()等的脏位。

extern "C" __declspec( dllimport ) 
LPXLOPER CalDaysInYear2(short DayCount);

注意,第二个项目的标题使用dllimport而不是dllexport。