locked
Writing to Visual Studio Output window from standalone exe RRS feed

  • General discussion

  • Hi,

    I have a little bit of a problem:

    I've written a Visual Studio Addin which runs when you save a custom defined text file -
    an Addin that generates code from the file being saved.

    To do the actual code generation, the Addin executes a standalone exe with the appopriate
    command line arguments (input file name) by doing a Process.Start().

    Now, my requirement is to make the output of this exe to be visible in the VS Output window.
    ie. I am doing a Console.Write() in the exe, but this output is visible in the Console window.

    I have tried Debug.Write & Trace.Write, but these only work in the Addin (basically the code project which is active in VS at the time)
    and not from any standalone exe.

    Any suggestions? Please help.

    Regards,
    Santosh
    Monday, February 8, 2010 3:22 PM

All replies

  • I needed to do something similar recently so a dedicated Pane but it can be reused here.

    The easiest way might be to ask the StdOut to be redirected when your Addin launches your EXE. That way, you can capture all of the output from your Addin (or anything it spawns, etc.) and then just output it in one go at the end. This just dumps a DIR of my C:\ Drive into the 'General' pane:

    System.Diagnostics.Process theCommand = new System.Diagnostics.Process();
    
    theCommand.StartInfo.FileName = @"cmd.exe";
    
    theCommand.StartInfo.UseShellExecute = false;
    
    theCommand.StartInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Normal;
    
    theCommand.StartInfo.CreateNoWindow = false;
    
    theCommand.StartInfo.RedirectStandardInput = false;
    
    theCommand.StartInfo.RedirectStandardOutput = true;
    
    theCommand.StartInfo.Arguments = @"/C dir C:\";
    
    theCommand.Start();
    
    string contentsFromSpawnedApp = theCommand.StandardOutput.ReadToEnd();
    
    theCommand.Close();
    

    Then you need to dump the contents to the pane. I'll assume the 'General' pane here:

     

    ServiceProvider sp = new ServiceProvider((Microsoft.VisualStudio.OLE.Interop.IServiceProvider)_applicationObject); 
                        Microsoft.VisualStudio.Shell.Interop.IVsOutputWindow output = sp.GetService(typeof(SVsOutputWindow)) as IVsOutputWindow;
    
    if (output != null)
    {
        Guid generalPaneGuid = VSConstants.GUID_OutWindowGeneralPane;
    
        IVsOutputWindowPane thePane = null;
    
        output.GetPane(ref generalPaneGuid, out thePane);
    
        if (thePane == null)
        {
            output.CreatePane(ref generalPaneGuid, "General", 1, 1);
        }
    
        output.GetPane(ref generalPaneGuid, out thePane);
    
        thePane.OutputStringThreadSafe(contentsFromSpawnedApp);
    }
    

    The disadvantage of this is that it dumps everything out in one go at the end. If you've a long running EXE, or loads and loads of text, you'll probably need to spawn a thread to probe the redirected StdOut.

    I needed to add three references to get that working:

    Microsoft.VisualStudio.OLE.Interop
    Microsoft.VisualStudio.Shell
    Microsoft.VisualStudio.Shell.Interop
    Tuesday, February 9, 2010 3:20 AM