通过COM发送和接收数组

时间:2012-02-10 12:47:38

标签: c++ com variant safearray

通过COM接收和发送数组的正确方法是什么?到目前为止,这是我的尝试:包含在变体中的双重安全阵列。

//takes variant holding safearray of doubles
//returns a similar variant having multipled every element by 2
STDMETHODIMP MyComClass::safearraytimestwo(VARIANT in, VARIANT* out)
{
    CComSafeArray<double> sa_in;
    sa_in.Attach(*in.pparray);
    ULONG size = sa_in.GetCount();

    CComSafeArray<double> *out_sa = new CComSafeArray<double>(size);

    for (long i=0;i<size;i++)
        out_sa->SetAt(i,sa_in[i]*2);

    out = new CComVariant(out_sa);
    return S_OK;
}

问题:   - 当前编译在循环操作上失败:error C2679: binary '=' : no operator found which takes a right-hand operand of type 'ATL::_ATL_AutomationType<DOUBLE>::_typewrapper' (or there is no acceptable conversion) 编辑:使用SetAt()而不是operator[] 解决   - 我应该在堆上声明out_sa吗?当out被解除分配时,它会被解除分配(我只能假设客户端会这样做吗?)

非常感谢任何帮助!

编辑2:这是一个部分实现,只是为了返回一个safearray。

STDMETHODIMP CSpatialNet::array3(VARIANT in, VARIANT* out)
{
    CComSafeArray<double> out_sa;
    out_sa.Create(2);
    out_sa.SetAt(0,1.2);
    out_sa.SetAt(1,3.4);
    *out = CComVariant(out_sa);
    out_sa.Detach();
    return S_OK;
}

这也失败了; lisp报告

(vl-load-com)
(setq n (vlax-create-object "sdnacomwrapper.SpatialNet"))
(setq v (vlax-make-variant 1.0))
(vlax-invoke-method n 'array3 v 'newvar)
; error: ActiveX Server returned an error: The parameter is incorrect

用变量数组替换CComSafeArray<double>会产生相同的错误。

3 个答案:

答案 0 :(得分:5)

有这个工作 - 正确的代码是这样的:

STDMETHODIMP MyComClass::arraytimestwo(VARIANT in, VARIANT* out)
{
    CComSafeArray<double> sa_in;
    sa_in.Attach(in.parray);
    ULONG size = sa_in.GetCount();
    CComSafeArray<double> out_sa;
    out_sa.Create(size);
    for (long i=0;i<size;i++)
        out_sa.SetAt(i,sa_in.GetAt(i)*2);

    CComVariant(out_sa).Detach(out);
    return S_OK;
}

在Lisp ......

(vl-load-com)
(setq n (vlax-create-object "mycomdll.MyComClass"))
(setq sa (vlax-make-safearray vlax-vbDouble '(0 . 1)))
(vlax-safearray-fill sa '(1 2))
(vlax-safearray->list sa)
(vlax-invoke-method n 'arraytimestwo sa 'newvar)
(vlax-safearray->list newvar)

原始尝试的具体错误:

  • 需要使用Detach方法为out
  • 分配值
  • 需要附加到in.parray而不是*in.pparray(不是同一件事)

答案 1 :(得分:1)

使用VARIANT参数的COM方法负责检查参数,捕获异常并且它实际上不会破坏[in]数组,因此在C ++方面更准确的实现将是:

STDMETHODIMP Foo(VARIANT in, VARIANT* out)
{
    _ATLTRY
    {
        ATLENSURE_THROW(in.vt == (VT_ARRAY | VT_R8), E_INVALIDARG);
        ATLENSURE_THROW(out, E_POINTER);
        VariantInit(out);
        CComSafeArray<DOUBLE>& sa_in =
            reinterpret_cast<CComSafeArray<DOUBLE>&>(in.parray);
        ULONG size = sa_in.GetCount();
        CComSafeArray<DOUBLE> out_sa;
        ATLENSURE_SUCCEEDED(out_sa.Create(size));
        for(ULONG nIndex = 0; nIndex < size; nIndex++)
            out_sa.SetAt(nIndex, sa_in.GetAt(nIndex) * 2);
        // NOTE: Constructor copies data so it's accurate just inefficient
        ATLVERIFY(SUCCEEDED(CComVariant(out_sa).Detach(out)));
    }
    _ATLCATCH(Exception)
    {
        return Exception;
    }
    return S_OK;
}

答案 2 :(得分:1)

Sideshow Bob和Roman R.使用的解决方案

ComVariant(out_sa).Detach(out);

这有一个严重的缺点。 SAFEARRAY out_sa被传递到CComVariant's构造函数,构造函数将复制SAFEARRAY。为了避免更好地使用副本

::VariantInit(out);
out->vt = (VT_ARRAY | VT_R8);
out->parray = out_sa.Detach();

正如罗曼指出的那样,您还应该首先检查in是否确实为VT_ARRAY | VT_R8类型。鲍勃的解决方案存在严重缺陷:in.parray附加到sa_in但未分离,因此析构函数将销毁in.parray。但是根据COM的规则,不允许函数arraytimestwo(VARIANT in,...修改自变量in。 COM充满陷阱。因此,我认为最好通过引用传递参数in

我提供了(希望!)改进的解决方案和测试功能:

STDMETHODIMP arraytimestwo(const VARIANT &in, VARIANT* out)
{
  try
  {
    if (in.vt != (VT_ARRAY | VT_R8)) return E_INVALIDARG;
    CComSafeArray<double> sa_out;
    variant_t wrapCopyIn(in);
    sa_out.Attach(wrapCopyIn.parray);
    if (sa_out.GetDimensions() > 1) return E_INVALIDARG;

    for (long i = sa_out.GetLowerBound(0); i <= sa_out.GetUpperBound(0); i++)
       sa_out[i] *= 2;
    //Don't forget
    sa_out.Detach();

    *out = wrapCopyIn.Detach();
  }
  catch (const CAtlException& e)
  {
    // Exception object implicitly converted to HRESULT,
    // and returned as an error code to the caller
    return e;
  }
  return S_OK;
}

void TestArraytimestwo()
{
  CComSafeArray<double> vec(5, 1);
  for (int i = vec.GetLowerBound(); i <= vec.GetUpperBound(); i++) vec[i] = i * 1.1;

  variant_t in, out;
  in.vt = (VT_ARRAY | VT_R8);
  in.parray = vec.Detach();

  if (!SUCCEEDED(arraytimestwo(in, &out)))
  {
   std::cout << "Something went wrong!" << "\n";
   return;
  }

  CComSafeArray<double> sa_out;
  sa_out.Attach(out.parray);
  vec.Attach(in.parray);
  for (int i = vec.GetLowerBound(); i <= vec.GetUpperBound(); i++)
     std::cout << vec[i] << "  " << sa_out[i] << std::endl;

  //Not necessary, but I do it out of habit
  vec.Detach();
  sa_out.Detach();
}

备注:Bob的原始功能应如下所示(跳过try ... catch)

STDMETHODIMP arraytimestwoBob(const VARIANT &in, VARIANT* out)
{
  CComSafeArray<double> sa_in;
  sa_in.Attach(in.parray);
  CComSafeArray<double> out_sa(sa_in.GetCount(), sa_in.GetLowerBound());
  for (long i = sa_in.GetLowerBound(); i <= sa_in.GetUpperBound(); i++) out_sa[i] = 2 * sa_in[i];
  sa_in.Detach();//Detach, this function doesn't own in 
  ::VariantInit(out);
  out->vt = (VT_ARRAY | VT_R8);
  out->parray = out_sa.Detach();
  return S_OK;
}