程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> C語言 >> C++ >> 關於C++ >> 把JS、vbs中的數組傳遞給COM組件Activex

把JS、vbs中的數組傳遞給COM組件Activex

編輯:關於C++

COM組件的方法在IDL中的聲明:

[id(1), helpstring("方法InputArray")] HRESULT InputArray([in] VARIANT vData);

在腳本中建立數組並調用COM組件的方法:

當數組很大的時候,like 100k ,javascript在給數組賦值的時候效率非常低!完成時間,cpu占用率,占用的內存都大的可怕。反而VBScript卻完成的很好。

COM組件的代碼:

從代碼中可以看到vbscript傳進來的是個SafeArray。而javascript的情況就復雜了,javascript中得數組並不是真正意義上的數組,這個“數組”傳到COM中被放進一個集合裡,參數VARIANT的類型被置為VT_DISPATCH,我們得通過這個IDispatch指針調用invoke才能得到用來讀取集合的枚舉接口。

STDMETHODIMP CBigParamCtl::InputArray(VARIANT vData)
{
LPBYTE p ;

DWORD nLen;
HRESULT hr;
if( vData.vt == VT_DISPATCH)
{
  //deal with javascript array
  hr = VariantEnumToBytes(vData.pdispVal,&p, &nLen);
}
else
{
  //deal with vbscript array
  hr = VariantArrayToBytes(&vData, &p, &nLen) ;
}
if( S_OK == hr)
{
  //....... do sth on p
  delete[] p;
}

return S_OK;
}
HRESULT VariantEnumToBytes(IDispatch* disp, LPBYTE *ppBytes, DWORD *pdwBytes)
{
// DebugBreak();
HRESULT hr;
DISPPARAMS noArgs = { NULL, NULL, 0, 0 };
CComVariant resultV;
hr = disp->Invoke( DISPID_NEWENUM,
  IID_NULL,
  LOCALE_SYSTEM_DEFAULT,
  DISPATCH_PROPERTYGET,
  &noArgs,
  &resultV,
  NULL,
  NULL );
if( FAILED( hr ) && FAILED( resultV.ChangeType( VT_UNKNOWN ) ) )
  return E_FAIL;
// Bug 37459, above Invoke succeeds, but returns resultV.vt == VT_EMPTY, resultV->other param unchanged
if (resultV.vt != VT_UNKNOWN && resultV.vt != VT_DISPATCH)
{
  return E_FAIL;
}

CComQIPtr pEnum( resultV.punkVal );
if( !pEnum )
  return E_FAIL;
// Count the elements
*pdwBytes = 0;
hr = S_OK;

//Get Enum Size
while( hr == S_OK )
{
  hr = pEnum->Skip(1);
  if( hr == S_OK )
  (*pdwBytes)++;
}
//allocate memory
*ppBytes = (LPBYTE)new BYTE[*pdwBytes];
int nCount = 0;
CComVariant elemV;
pEnum->Reset();
hr = S_OK;
while( hr == S_OK )
{
  // Could switch to use Skip when Cary gets
  // it working.
  hr = pEnum->Next( 1, &elemV, NULL );
  if( elemV.vt != VT_I4 )
  hr = S_FALSE; // correct for dispproxy bug 19307
  else
  {
  int nTmp = elemV.lVal;
  (*ppBytes)[nCount] = (BYTE)nTmp;
  }

  if( hr == S_OK )
  nCount++;
}


return S_OK;
}
HRESULT VariantArrayToBytes(VARIANT *pVariant, LPBYTE *ppBytes, DWORD *pdwBytes)
{
USES_CONVERSION;
if (pVariant->vt != (VT_VARIANT | VT_BYREF))
  return E_INVALIDARG;
if (!(pVariant->pvarVal->vt & VT_ARRAY))
  return E_INVALIDARG;

SAFEARRAY* pX = NULL;

if (pVariant->pvarVal->vt & VT_BYREF)
  pX = *(pVariant->pvarVal->pparray);
else
  pX = pVariant->pvarVal->parray;
if (::SafeArrayGetDim(pX) != 1)
  return E_INVALIDARG;

   *ppBytes = NULL;
   *pdwBytes = 0;
VARIANT *pArray = NULL;
  HRESULT hr = E_FAIL;
_variant_t v;
hr = SafeArrayAccessData(pX, (void **) &pArray );
if( SUCCEEDED(hr))
{
  *pdwBytes = pX->rgsabound->cElements;
  *ppBytes = (LPBYTE)new BYTE[*pdwBytes];
for( DWORD i = 0; i < *pdwBytes; i++)
  {
  v = pArray[i];
  v.ChangeType(VT_UI1);
  (*ppBytes)[i] = v.bVal;
  }
SafeArrayUnaccessData( pX );
}
else
  return hr;
SafeArrayDestroy(pX);
return S_OK;
}

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved