Ejemplo n.º 1
0
        /// <summary>
        /// Returns the buffer contents for a moniker.
        /// </summary>
        /// <returns>Buffer contents</returns>
        private string GetBufferContents(string fileName, out IVsTextStream srpStream)
        {
            Guid   CLSID_VsTextBuffer = new Guid("{8E7B96A8-E33D-11d0-A6D5-00C04FB67F6A}");
            string bufferContents     = "";

            srpStream = null;

            IVsRunningDocumentTable rdt = this.projectMgr.GetService(typeof(SVsRunningDocumentTable)) as IVsRunningDocumentTable;

            if (rdt != null)
            {
                IVsHierarchy      hier;
                IVsPersistDocData persistDocData;
                uint   itemid, cookie;
                bool   docInRdt = true;
                IntPtr docData  = IntPtr.Zero;
                int    hr       = NativeMethods.E_FAIL;
                try
                {
                    //Getting a read lock on the document. Must be released later.
                    hr = rdt.FindAndLockDocument((uint)_VSRDTFLAGS.RDT_ReadLock, fileName, out hier, out itemid, out docData, out cookie);
                    if (ErrorHandler.Failed(hr) || docData == IntPtr.Zero)
                    {
                        Guid iid = VSConstants.IID_IUnknown;
                        cookie   = 0;
                        docInRdt = false;
                        ILocalRegistry localReg = this.projectMgr.GetService(typeof(SLocalRegistry)) as ILocalRegistry;
                        ErrorHandler.ThrowOnFailure(localReg.CreateInstance(CLSID_VsTextBuffer, null, ref iid, (uint)CLSCTX.CLSCTX_INPROC_SERVER, out docData));
                    }

                    persistDocData = Marshal.GetObjectForIUnknown(docData) as IVsPersistDocData;
                }
                finally
                {
                    if (docData != IntPtr.Zero)
                    {
                        Marshal.Release(docData);
                    }
                }

                //Try to get the Text lines
                IVsTextLines srpTextLines = persistDocData as IVsTextLines;
                if (srpTextLines == null)
                {
                    // Try getting a text buffer provider first
                    IVsTextBufferProvider srpTextBufferProvider = persistDocData as IVsTextBufferProvider;
                    if (srpTextBufferProvider != null)
                    {
                        hr = srpTextBufferProvider.GetTextBuffer(out srpTextLines);
                    }
                }

                if (ErrorHandler.Succeeded(hr))
                {
                    srpStream = srpTextLines as IVsTextStream;
                    if (srpStream != null)
                    {
                        // QI for IVsBatchUpdate and call FlushPendingUpdates if they support it
                        IVsBatchUpdate srpBatchUpdate = srpStream as IVsBatchUpdate;
                        if (srpBatchUpdate != null)
                        {
                            ErrorHandler.ThrowOnFailure(srpBatchUpdate.FlushPendingUpdates(0));
                        }

                        int lBufferSize = 0;
                        hr = srpStream.GetSize(out lBufferSize);

                        if (ErrorHandler.Succeeded(hr))
                        {
                            IntPtr dest = IntPtr.Zero;
                            try
                            {
                                // Note that GetStream returns Unicode to us so we don't need to do any conversions
                                dest = Marshal.AllocCoTaskMem((lBufferSize + 1) * 2);
                                ErrorHandler.ThrowOnFailure(srpStream.GetStream(0, lBufferSize, dest));
                                //Get the contents
                                bufferContents = Marshal.PtrToStringUni(dest);
                            }
                            finally
                            {
                                if (dest != IntPtr.Zero)
                                {
                                    Marshal.FreeCoTaskMem(dest);
                                }
                            }
                        }
                    }
                }
                // Unlock the document in the RDT if necessary
                if (docInRdt && rdt != null)
                {
                    ErrorHandler.ThrowOnFailure(rdt.UnlockDocument((uint)(_VSRDTFLAGS.RDT_ReadLock | _VSRDTFLAGS.RDT_Unlock_NoSave), cookie));
                }

                if (ErrorHandler.Failed(hr))
                {
                    // If this failed then it's probably not a text file.  In that case,
                    // we just read the file as a binary
                    bufferContents = File.ReadAllText(fileName);
                }
            }
            return(bufferContents);
        }
Ejemplo n.º 2
0
        /// <summary>
        /// This is used to get the content of a specific topic file if it is open in an editor so that the
        /// current content is displayed for it in the topic previewer control.
        /// </summary>
        /// <param name="sender">The sender of the event</param>
        /// <param name="e">The event arguments</param>
        private void ucTopicPreviewer_TopicContentNeeded(object sender, TopicContentNeededEventArgs e)
        {
            ThreadHelper.ThrowIfNotOnUIThread();

            IVsHierarchy      hier;
            IVsPersistDocData persistDocData;
            IVsTextStream     srpStream;
            IntPtr            docData = IntPtr.Zero;
            uint itemid, cookie = 0;
            int  hr = VSConstants.E_FAIL;

            IVsRunningDocumentTable rdt = Utility.GetServiceFromPackage <IVsRunningDocumentTable,
                                                                         SVsRunningDocumentTable>(true);

            if (rdt == null)
            {
                return;
            }

            try
            {
                // Getting a read lock on the document.  This must be released later.
                hr = rdt.FindAndLockDocument((uint)_VSRDTFLAGS.RDT_ReadLock, e.TopicFilename, out hier,
                                             out itemid, out docData, out cookie);

                if (ErrorHandler.Failed(hr) || docData == IntPtr.Zero)
                {
                    return;
                }

                persistDocData = Marshal.GetObjectForIUnknown(docData) as IVsPersistDocData;

                // Try to get the Text lines
                IVsTextLines srpTextLines = persistDocData as IVsTextLines;

                if (srpTextLines == null)
                {
                    // Try getting a text buffer provider first
                    IVsTextBufferProvider srpTextBufferProvider = persistDocData as IVsTextBufferProvider;

                    if (srpTextBufferProvider != null)
                    {
                        hr = srpTextBufferProvider.GetTextBuffer(out srpTextLines);
                    }
                }

                if (ErrorHandler.Succeeded(hr))
                {
                    srpStream = srpTextLines as IVsTextStream;

                    if (srpStream != null)
                    {
                        IVsBatchUpdate srpBatchUpdate = srpStream as IVsBatchUpdate;

                        if (srpBatchUpdate != null)
                        {
                            ErrorHandler.ThrowOnFailure(srpBatchUpdate.FlushPendingUpdates(0));
                        }

                        int lBufferSize = 0;
                        hr = srpStream.GetSize(out lBufferSize);

                        if (ErrorHandler.Succeeded(hr))
                        {
                            IntPtr dest = IntPtr.Zero;

                            try
                            {
                                // GetStream() returns Unicode data so we need to double the buffer size
                                dest = Marshal.AllocCoTaskMem((lBufferSize + 1) * 2);
                                ErrorHandler.ThrowOnFailure(srpStream.GetStream(0, lBufferSize, dest));

                                // Get the contents
                                e.TopicContent = Marshal.PtrToStringUni(dest);
                            }
                            finally
                            {
                                if (dest != IntPtr.Zero)
                                {
                                    Marshal.FreeCoTaskMem(dest);
                                }
                            }
                        }
                    }
                }
            }
            finally
            {
                if (docData != IntPtr.Zero)
                {
                    Marshal.Release(docData);
                }

                if (cookie != 0)
                {
                    ErrorHandler.ThrowOnFailure(rdt.UnlockDocument((uint)_VSRDTFLAGS.RDT_ReadLock, cookie));
                }
            }
        }