0
我在C++/CLI包裝器中使用C#dll。該DLL正在返回一個ADODB :: Recordset ^對象,但我需要包裝來返回一個_RecordsetPtr對象。 如何在兩者之間進行轉換?將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;
}
一個沉重的阻抗失配。用Marshal :: GetIUnknownForObject()恢復原始接口指針,將IntPtr強制轉換爲IUnknown *並調用_RecordsetPtr構造函數。一個引用計數太多,我認爲,請致電Marshal :: Release() –