将ADODB :: Recordset ^转换为_RecordsetPtr

时间:2016-03-02 17:21:51

标签: c# c++ adodb recordset

我在C ++ / CLI包装器中使用了C#dll。 dll返回一个ADODB :: Recordset ^对象,但是我需要包装器来返回一个_RecordsetPtr对象。 我怎样才能在两者之间进行转换?

这是我到目前为止所拥有的。我遇到的问题是,在for循环的最后一行之后,该函数跳转到return语句并结束。它不会继续循环,并且它不会击中" Object ^ rows =。 。 "线。

_RecordsetPtr TraserInterface::GetDistributorRecordset()
{
    ADODB::Recordset^ recordset = TraserWrapper::Instance->traserInterface->DistributorRecordset;
    ADODB::Fields^ fields = ((ADODB::RecordsetClass^)recordset)->default;
    HRESULT hr;
    _RecordsetPtr recordsetPtr("ADODB.Recordset");
    for (int i = 0; i < fields->Count; i++)
    {
        ADODB::Field^ field = fields[i];
        String^ fieldName = field->Name;
        _bstr_t bstrName = MarshalString(fieldName).c_str();
        int type = (int)field->Type;
        int definedSize = field->DefinedSize;
        int fieldAttrib = field->Attributes;
        hr = recordsetPtr->Fields->Append(bstrName, (DataTypeEnum)type, definedSize, (FieldAttributeEnum)fieldAttrib);
    }
    Object^ rows = recordset->GetRows((int)ADODB::GetRowsOptionEnum::adGetRowsRest, (Object^)ADODB::BookmarkEnum::adBookmarkFirst, (Object^)fields);
    // loop through rows and populate recordsetPtr . . .
    return recordsetPtr;
}

1 个答案:

答案 0 :(得分:1)

感谢Hans Passant,我找到了解决方案:

_RecordsetPtr TraserInterface::GetDistributorRecordset()
{
    ADODB::Recordset^ recordset = TraserWrapper::Instance->traserInterface->DistributorRecordset);
    IntPtr recordsetIntPtr = Marshal::GetIUnknownForObject(recordset);
    IUnknown* unknown = (IUnknown*)(void*)recordsetIntPtr;
    _RecordsetPtr recordsetPtr(unknown);
    return recordsetPtr;
}