C# ArrayList 与 C++ Interop
C# ArrayList with C++ Interop
在 C# 中得到这个
public static ArrayList MyFct()
{
var x = new ArrayList();
x.Add(5);
return x
}
在使用 Interop 的 C++ 端,调用 Invoke_3() 后,我得到一个包含我的数组的 variant_t,但是我如何“提取“它?
(比如我想获取商品数量等)
这是一个 VARIANT (variant_t
is a wrapper that comes from comdef.h
/ #import
statement) that contains an IDispatch 参考,因此您可以使用 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.
}
在 C# 中得到这个
public static ArrayList MyFct()
{
var x = new ArrayList();
x.Add(5);
return x
}
在使用 Interop 的 C++ 端,调用 Invoke_3() 后,我得到一个包含我的数组的 variant_t,但是我如何“提取“它?
(比如我想获取商品数量等)
这是一个 VARIANT (variant_t
is a wrapper that comes from comdef.h
/ #import
statement) that contains an IDispatch 参考,因此您可以使用 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.
}