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; int i = sizeof(XlOper12); 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); }
internal static void XlAutoFree12(IntPtr pXloper12) { // CONSIDER: This might be improved.... // Another option would be to have the Com memory allocator run in unmanaged code. // Right now I think this is OK, and easiest from where I'm coming. // This function can only be called after a return from a user function. // I just free all the possibly big memory allocations. XlObjectArray12Marshaler.FreeMemory(); }