views:

307

answers:

3

I've read several articles that tell you how to add text to the output window in visual studio from within an Add-On (specifically, a visual studio 2008 integration package, via the visual studio 2008 SDK 1.1), but no examples of how to read text from the output window. My goal is to parse text from the debug output window while debugging a certain application (TRACE output and possibly stdin/stdout). The IVsOutputWindowPane interface has no methods for reading in text from the output window. The documentation seems to imply that it is possible, but it doesn't provide an example:

http://msdn.microsoft.com/en-us/library/bb166236(VS.80).aspx

Quote: In addition, the OutputWindow and OutputWindowPane objects add some higher-level functionality to make it easier to enumerate the Output window panes and to retrieve text from the panes.

Preferably I'd like to be able to subscribe to an event that fires when a new line of text arrives, similar to a StreamReader's asynchronous reads.

+1  A: 

The default behavior (when you don’t set the listener explicitly) of VS is to display trace massages in the debugger output window, which you appreciate if you want a simple solution and do no other actions with the massages.

Unfortunately this is not your case. So you have to define a trace listener to send (and store) your trace massages where you then will be able to read them. The trace listener could be a file (for example XML) or you can create a custom listener by deriving a class from the base class TraceListener if you don't want to bother yourself with an additional file.

Machta
How would a trace listener capture what goes to the output window?
John Saunders
Nice edit, but still, a tracelistener would only capture trace messages. That's not all that goes into the Output window.
John Saunders
OK, that’s true. If this is enough for the user he can use this solution, but I admit this is not the way of acquiring everything written in the Output window.
Machta
I would be satisfied with just being able to capture trace output - but it seems the documentation is hinting that direct access to the output window pane text is possible (see edit to my original post). That would be the preferable solution if it were actually possible. Is the documentation simply incorrect/misleading? That wouldn't surprise me.
Jeremy Bell
You say that it dosn't provide an example... I can see at least 4.
Machta
So If I understand you well, you don't need to use this approach, but you are curious, whether the documentation contains any mistakes.
Machta
I tried to do something with the IVsOutputWindow, so I added the using directive and tried to add Microsoft.VisualStudio.Shell.Interop.dll assembly reference, but the assembly was not in the VS add reference .Net list... I don't even dare imagine how many more bugs/mistakes/problems you will have to pass and solve to complete this seems-to-be-easy question... I surrender. Good luck to you brother, you will need
Machta
The examples all show how to write text to the output window, but not how to read text.I did some experimentation with getting the OutputWindowPane. OutputWindowPane has a TextDocument property, but TextDocument doesn't seem to have any way of getting the text from the document directly. The only way I could figure out was to call OutputWindowPane.TextDocument.Selection.SelectAll() and then OutputWindowPane.TextDocument.Selection.Text. I had to queue up a background worker to do this every few seconds, check if there was new text, and parse the new text. There has to be a better way...
Jeremy Bell
+1  A: 

I don't know that what you ask is possible. But, you can register your add-in as a debugger for your application so that you get the output the trace messages. These are typically routed to OutputDebugString, and can be captured as described in this article: http://www.drdobbs.com/showArticle.jhtml?articleID=184410719. It does not give you the normal output, only debug, but it does not depend on the technology of the debugged application.

jdv
A: 

The solution on this page selects the text in order to read it. I'm hoping there's a better way. http://stackoverflow.com/questions/134796/how-to-automatically-stop-visual-c-build-at-first-compile-error

Private Sub OutputWindowEvents_OnPaneUpdated(ByVal pPane As OutputWindowPane) Handles OutputWindowEvents.PaneUpdated
    pPane.TextDocument.Selection.SelectAll()
    Dim Context As String = pPane.TextDocument.Selection.Text
    pPane.TextDocument.Selection.EndOfDocument()
End Sub
Jon