I m using P/Invoke to call an unmanaged C function from C#, passing an array of objects. In the unmanaged code I query IUnknown for IDispatch. This works for the simple case, but getting IDispatch fails if one of the objects is an array itself.
管理代码:
[DllImport("NativeDll.dll")]
static extern void TakesAnObjectArray(int len,
[MarshalAs(UnmanagedType.LPArray,
ArraySubType = UnmanagedType.IUnknown)]object[] a);
public static void exec1(int a, object b, string c)
{
Object[] info_array;
Object[] parameters_array;
parameters_array = new object[4];
parameters_array[0] = a;
parameters_array[1] = b;
parameters_array[2] = c;
parameters_array[3] = 55;
// THIS WORKS GREAT
TakesAnObjectArray(4, parameters_array);
info_array = new object[6];
info_array[0] = parameters_array;
// THIS DOESN T
// I CAN T GET IDISPATCH FOR THE 1ST OBJECT
TakesAnObjectArray(6, info_array);
}
un管理代码:
void TakesAnObjectArray(int len, LPUNKNOWN p[])
{
HRESULT hr;
for (int i=0; i<len; i++)
{
IDispatch *disp = NULL;
hr = p[i]->QueryInterface(IID_IDispatch, (void**)&disp);
}
}
QueryInterface is successful most of the time. But, if the managed object is actually a System.Object[] , I can t get an IDispatch interface (hr = 0x80004002 = E_NOINTERFACE = No such interface supported ).
Can I use MarshalAs(...) in some way that will fix this? Or Is there another way to get this to work?