问题描述
我怎么能转换成JavaScript数组()到ATL / COM阵列,而不使用VBArray的?
How can I convert a JavaScript array() to an ATL/COM array without using VBArray?
我要转换的是一个新的Array()将SAFEARRAY。
What I want to convert is a new Array() to a SAFEARRAY.
推荐答案
下面是一个code来做到这一点(考虑你已经得到了JS数组对象为C ++变体),同样的方法江晟建议前面:
Here's a code to do just that (considering you already got the JS Array object as a C++ Variant), same way as Sheng Jiang suggested earlier:
bool VariantToArray(__in const CComVariant& var, __out vector<CComVariant>& vecVars)
{
// convert variant to dispatch object
CComPtr<IDispatch> pDispatch = VariantToDispatch(var);
if (!pDispatch)
return false;
// invoke the object to retrieve the enumerator containing object
CComVariant varResult;
DISPPARAMS dispparamsNoArgs = {0};
EXCEPINFO excepInfo = {0};
UINT uiArgErr = (UINT)-1; // initialize to invalid arg
HRESULT hr = pDispatch->Invoke( DISPID_NEWENUM,
IID_NULL,
LOCALE_USER_DEFAULT,
DISPATCH_METHOD | DISPATCH_PROPERTYGET,
&dispparamsNoArgs,
&varResult,
&excepInfo,
&uiArgErr);
if (FAILED(hr))
return false;
// query the retrieved interface and get the enumerator object
CComPtr<IEnumVARIANT> pEnumVariant;
switch (varResult.vt)
{
case VT_UNKNOWN:
{
CComPtr<IUnknown> pUnknownResult = varResult.punkVal;
if (!pUnknownResult)
return false;
pEnumVariant = pUnknownResult; // implied query interface
}
break;
case VT_DISPATCH:
{
CComPtr<IDispatch> pDispatchResult = varResult.pdispVal;
if (!pDispatchResult)
return false;
pEnumVariant = pDispatchResult; // implied query interface
}
break;
default:
return false;
}
if (!pEnumVariant)
return false;
// reset enumerator to beginning of the list
hr = pEnumVariant->Reset();
if (FAILED(hr))
return false;
// enumerate and fetch items
CComVariant varItem;
ULONG uiFetched = 0;
do
{
// get next item
hr = pEnumVariant->Next(1, &varItem, &uiFetched);
if (FAILED(hr))
return false;
if (uiFetched == NULL) // last item
break;
// insert the item to the vector
vecVars.push_back(varItem);
} while (true);
return true;
}
希望帮助。
注意:结果
我看到一个帖子,其中someone抱怨这不会对IE9 工作(但它确实对IE6,7,8),我检查出来自己 - 对IE9(只)Invoke方法的失败并返回DISP_E_EXCEPTION。结果
所以我还在寻找一个更好的解决方案。
Note:
I saw a post where someone complained this doesn't work on IE9 (but it does on IE6,7,8), I checked it out myself - on IE9 (only) the Invoke method fail and return DISP_E_EXCEPTION.
So i'm still looking for a better solution.
编辑:结果
这里有一个code即会适用于所有IE浏览器:
Edited:
Here's a code that will works on all IE browsers:
bool VariantToArray(__in const CComVariant& var, __out vector<CComVariant>& vecVars)
{
// convert variant to dispatch object
CComPtr<IDispatch> pDispatch = VariantToDispatch(var);
if (!pDispatch)
return false;
// get DISPID of length parameter from array object
LPOLESTR sLengthName = L"length";
DISPID dispidLength = 0;
HRESULT hr = pDispatch->GetIDsOfNames(IID_NULL, &sLengthName, 1, LOCALE_USER_DEFAULT, &dispidLength);
if (FAILED(hr))
return false;
// get the number of elements using the DISPID of length parameter
CComVariant varLength;
DISPPARAMS dispParams = {0};
hr = pDispatch->Invoke(dispidLength, IID_NULL, LOCALE_USER_DEFAULT, DISPATCH_PROPERTYGET, &dispParams, &varLength, NULL, NULL);
if (FAILED(hr))
return false;
int nLength = 0; // length of the array
bool bGotInt = VariantToInt(varLength, nLength);
if (!bGotInt)
return false;
// get items of array
for (int i=0 ; i<nLength ; ++i)
{
// get DISPID of item[i] from array object
wstring strIndex = StringUtils::IntToString(i);
DISPID dispidIndex = 0;
LPOLESTR pIndex = reinterpret_cast<LPOLESTR>(const_cast<WCHAR *>(strIndex.data()));
hr = pDispatch->GetIDsOfNames(IID_NULL, &pIndex, 1, LOCALE_USER_DEFAULT, &dispidIndex);
if (FAILED(hr))
continue;
CComVariant varItem;
hr = pDispatch->Invoke(dispidIndex, IID_NULL, LOCALE_USER_DEFAULT, DISPATCH_PROPERTYGET, &dispParams, &varItem, NULL, NULL);
if (FAILED(hr))
continue;
vecVars.push_back(varItem);
}
return true;
}
享受:)
这篇关于我怎样才能JavaScript数组()转换为ATL / COM阵列?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!