// GetInstance is called when the function is baked. public static ICustomMarshaler GetInstance(string marshalCookie) { // marshalCookie denotes the array rank // must be 1 or 2 if (marshalCookie == "1") { if (instance1 == null) { instance1 = new XlObjectArray12Marshaler(1); } return instance1; } else if (marshalCookie == "2") { if (instance2 == null) instance2 = new XlObjectArray12Marshaler(2); return instance2; } throw new ArgumentException("Invalid cookie for XlObjectArrayMarshaler"); }
private unsafe static int TryExcelImpl12(int xlFunction, out object result, params object[] parameters) { int xlReturn; // Set up the memory to hold the result from the call XlOper12 resultOper = new XlOper12(); resultOper.xlType = XlType12.XlTypeEmpty; XlOper12* pResultOper = &resultOper; // No need to pin for local struct // Special kind of ObjectArrayMarshaler for the parameters (rank 1) using (XlObjectArray12Marshaler paramMarshaler = new XlObjectArray12Marshaler(1, true)) { XlOper12** ppOperParameters = (XlOper12**)paramMarshaler.MarshalManagedToNative(parameters); xlReturn = Excel12v(xlFunction, parameters.Length, ppOperParameters, pResultOper); } // pResultOper now holds the result of the evaluated function // Get ObjectMarshaler for the return value ICustomMarshaler m = XlObject12Marshaler.GetInstance(""); result = m.MarshalNativeToManaged((IntPtr)pResultOper); // And free any memory allocated by Excel Excel12v(xlFree, 1, &pResultOper, (XlOper12*)IntPtr.Zero); return xlReturn; }