External C # Tool for SSMS - Read from stdin

I am trying to write an application that uses the functionality of the external tools of SQL Server Management Studio.

To specify an external tool, you can enter the path to the application and provide some arguments to navigate to the application via STDIN.

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

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

Is there anything I can do that can enable this, or am I stuck with a lot of windows?

Thank you in advance

0


source to share


1 answer


Horrible as it is, you can use Microsoft.VisualBasic.ApplicationServices to make this very 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 modify 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 Form1, drop a shortcut and some code to show that it works:

    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 gotten with this little snippet is that the command line arguments you get on first launch include the name of the application, but it is not included in subsequent launches.

0


source







All Articles