long CStrArrayDoc::Sort(VARIANT FAR* vArray) { long i, j, min; BSTR bstrTemp; SAFEARRAY FAR* psa = NULL; BSTR HUGEP *pbstr; HRESULT hr; DWORD dwTimeStart; LONG cElements, lLBound, lUBound; USES_CONVERSION; // Type check VARIANT parameter. It should contain a BSTR array // passed by reference. The array must be passed by reference it is // an in-out-parameter. if (V_VT(vArray) != (VT_ARRAY | VT_BSTR)) AfxThrowOleDispatchException(1001, "Type Mismatch in Parameter. Pass a string array by reference"); psa = V_ARRAY(vArray); // Check dimensions of the array. if (SafeArrayGetDim(psa) != 1) AfxThrowOleDispatchException(1002, "Type Mismatch in Parameter. Pass a one-dimensional array"); dwTimeStart = GetTickCount(); // Get array bounds. hr = SafeArrayGetLBound(psa, 1, &lLBound); if (FAILED(hr)) goto error; hr = SafeArrayGetUBound(psa, 1, &lUBound); if (FAILED(hr)) goto error; // Get a pointer to the elements of the array. hr = SafeArrayAccessData(psa, (void HUGEP* FAR*)&pbstr); if (FAILED(hr)) goto error; // Bubble sort. cElements = lUBound-lLBound+1; for (i = 0; i < cElements-1; i++) { min = i; for (j = i+1; j < cElements; j++) { // NULL is a valid value for a BSTR. This code treats a NULL // BSTR as less than other string values. if (pbstr[min] == NULL) continue; else if (pbstr[j] == NULL || wcscmp(pbstr[j], pbstr[min]) < 0) min = j; } //Swap array[min] and array[i]. bstrTemp = pbstr[min]; pbstr[min] = pbstr[i]; pbstr[i] = bstrTemp; } hr = SafeArrayUnaccessData(psa); if (FAILED(hr)) goto error; return GetTickCount()-dwTimeStart; error: AfxThrowOleDispatchException(1003, "Unexpected Failure in FastSort method"); return 0; }
声明:Dim varDatas as Variant
真正使用时再定义维数:ReDim varDatas(5,3)2。在VC中用VARIANT接收,此时这个变体是VT_ARRAY | VT_VARIANT类型的,
也就是是说这个变体的parray存放的是SAFEARRAY,有一堆API可以对
SAFEARRAY进行操作!
可有示例代码瞧瞧?借来一观
{ long i, j, min;
BSTR bstrTemp;
SAFEARRAY FAR* psa = NULL;
BSTR HUGEP *pbstr;
HRESULT hr;
DWORD dwTimeStart;
LONG cElements, lLBound, lUBound; USES_CONVERSION; // Type check VARIANT parameter. It should contain a BSTR array
// passed by reference. The array must be passed by reference it is
// an in-out-parameter.
if (V_VT(vArray) != (VT_ARRAY | VT_BSTR))
AfxThrowOleDispatchException(1001,
"Type Mismatch in Parameter. Pass a string array by reference");
psa = V_ARRAY(vArray);
// Check dimensions of the array.
if (SafeArrayGetDim(psa) != 1)
AfxThrowOleDispatchException(1002,
"Type Mismatch in Parameter. Pass a one-dimensional array"); dwTimeStart = GetTickCount(); // Get array bounds.
hr = SafeArrayGetLBound(psa, 1, &lLBound);
if (FAILED(hr))
goto error;
hr = SafeArrayGetUBound(psa, 1, &lUBound);
if (FAILED(hr))
goto error; // Get a pointer to the elements of the array.
hr = SafeArrayAccessData(psa, (void HUGEP* FAR*)&pbstr);
if (FAILED(hr))
goto error; // Bubble sort.
cElements = lUBound-lLBound+1;
for (i = 0; i < cElements-1; i++)
{
min = i;
for (j = i+1; j < cElements; j++)
{
// NULL is a valid value for a BSTR. This code treats a NULL
// BSTR as less than other string values.
if (pbstr[min] == NULL)
continue;
else if (pbstr[j] == NULL
|| wcscmp(pbstr[j], pbstr[min]) < 0)
min = j;
} //Swap array[min] and array[i].
bstrTemp = pbstr[min];
pbstr[min] = pbstr[i];
pbstr[i] = bstrTemp;
} hr = SafeArrayUnaccessData(psa);
if (FAILED(hr))
goto error; return GetTickCount()-dwTimeStart; error: AfxThrowOleDispatchException(1003,
"Unexpected Failure in FastSort method");
return 0; }