tags:

views:

687

answers:

1

Hi all,

I am trying to write an app that makes use of the external tools functionality of SQL Server Management Studio.

To specify an external tool, you can enter a path to the app and specify some arguments to pass to the app via STDIN.

Currently I just have a form that displays the arguments. Every time I run the external tool I get a new instance of the application.

Ideally I would like for the first time I run the tool to load the application, and each subsequent running to take the arguments from STDIN and do something with them WITHOUT creating a new instance of the app.

Is there anything I can do that could allow this, or am I stuck with lots of windows?

Thanks in advance

A: 

As horrible as it sounds, you can leverage Microsoft.VisualBasic.ApplicationServices to make this really simple (you can add a reference to Microsoft.VisualBasic in your c# project).

As a quick example, you can create a new C# WinForms project and alter Program.cs to look something like this:

class Program : WindowsFormsApplicationBase
{
    static Form1 mainForm = null;

    /// <summary>
    /// The main entry point for the application.
    /// </summary>
    [STAThread]
    static void Main(string[] commandline)
    {
        Application.EnableVisualStyles();
        Application.SetCompatibleTextRenderingDefault(false);
        Program prog = new Program();
        prog.MainForm = mainForm = new Form1();
        prog.Run(commandline);
    }

    public Program()
    {
        this.IsSingleInstance = true;
    }

    protected override void OnStartupNextInstance(StartupNextInstanceEventArgs eventArgs)
    {
        base.OnStartupNextInstance(eventArgs);
        mainForm.Startup(eventArgs.CommandLine.ToArray());
    }
}

Then in the Form1 throw a label on there and a little code to show it's working:

    public void Startup(string[] commandLine)
    {
        string output = "";
        foreach (string arg in commandLine)
            output += arg + "\n";

        label1.Text = output;
    }

    public Form1()
    {
        InitializeComponent();
        Startup(Environment.GetCommandLineArgs());
    }

The only gotcha with this little snippet is that the command line arguments you get on first launch include the application name, but it's not included on subsequent launches.

Steven Robbins