[英]C# ArrayList with C++ Interop
Got this in C#在 C# 中得到了这个
public static ArrayList MyFct()
{
var x = new ArrayList();
x.Add(5);
return x
}
On C++ side with Interop, after calling Invoke_3(), I get a variant_t which contains my array, but how can I "extract" it?在带有 Interop 的 C++ 端,调用 Invoke_3() 后,我得到一个包含我的数组的variant_t ,但我如何“提取”它?
(for instance I would like to get the number of items, etc.) (例如我想获取项目的数量等)
This is a VARIANT ( variant_t
is a wrapper that comes from comdef.h
/ #import
statement) that contains an IDispatch reference, so you can use IDispatch
's methods, for example like this (error checks omitted):这是一个包含IDispatch引用的VARIANT (variant_t 是来自
variant_t
/ #import
语句的包装comdef.h
),因此您可以使用IDispatch
的方法,例如像这样(省略错误检查):
variant_t v;
HRESULT hr = unk->GetList(&v); // get my VARIANT that contains an ArrayList reference
// get "Count" method dispid
LPOLESTR countName = (LPOLESTR)L"Count";
DISPID dispidCount;
hr = V_DISPATCH(&v)->GetIDsOfNames(IID_NULL, &countName, DISPATCH_METHOD, 0, &dispidCount);
DISPPARAMS dp = { nullptr, nullptr, 0, 0 };
variant_t count;
hr = V_DISPATCH(&v)->Invoke(dispidCount, IID_NULL, 0, DISPATCH_METHOD, &dp, &count, nullptr, nullptr);
// get "Item(i)" method dispid
LPOLESTR itemName = (LPOLESTR)L"Item";
DISPID dispidName;
hr = V_DISPATCH(&v)->GetIDsOfNames(IID_NULL, &itemName, DISPATCH_METHOD, 0, &dispidName);
for (int i = 0; i < V_I4(&count); i++)
{
// build index as variant
variant_t index(i);
DISPPARAMS dpIndex = { &index, nullptr, 1, 0 };
// get item(i)
variant_t item;
hr = V_DISPATCH(&v)->Invoke(dispidName, IID_NULL, 0, DISPATCH_METHOD, &dpIndex, &item, nullptr, nullptr);
// item can be a scalar value
// String => VT_BSTR, Int32 => VT_I4, etc.
// or another ComVisible object => VT_DISPATCH, VT_UNKNOWN, etc.
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.