我的技术规范

C/C++, Algorithm
随笔 - 11, 文章 - 7, 评论 - 1, 引用 - 0
数据加载中……

访问SafeArray,含“可选参数”



STDMETHODIMP 
CWinSockIPv6::
SendTo( SAFEARRAY** buf, VARIANT len, LONG* bytesSent)
{
    LONG lBound, uBound;
    LONG length;

    SafeArrayGetLBound( *buf, 1, &lBound);
    SafeArrayGetUBound( *buf, 1, &uBound);
    length = uBound - lBound + 1;

    if ( len.vt == VT_ERROR && len.scode == DISP_E_PARAMNOTFOUND)
    {
        ; // 这样 length = 默认值
    }
    else
    {
        if ( len.vt != VT_I4 )
            VariantChangeType( &len, &len, 0, VT_I4);

        if ( len.lVal > length ) // 要发送的数据,怎么可以比 length 还多呢?
            return S_FALSE;
        else
            length = len.lVal;
    }

    char* p;
    SafeArrayAccessData( *buf, (void HUGEP**)&p );
    *bytesSent = CWinSockIPv6Internel<CWinSockIPv6>::sendto( p, length);
    SafeArrayUnaccessData(*buf);
    
    return S_OK;
}

STDMETHODIMP 
CWinSockIPv6::
GetData( SAFEARRAY** buf, LONG* bytesGet)
{
    if( *buf != NULL ) {
        SafeArrayDestroy( *buf );
        *buf = NULL;
    }
    
    * bytesGet = 0;

    if ( recv_length_ == 0)
        return S_OK;

    SAFEARRAYBOUND rgsabound[1];
    rgsabound[0].lLbound = 0;
    rgsabound[0].cElements = recv_length_;

    *buf = SafeArrayCreate( VT_UI1, 1, rgsabound); 

    if ( *buf == NULL)
        return E_OUTOFMEMORY;

    HUGEP void * p;
    SafeArrayAccessData( *buf, (void HUGEP * FAR *)&p );
    memcpy( p, recv_buffer_, recv_length_);
    SafeArrayUnaccessData(*buf);

    * bytesGet = recv_length_;

    return S_OK;
}

posted on 2012-02-19 21:01 panchao 阅读(270) 评论(0)  编辑 收藏 引用 所属分类: utils


只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理