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);
        }
        // This is the function we register with Excel
        // All the IntPtrs are XLOPER12*
        public unsafe XlOper12 *RtdWrapper(XlOper12 *topic1, XlOper12 *topic2, XlOper12 *topic3, XlOper12 *topic4,
                                           XlOper12 *topic5, XlOper12 *topic6, XlOper12 *topic7, XlOper12 *topic8)
        {
            var callParamIndex = 2;

            _callParams[2] = topic1;
            _callParams[3] = topic2;
            _callParams[4] = topic3;
            _callParams[5] = topic4;
            _callParams[6] = topic5;
            _callParams[7] = topic6;
            _callParams[8] = topic7;
            _callParams[9] = topic8;

            callParamIndex = 9;

            int countParams = LastNonMissingIndex(_callParams, callParamIndex) + 1;

            try
            {
                int xlReturn = XlCallImpl.Excel12v(XlCallImpl.xlfRtd, countParams, _callParams, _resultXloper12);
                if (xlReturn == 0) // xlReturnSuccess)
                {
                    _resultXloper12->xlType |= XlType12.XlBitXLFree;
                    return(_resultXloper12);
                }
            }
            catch (Exception e)
            {
                Logger.Initialization.Error(e, "RTD call from wrapper failed with Exception");
            }
            return(_errorValueXloper12);
        }
Exemple #3
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 #4
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);
        }
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);
        }
 public unsafe static IntPtr GetCallerSheetId12()
 {
     IntPtr retval = IntPtr.Zero;
     XlOper12 resultOper = new XlOper12();
     XlOper12* pResultOper = &resultOper;
     int xlReturn;
     xlReturn = Excel12v(xlfCaller, 0, (XlOper12**)IntPtr.Zero, pResultOper);
     if (xlReturn == 0)
     {
         if (resultOper.xlType == XlType12.XlTypeReference)
         {
             retval = resultOper.refValue.SheetId;
             Excel12v(xlFree, 1, &pResultOper, (XlOper12*)IntPtr.Zero);
         }
     }
     return retval;
 }
        internal unsafe static void SetExcelReference12(XlOper12 *pOper, XlOper12.XlMultiRef12 *pMultiRef, object /*ExcelReference*/ r)
        {
            IntPtr sheetId = ExcelReferenceGetSheetId(r);

            int[][] rects     = ExcelReferenceGetRectangles(r);
            int     rectCount = rects.GetLength(0);

            pOper->xlType           = XlType12.XlTypeReference;
            pOper->refValue.SheetId = sheetId;

            pOper->refValue.pMultiRef        = pMultiRef;
            pOper->refValue.pMultiRef->Count = (ushort)rectCount;

            XlOper12.XlRectangle12 *pRectangles = (XlOper12.XlRectangle12 *)(&pOper->refValue.pMultiRef->Rectangles);

            for (int i = 0; i < rectCount; i++)
            {
                pRectangles[i].RowFirst    = rects[i][0];
                pRectangles[i].RowLast     = rects[i][1];
                pRectangles[i].ColumnFirst = rects[i][2];
                pRectangles[i].ColumnLast  = rects[i][3];
            }
        }