问题描述
我不知道如何处理VBA中的XLOPER类型变量。有很多网页解释Xloper变量,c ++ DLL 和 XLL加载项之间的关系,但我需要在dll中写入c ++函数直接返回一个xloper到没有XLL框架的VBA。 (不需要用户定义函数,因为来自dll的函数将由VBA代码调用 - 而不是由Excel用户调用)。
I have no idea how to handle XLOPER type variable in VBA. There are plenty of web pages explaining the relationship between Xloper variable , c++ DLL and XLL add-in, however i need to write c++ function in a dll returning a xloper directly to VBA without XLL framework. (No need of User Defined Function since functions from the dll will be called by the VBA code - not by Excel users).
确实,我编写一个Excel VSTO,我需要从它调用c ++代码。我发现xloper变量是非常有用的XLL,所以我想直接使用这个变量类型在VBA / VB.Net。那么是可能吗?
Indeed i'm coding a Excel VSTO and i need to call c++ code from it. I found that xloper variable are very useful for XLL so i would like to use this variable type in VBA/VB.Net directly. So is it possible?
- 我猜想xloper可以使用变体类型处理,但我不知道如何。
i找到了这篇文章:,但没有明确的答案。
- my guess it that the xloper can be handled using variant type but i don't know how.i found this post :How can I marshall between XLOPER and VARIANT? but there is no clear answer.
编辑:
对不起,如果我不清楚,所以例如我使这个c ++函数从excel接收一个Int,并返回相同的值加上5作为xloper variale到excel。
sorry if i wasn't clear , so for instance i made this c++ function receiving a Int from excel and returning the same value plus 5 as a xloper variale to excel.
_declspec(dllexport) xloper _stdcall returnInt( int iVal)
{
xloper pxlval_ret;
int a ;
a =5 + iVal;
pxlval_ret->xltype = xltypeInt;
pxlval_ret->val.w = a ;
return pxlval_ret;
}
但我不知道如何在vba中调用它,变量a VARIANT?
but i don't know how do call it in vba, Is the return variable a VARIANT?
推荐答案
最后我仔细阅读了书中的Excel Add-in Development in C / C ++,道尔顿。它回答了问题,并为其提供源代码。如果你想要执行这个操作,你需要创建一个xloper包装器,xloper_to_v函数是上述书的xloper.cpp的参数。由于版权,我不能在这里发布整个代码,但只是一些代码行;我想它可以给一些有用的见解:
finally I read attentively the book "Excel Add-in Development in C / C++, 2nd Edition by Steve Dalton". It answers the question and provides source code for it. If you want to perform this operation you need to create a xloper wrapper, the xloper_to_v function which is par of xloper.cpp of the aforementioned book do this job. Due to copyrights, I can't publish here the whole code but just some lines of codes; i think it can give some useful insight:
bool xloper_to_vt(const xloper *p_op, VARIANT &var, bool convert_array)
{
VariantInit(&var); // type is set to VT_EMPTY
switch(p_op->xltype)
{
case xltypeNum:
var.vt = VT_R8;
var.dblVal = p_op->val.num;
break;
case xltypeInt:
var.vt = VT_I2;
var.iVal = p_op->val.w;
break;
case xltypeBool:
// see in the book
case xltypeStr:
// see in the book
case xltypeErr:
// see in the book
case xltypeMulti:
if(convert_array)
{
VARIANT temp_vt;
SAFEARRAYBOUND bound[2];
long elt_index[2];
// see in the book
xloper *p_op_temp = p_op->val.array.lparray;
for(WORD r = 0; r < p_op->val.array.rows; r++)
{
for(WORD c = 0; c < p_op->val.array.columns;)
{
// see in the book
}
}
break;
}
// else, fall through to default option
default: // type not converted
return false;
}
return true;
}
这篇关于如何直接从c ++ dll返回XLOPER(Excel变量)到VBA?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!