After explaining the problem, you see here my complete solution of adding two variants with VT_BYREF support. You need two more VARIANTS in bad case.
It would be nice to get a smart solution of MS for Add, Sub etc., i.e. by declaring
VarAddInd(LPVARIANT, LPVARIANT, LPVARIANT)
As far as you can see in this code snippet, support for VT_I1, VT_UI2, VT_UI4, VT_UI8 and VT_I8 is missing as well.
MS has stopped in the middle to support VB6 classic only.
TCEOLEHELP_DLLEXPORT long __stdcall VariantAdd(LPVARIANT VL, LPVARIANT VR, LPVARIANT VResult) {
LPVARIANT PL; LPVARIANT PR; long ret;
if (VarHelpL == NULL) VarHelpL = VariantNew();
if (VarHelpR == NULL) VarHelpR = VariantNew();
short vt = VL->vt;
if (vt & VT_BYREF) {
ret = VariantCopyIndPtr(VarHelpL, VL);
if (ret) return ret;
PL = VarHelpL;
vt = vt & 0x3fff;
}
else
PL = VL;
if (vt == VT_I1 || vt == VT_UI2 || vt == VT_UI4 || vt == VT_UI8 || vt == VT_I8) {
if (PL == VarHelpL)
ret = VariantConvertDECIMAL(PL, PL, LOCALE_INVARIANT);
else {
ret = VariantConvertDECIMAL(PL, VarHelpL, LOCALE_INVARIANT);
PL = VarHelpL;
}
if (ret) return ret;
}
vt = VR->vt;
if (vt & VT_BYREF) {
ret = VariantCopyIndPtr(VarHelpR, VR);
if (ret) return ret;
PR = VarHelpR;
vt = vt & 0x3fff;
}
else
PR = VR;
if (vt == VT_I1 || vt == VT_UI2 || vt == VT_UI4 || vt == VT_UI8 || vt == VT_I8) {
if (PR == VarHelpR)
ret = VariantConvertDECIMAL(PR, PR, LOCALE_INVARIANT);
else {
ret = VariantConvertDECIMAL(PR, VarHelpR, LOCALE_INVARIANT);
PR = VarHelpR;
}
if (ret) return ret;
}
ret = VarAdd(PL, PR, VResult);
if (PL == VarHelpL) if (VarHelpL) VariantClear(VarHelpL);
if (PR == VarHelpR) if (VarHelpR) VariantClear(VarHelpR);
return ret;
}