private unsafe static int TryExcelImpl4(int xlFunction, out object result, params object[] parameters) { int xlReturn; // Set up the memory to hold the result from the call XlOper resultOper = new XlOper(); resultOper.xlType = XlType.XlTypeEmpty; XlOper *pResultOper = &resultOper; // No need to pin for local struct // Special kind of ObjectArrayMarshaler for the parameters (rank 1) using (XlObjectArrayMarshaler paramMarshaler = new XlObjectArrayMarshaler(1, true)) { XlOper **ppOperParameters = (XlOper **)paramMarshaler.MarshalManagedToNative(parameters); xlReturn = Excel4v(xlFunction, pResultOper, parameters.Length, ppOperParameters); } // pResultOper now holds the result of the evaluated function // Get ObjectMarshaler for the return value ICustomMarshaler m = XlObjectMarshaler.GetInstance(""); result = m.MarshalNativeToManaged((IntPtr)pResultOper); // And free any memory allocated by Excel Excel4v(xlFree, (XlOper *)IntPtr.Zero, 1, &pResultOper); return(xlReturn); }
public unsafe static uint GetCurrentSheetId4() { XlOper SRef = new XlOper(); SRef.xlType = XlType.XlTypeSReference; //SRef.srefValue.Count = 1; //SRef.srefValue.Reference.RowFirst = 1; //SRef.srefValue.Reference.RowLast = 1; //SRef.srefValue.Reference.ColumnFirst = 1; //SRef.srefValue.Reference.ColumnLast = 1; XlOper resultOper = new XlOper(); XlOper *pResultOper = &resultOper; XlOper * pSRef = &SRef; XlOper **ppSRef = &(pSRef); int xlReturn; xlReturn = Excel4v(xlSheetNm, pResultOper, 1, ppSRef); if (xlReturn == 0) { XlOper ResultRef = new XlOper(); xlReturn = Excel4v(xlSheetId, &ResultRef, 1, (XlOper **)&(pResultOper)); if (xlReturn == 0 && ResultRef.xlType == XlType.XlTypeReference) { return(ResultRef.refValue.SheetId); } } return(0); }
public unsafe static IntPtr GetCurrentSheetId4() { // In a macro type function, xlSheetNm seems to return the Active sheet instead of the Current sheet. // So we first try to get the Current sheet from the caller. IntPtr retval = GetCallerSheetId4(); if (retval != IntPtr.Zero) { return(retval); } // Else we try the old way. XlOper SRef = new XlOper(); SRef.xlType = XlType.XlTypeSReference; //SRef.srefValue.Count = 1; //SRef.srefValue.Reference.RowFirst = 1; //SRef.srefValue.Reference.RowLast = 1; //SRef.srefValue.Reference.ColumnFirst = 1; //SRef.srefValue.Reference.ColumnLast = 1; XlOper resultOper = new XlOper(); XlOper *pResultOper = &resultOper; XlOper * pSRef = &SRef; XlOper **ppSRef = &(pSRef); int xlReturn; xlReturn = Excel4v(xlSheetNm, pResultOper, 1, ppSRef); if (xlReturn == 0) { XlOper resultRef = new XlOper(); XlOper *pResultRef = &resultRef; xlReturn = Excel4v(xlSheetId, pResultRef, 1, (XlOper **)&(pResultOper)); // Done with pResultOper - Free Excel4v(xlFree, (XlOper *)IntPtr.Zero, 1, &pResultOper); if (xlReturn == 0) { if (resultRef.xlType == XlType.XlTypeReference) { return(resultRef.refValue.SheetId); } // Done with ResultRef - Free it too Excel4v(xlFree, (XlOper *)IntPtr.Zero, 1, &pResultRef); } } return(retval); }
private static extern unsafe int Excel4v(int xlfn, XlOper *pOperRes, int count, XlOper **ppOpers);