📄 test.cpp
字号:
vSingle = vString.CopyAs(VT_R4);
//@E TestVariant3
ostr << endcl << "Testing arithmetic:" << endcl;
vTmp = vInteger + vLong;
ostr << "vTmp = vInteger + vLong; // "
<< vInteger << " + " << vLong << " == " << vTmp << endcl;
ostr << "vTmp += vDouble; // " << vTmp;
vTmp += vDouble;
ostr << " += " << vDouble << " == " << vTmp << endcl;
Single flt = 3.25f;
ostr << "Single flt = 3.25;" << endcl
<< "flt += (Single)vTmp; // " << flt;
flt += (Single)vTmp;
ostr << " += " << vTmp << " == " << flt << endcl;
ostr << "vTmp += (Integer)77; // " << vTmp;
vTmp += Integer(77);
ostr << " += " << (Integer)77 << " == " << vTmp << endcl;
ostr << "vTmp += vInteger++; // " << vTmp;
vTmp += vInteger++;
ostr << " += " << (Integer)vInteger - 1 << " == " << vTmp << endcl;
ostr << "vTmp += ++vInteger; // " << vTmp;
vTmp += ++vInteger;
ostr << " += " << vInteger << " == " << vTmp << endcl;
vString = _W("Stuff");
vTmp = vString + vLong;
ostr << "vTmp = vString + vLong; // "
<< vString << " + " << vLong << " == " << vTmp << endcl;
ostr << "vTmp += vDouble; // " << vTmp << " += ";
vTmp += vDouble;
ostr << vDouble << " == " << vTmp << endcl;
vTmp = vInteger - vLong;
ostr << "vTmp = vInteger - vLong; // "
<< vInteger << " - " << vLong << " == " << vTmp << endcl;
ostr << "vTmp -= vDouble; // " << vTmp;
vTmp -= vDouble;
ostr << " -= " << vDouble << " == " << vTmp << endcl;
ostr << "flt -= (Single)vTmp; // " << flt;
flt -= (Single)vTmp;
ostr << " -= " << vTmp << " == " << flt << endcl;
ostr << "vTmp -= Integer(77); // " << vTmp;
vTmp -= (Integer)77;
ostr << " -= " << (Integer)77 << " == " << vTmp << endcl;
ostr << "vTmp -= vInteger--; // " << vTmp;
vTmp -= vInteger--;
ostr << " -= " << (Integer)vInteger + 1 << " == " << vTmp << endcl;
ostr << "vTmp -= --vInteger; // " << vTmp;
vTmp -= --vInteger;
ostr << " -= " << vInteger << " == " << vTmp << endcl;
vTmp = vInteger * vLong;
ostr << "vTmp = vInteger * vLong; // "
<< vInteger << " * " << vLong << " == " << vTmp << endcl;
ostr << "vTmp *= vDouble; // " << vTmp;
vTmp *= vDouble;
ostr << " *= " << vDouble << " == " << vTmp << endcl;
ostr << "flt *= (Single)vTmp; // " << flt;
flt *= (Single)vTmp;
ostr << " *= " << vTmp << " == " << flt << endcl;
ostr << "vTmp *= Integer(7); // " << vTmp;
vTmp *= (Integer)7;
ostr << " *= " << (Integer)7 << " == " << vTmp << endcl;
vTmp = vLong / vInteger;
ostr << "vTmp = vInteger / vLong; // "
<< vLong << " / " << vInteger << " == " << vTmp << endcl;
ostr << "vTmp /= vSingle; // " << vTmp;
vTmp /= vSingle;
ostr << " /= " << vSingle<< " == " << vTmp << endcl;
ostr << "flt /= (Single)vTmp; // " << flt;
flt /= (Single)vTmp;
ostr << " /= " << vTmp << " == " << flt << endcl;
ostr << "vTmp /= (Integer)7; // " << vTmp;
vTmp /= (Integer)7;
ostr << " /= " << (Integer)7 << " == " << vTmp << endcl;
vTmp = vLong % vInteger;
ostr << "vTmp = vLong % vInteger; // "
<< vLong << " % " << vInteger << " == " << vTmp << endcl;
ostr << "vTmp %= vInteger; // " << vTmp;
vTmp %= vInteger;
ostr << " %= " << vInteger << " == " << vTmp << endcl;
ostr << endcl << "Testing logical operators:" << endcl;
vDouble = vLong.CopyAs(VT_R8);
Boolean f;
f = (vDouble == vLong);
ostr << "f = (vDouble == vLong); // ("
<< vDouble << " == " << vLong << ") == " << f << endcl;
f = (vInteger == vLong);
ostr << "f = (vInteger == vLong); // ("
<< vInteger << " == " << vLong << ") == " << f << endcl;
f = (vInteger != vLong);
ostr << "f = (vInteger != vLong); // ("
<< vInteger << " != " << vLong << ") == " << f << endcl;
f = (vInteger <= vLong);
ostr << "f = (vInteger <= vLong); // ("
<< vInteger << " <= " << vLong << ") == " << f << endcl;
f = (vInteger < vLong);
ostr << "f = (vInteger < vLong); // ("
<< vInteger << " < " << vLong << ") == " << f << endcl;
f = (vInteger > vLong);
ostr << "f = (vInteger > vLong); // ("
<< vInteger << " > " << vLong << ") == " << f << endcl;
f = (vInteger >= vLong);
ostr << "f = (vInteger >= vLong); // ("
<< vInteger << " >= " << vLong << ") == " << f << endcl;
vTmp = vDate;
vTmp.Type(VT_BSTR);
f = (vTmp == vDate);
ostr << "f = (vTmp == vDate); // ("
<< vTmp << " == " << vDate << ") == " << f << endcl;
f = (vDate == vTmp);
ostr << "f = (vDate == vTmp); // ("
<< vTmp << " == " << vDate << ") == " << f << endcl;
f = (vTmp > vString);
ostr << "f = (vTmp == vString); // ("
<< vTmp << " == " << vString << ") == " << f << endcl;
f = (vTmp != vString);
ostr << "f = (vTmp != vString); // ("
<< vTmp << " != " << vString << ") == " << f << endcl;
ostr << ends;
char * pch = ostr.str();
vRet = pch;
// Since we grabbed the pointer, we must delete it
delete[] pch;
*pvInOut = _W("...back to you");
*pvOut = _W("I'm out of here");
return vRet;
} catch(Long e) {
ErrorHandler(e);
return BNULL;
}
}
Variant DLLAPI TestSA(SAFEARRAY ** ppsaiInOut, SAFEARRAY ** ppsasOut)
{
try {
/* TestSA deals with three arrays:
- It receives a 2-D array of Integers (ppsaiInOut2), which
it modifies and returns in the same parameter.
- It creates a 1-D array of Integers (psaiNew), which it
initializes and returns through a Variant return value.
- It creates a 1-D array of Strings (psasNew), which it
initializes and returns through an out parameter (ppsasOut).
*/
HRESULT hres;
int i;
//@B Create
// Create a new 1-D array of Integers
SAFEARRAY * psaiNew;
SAFEARRAYBOUND aDim[1];
aDim[0].lLbound = 1;
aDim[0].cElements = 8;
// Equivalent to: Dim aiNew(1 To 8) As Integer
psaiNew = SafeArrayCreate(VT_I2, 1, aDim);
if (psaiNew == NULL) throw ERROR_NOT_ENOUGH_MEMORY;
//@E Create
//@B Lock
// Initialize Integer array to squares of index
if (hres = SafeArrayLock(psaiNew)) throw hres;
int iCur = aDim[0].lLbound;
// Keep separate C++ index (i) and Basic index (iCur)
for (i = 0; i < (int)aDim[0].cElements; i++, iCur++) {
// Equivalent to: ai(iCur) = iCur * iCur
((Integer*)psaiNew->pvData)[i] = iCur * iCur;
}
if (hres = SafeArrayUnlock(psaiNew)) throw hres;
//@E Lock
// Create a new 1-D array of strings
SAFEARRAY * psasNew;
aDim[0].lLbound = 3;
aDim[0].cElements = 6;
// Equivalent to: Dim asNew(3 To 9) As String
psasNew = SafeArrayCreate(VT_BSTR, 1, aDim);
if (psasNew == NULL) throw E_OUTOFMEMORY;
// Initialize BSTR array
WCHAR wsz[] = L"Fan";
if (hres = SafeArrayLock(psasNew)) throw hres;
for (i = 0; i < (int)aDim[0].cElements; i++) {
wsz[0] = L'F' + (WCHAR)i;
// Equivalent to: ai(iCur) = string
((BSTR *)psasNew->pvData)[i] = SysAllocString(wsz);
}
if (hres = SafeArrayUnlock(psasNew)) throw hres;
// Analyze input array psaiInOut
SAFEARRAY * psaiInOut = *ppsaiInOut; // Dereference for performance
//@B GetInfo1
long cDim = SafeArrayGetDim(psaiInOut);
long cbElem = SafeArrayGetElemsize(psaiInOut);
//@E GetInfo1
// Get dimension bounds of input array
//@B GetInfo2
SAFEARRAYBOUND * aDims = new SAFEARRAYBOUND[cDim];
long iT;
for (i = 0; i < cDim; i++) {
hres = SafeArrayGetLBound(psaiInOut, i + 1, &aDims[i].lLbound);
if (hres) throw hres;
if (hres = SafeArrayGetUBound(psaiInOut, i + 1, &iT)) throw hres;
// Calculate elements from upper and lower bound
aDims[i].cElements = iT - aDims[i].lLbound + 1;
}
//@E GetInfo2
//@B Redim
// Double the size of the last dimension
i = cDim - 1;
aDims[i].cElements *= 2;
if (hres = SafeArrayRedim(psaiInOut, &aDims[i])) throw hres;
//@E Redim
long x, xMin, xMax, y, yMin, yMax;
//@B GetPut
// Modify 2-D array with SafeArrayGetElement and SafeArrayGetElement
long ai[2];
Integer iVal;
xMin = aDims[0].lLbound;
xMax = xMin + (int)aDims[0].cElements - 1;
yMin = aDims[1].lLbound;
yMax = yMin + (int)aDims[1].cElements - 1;
for (x = xMin; x <= xMax; x++) {
ai[0] = x;
for (y = yMin; y <= yMax; y++) {
ai[1] = y;
if (hres = SafeArrayGetElement(psaiInOut, ai, &iVal)) throw hres;
// Equivalent to: aiInOut(x, y) = aiInOut(x, y) + 1
iVal++;
if (hres = SafeArrayPutElement(psaiInOut, ai, &iVal)) throw hres;
}
}
//@E GetPut
//@B PtrIndex
// Lock 2-D array and modify
xMin = aDims[0].lLbound;
xMax = xMin + (int)aDims[0].cElements - 1;
yMin = aDims[1].lLbound;
yMax = yMin + (int)aDims[1].cElements - 1;
// Set up dimension array and pointer to receive value
Integer * piInOut;
if (hres = SafeArrayLock(psaiInOut)) throw hres;
for (x = xMin; x <= xMax; x++) {
ai[0] = x;
for (y = yMin; y <= yMax; y++) {
ai[1] = y;
hres = SafeArrayPtrOfIndex(psaiInOut, ai, (void **)&piInOut);
if (hres) throw hres;
// Equivalent to: aiInOut(x, y) = aiInOut(x, y) + 1
(*piInOut)++;
}
}
if (hres = SafeArrayUnlock(psaiInOut)) throw hres;
//@E PtrIndex
//@B UglyCast
// Set up dimension array and pointer to receive value
if (hres = SafeArrayLock(psaiInOut)) throw hres;
Integer (*aiInOut)[4] = (Integer(*)[4])psaiInOut->pvData;
for (x = 0; x < (int)aDims[0].cElements; x++) {
for (y = 0; y < (int)aDims[1].cElements; y++) {
// Equivalent to: aiInOut(x, y) = aiInOut(x, y) + 1
// Switch x and y order for Basic storage order
aiInOut[y][x]++;
}
}
if (hres = SafeArrayUnlock(psaiInOut)) throw hres;
//@E UglyCast
delete[] aDims;
// Return through out parameter
*ppsasOut = psasNew;
//@B Copy
// Copy from psaiNew to psaiRet
SAFEARRAY * psaiRet;
if (hres = SafeArrayCopy(psaiNew, &psaiRet)) throw hres;
//@E Copy
// Destroy unneeded copy
//@B Destroy
if (hres = SafeArrayDestroy(psaiNew)) throw hres;
//@E Destroy
// Put copy into variant for return
Variant vRet = psaiRet;
// *ppsaiInOut = psaiRet;
return vRet;
} catch(Long e) {
ErrorHandler(e);
return BNULL;
}
}
Variant DLLAPI TestSafeArray(ArrayInteger & aiInOut,
ArrayString & asOut)
{
try {
Integer iVal;
Long i, iMid;
//@B GetSet
iMid = aiInOut.LBound() + (aiInOut.Elements() / 2);
// Get middle value of array
iVal = aiInOut.Get(iMid);
// Double it
iVal *= 2;
// Put modified version back
aiInOut.Set(iVal, iMid);
//@E GetSet
//@B CppIndex
// Square each value, C++ style
aiInOut.Lock();
for (i = 0; i < aiInOut.Elements(); i++) {
aiInOut[i] *= aiInOut[i];
}
aiInOut.Unlock();
//@E CppIndex
//@B BasIndex
// Divide each by two, Basic style
aiInOut.Lock();
for (i = aiInOut.LBound(); i <= aiInOut.UBound(); i++) {
aiInOut(i) /= 2;
}
aiInOut.Unlock();
//@E BasIndex
//@B CopyArray
// Copy an array
ArrayInteger aiCopy = aiInOut;
//@E CopyArray
//@B ReDimIt
// Redimension to throw away last element
if (aiInOut.IsSizable()) {
aiInOut.ReDim(Dim(aiInOut.LBound(), aiInOut.UBound() - 1));
}
//@E ReDimIt
//@B AssignArray
aiCopy = aiInOut;
//@E AssignArray
//@B ArrayString
// Create array of strings
ArrayString as = Dim(4, 9);
String s = _W("Fan");
for (i = as.LBound(); i <= as.UBound(); i++) {
s[0] = L'F' + (WCHAR)i;
as(i) = s;
}
// Return it through out parameter
asOut = as;
//@E ArrayString
//@B ArrayDouble
// Create array of doubles
ArrayDouble adbl = Dim(-5, 5);
for (i = adbl.LBound(); i <= adbl.UBound(); i++) {
adbl(i) = i * 3.1416;
}
// Return through Variant return value
Variant vRet = (Variant)adbl;
return vRet;
//@E ArrayDouble
// Some illegal operations - uncomment and move up to test
/*
aiInOut[40] = 1;
as(0) = s;
iVal = aiInOut.Get(13);
aiInOut.Set(iVal, -2);
*/
} catch(Long e) {
ErrorHandler(e);
return BNULL;
}
}
//@B AddEmUp
double DLLAPI AddEmUp(ParamArray & avParams)
{
try {
double dblRet = 0;
// Loop through the array, retrieving parameters
avParams.Lock();
for (long i = 0; i < avParams.Elements(); i++) {
// Ignore missing ones
if (!avParams[i].IsMissing()) {
dblRet += (double)avParams[i];
}
}
avParams.Unlock();
return dblRet;
} catch(Long e) {
ErrorHandler(e);
return 0.0;
}
}
//@E AddEmUp
//
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -