Exemple #1
0
        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 (XlMarshalXlOperArrayContext paramMarshaler
                       = new XlMarshalXlOperArrayContext(1, true))
            {
                XlOper12 **ppOperParameters = (XlOper12 **)paramMarshaler.ObjectArrayReturn(parameters);
                xlReturn = Excel12v(xlFunction, parameters.Length, ppOperParameters, pResultOper);
            }

            // pResultOper now holds the result of the evaluated function
            // Get ObjectMarshaler for the return value
            result = XlMarshalContext.ObjectParam((IntPtr)pResultOper);

            // And free any memory allocated by Excel
            Excel12v(xlFree, 1, &pResultOper, (XlOper12 *)IntPtr.Zero);

            return(xlReturn);
        }
Exemple #2
0
        public unsafe static uint GetCurrentSheetId12()
        {
            XlOper12 SRef = new XlOper12();

            SRef.xlType = XlType12.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;

            XlOper12  resultOper  = new XlOper12();
            XlOper12 *pResultOper = &resultOper;

            XlOper12 * pSRef  = &SRef;
            XlOper12 **ppSRef = &(pSRef);
            int        xlReturn;

            xlReturn = Excel12v(xlSheetNm, 1, ppSRef, pResultOper);
            if (xlReturn == 0)
            {
                XlOper12 ResultRef = new XlOper12();
                xlReturn = Excel12v(xlSheetId, 1, (XlOper12 **)&(pResultOper), &ResultRef);
                if (xlReturn == 0 && ResultRef.xlType == XlType12.XlTypeReference)
                {
                    return(ResultRef.refValue.SheetId);
                }
            }
            return(0);
        }
Exemple #3
0
        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);
        }
 int LastNonMissingIndex(XlOper12 **callParams, int lastIndex)
 {
     for (int i = lastIndex; 0 <= i; i--)
     {
         if (callParams[i]->xlType != XlType12.XlTypeMissing)
         {
             return(i);
         }
     }
     return(-1);
 }
Exemple #5
0
        public unsafe static IntPtr GetCurrentSheetId12()
        {
            // 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 = GetCallerSheetId12();

            if (retval != IntPtr.Zero)
            {
                return(retval);
            }

            // Else we try the old way.
            XlOper12 SRef = new XlOper12();

            SRef.xlType = XlType12.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;

            XlOper12  resultOper  = new XlOper12();
            XlOper12 *pResultOper = &resultOper;

            XlOper12 * pSRef  = &SRef;
            XlOper12 **ppSRef = &(pSRef);
            int        xlReturn;

            xlReturn = Excel12v(xlSheetNm, 1, ppSRef, pResultOper);
            if (xlReturn == 0)
            {
                XlOper12  resultRef  = new XlOper12();
                XlOper12 *pResultRef = &resultRef;
                xlReturn = Excel12v(xlSheetId, 1, (XlOper12 **)&(pResultOper), pResultRef);

                // Done with pResultOper - Free
                Excel12v(xlFree, 1, &pResultOper, (XlOper12 *)IntPtr.Zero);

                if (xlReturn == 0)
                {
                    if (resultRef.xlType == XlType12.XlTypeReference)
                    {
                        retval = resultRef.refValue.SheetId;
                    }
                    // Done with ResultRef - Free it too
                    Excel12v(xlFree, 1, &pResultRef, (XlOper12 *)IntPtr.Zero);
                }
                // CONSIDER: As a small optimisation, we could combine the two calls the xlFree. But then we'd have to manage an array here.
            }
            return(retval);
        }
        readonly XlOper12 **_callParams;     // Array with MaxCallParams Xloper12s
//        readonly XlString12** _callParamStrings; // Array with MaxCallParams XlString12s Only used for spill-over strings from long parameters

        public RtdWrapperHelper(string progId, object rtdWrapperOptions)
        {
            _progId            = progId;
            _rtdWrapperOptions = rtdWrapperOptions;

            // TODO: Can't easily use the regular marshallers, because we want to opt out of the memory management
            // TODO: But we should refactor this conversion code a bit

            int size       = Marshal.SizeOf(typeof(XlOper12));
            int stringSize = Marshal.SizeOf(typeof(XlString12)) + ((XlString12.MaxLength - 1) /* 1 char is in Data[1] */ * 2 /* 2 bytes per char */);

            _progIdXloper12            = (XlOper12 *)Marshal.AllocCoTaskMem(size);
            _progIdXloper12->xlType    = XlType12.XlTypeString;
            _progIdXloper12->pstrValue = (XlString12 *)Marshal.AllocCoTaskMem(stringSize);
            XlString12 *pdest     = _progIdXloper12->pstrValue;
            ushort      charCount = (ushort)Math.Min(progId.Length, XlString12.MaxLength);

            fixed(char *psrc = progId)
            {
                char *ps = psrc;
                char *pd = pdest->Data;

                for (int k = 0; k < charCount; k++)
                {
                    *(pd++) = *(ps++);
                }
            }

            pdest->Length = charCount;

            _emptyStringXloper12                    = (XlOper12 *)Marshal.AllocCoTaskMem(size);
            _emptyStringXloper12->xlType            = XlType12.XlTypeString;
            _emptyStringXloper12->pstrValue         = (XlString12 *)Marshal.AllocCoTaskMem(Marshal.SizeOf(typeof(XlString12)));
            _emptyStringXloper12->pstrValue->Length = 0;

            _errorValueXloper12           = (XlOper12 *)Marshal.AllocCoTaskMem(size);
            _errorValueXloper12->errValue = 15; // ExcelErrorValue
            _errorValueXloper12->xlType   = XlType12.XlTypeError;

            _resultXloper12         = (XlOper12 *)Marshal.AllocCoTaskMem(size);
            _resultXloper12->xlType = XlType12.XlTypeEmpty;

            _callParams    = (XlOper12 **)Marshal.AllocCoTaskMem(IntPtr.Size * MaxCallParams);
            _callParams[0] = _progIdXloper12;
            _callParams[1] = _emptyStringXloper12;

//            _callParamStrings = (XlString12**)Marshal.AllocCoTaskMem(stringSize * MaxCallParams);
        }