示例#1
0
        /// <summary>
        /// Fetches the next group of strings.
        /// </summary>
        public int Next(string[] buffer, int count)
        {
            // can't use simple interface after calling this method.
            m_fetched = -1;

            try
            {
                int fetched = 0;

                IntPtr pBuffer = Marshal.AllocCoTaskMem(IntPtr.Size * count);

                try
                {
                    int error = m_enumerator.RemoteNext(
                        count,
                        pBuffer,
                        out fetched);

                    if (error >= 0 && fetched > 0)
                    {
                        IntPtr[] pStrings = new IntPtr[m_fetched];
                        Marshal.Copy(pBuffer, pStrings, 0, fetched);

                        for (int ii = 0; ii < fetched; ii++)
                        {
                            m_buffer[ii] = Marshal.PtrToStringUni(pStrings[ii]);
                            Marshal.FreeCoTaskMem(pStrings[ii]);
                        }
                    }

                    return(fetched);
                }
                finally
                {
                    Marshal.FreeCoTaskMem(pBuffer);
                }
            }
            catch (Exception e)
            {
                // some (incorrect) implementations return E_FAIL at the end of the list.
                if (Marshal.GetHRForException(e) == ResultIds.E_FAIL)
                {
                    return(0);
                }

                throw ComUtils.CreateException(e, "IEnumString.RemoteNext");
            }
        }
示例#2
0
        /// <summary>
        /// Fetches the next string (returns null if no more data).
        /// </summary>
        public string Next()
        {
            // return null if at end of list.
            if (m_fetched == -1)
            {
                return(null);
            }

            // see if next item is available.
            if (m_position < m_fetched)
            {
                return(m_buffer[m_position++]);
            }

            m_position = 0;

            try
            {
                // fetch next batch.
                m_fetched = 0;

                IntPtr pBuffer = Marshal.AllocCoTaskMem(IntPtr.Size * m_buffer.Length);

                try
                {
                    int error = m_enumerator.RemoteNext(m_buffer.Length, pBuffer, out m_fetched);

                    if (error < 0 || m_fetched == 0)
                    {
                        return(null);
                    }

                    IntPtr[] pStrings = new IntPtr[m_fetched];
                    Marshal.Copy(pBuffer, pStrings, 0, m_fetched);

                    for (int ii = 0; ii < m_fetched; ii++)
                    {
                        m_buffer[ii] = Marshal.PtrToStringUni(pStrings[ii]);
                        Marshal.FreeCoTaskMem(pStrings[ii]);
                    }
                }
                finally
                {
                    Marshal.FreeCoTaskMem(pBuffer);
                }

                // check if end of list.
                if (m_fetched == 0)
                {
                    m_fetched = -1;
                    return(null);
                }

                // return first item.
                return(m_buffer[m_position++]);
            }
            catch (Exception e)
            {
                if (ComUtils.IsUnknownError(e, ResultIds.E_FAIL))
                {
                    throw ComUtils.CreateException(e, "IEnumString.RemoteNext");
                }

                // some (incorrect) implementations return E_FAIL at the end of the list.
                m_fetched = -1;
                return(null);
            }
        }