2

I was hoping someone here might be able to help me out with this. I'm not the most experienced programmer but I'm making progress on a project.

I've got a need to programmatically interact with Visual Studio. Some success has been had using EnvDTE Interop stuff, but it seems that some of what I need to do needs to be done inside VS so I'm attempting to utilize a VSPackage MenuCommand to do various things. Sorry the vagueness.

I'm currently successfully creating a custom MenuCommand with a VSPackage extension, and also am able to trigger that MenuCommand programmatically from another application using the DTE.

What I'm wondering is: is it possible to define a MenuCommand that CAN take arguments passed along to it from the triggering external application?

Using the VS Package Template in Visual Studio 2012 using the Menu Command option, all my code lives inside this method:

private void MenuItemCallback(object sender, EventArgs e)
    {
        // my code...
    }

There is obviously a lot of other auto-generated code plumbing this all together, but all MY code lives in this method. Is there a way to alter this method so that it will allow parameters to be passed to it? What other changes must I make to the other files to declare/register this differently-functioning method once I do so (if I can)?

For example:

static void Main(string[] args)    
{
    Type visualStudioType = Type.GetTypeFromProgID("VisualStudio.DTE.11.0");
    DTE dte = Activator.CreateInstance(visualStudioType) as DTE;
    dte.MainWindow.Visible = true;
    dte.ExecuteCommand("myCommand");        
}

This works. But what I'd like to do is change that last command to:

dte.ExecuteCommand("myCommand", "myArguments");

When I attempt to do something like this, I receive the following exception: "Command \"myCommand\" does not accept arguments or switches."

Sorry if I'm not being clear. Any help would be greatly appreciated.

  • I got this working. I want to really thank Carlos Quintero for his answer. While it didn't directly solve my problem I was able to eventually find out what I needed to do. I still don't completely understand but it was most likely related to the template auto-generating the command as a MenuCommand instead of as a OleMenuCommand. After changing it to an OleMenuCommand and casting it's EventArgs as OleMenuCmdEventArgs I am able to access the InValue which holds the parameters I pass in with the command. Huzzah! – James Connolly Dec 06 '14 at 20:15

1 Answers1

0

Commands created from add-ins accept parameters by default.

Commands created from packages need to specify the <CommandFlag>AllowParams</CommandFlag> when defining the command in the .vsct file. See: http://msdn.microsoft.com/en-us/library/bb491716.aspx

And see also this thread:

IOleComandTarget::exec for commands with parameters https://social.msdn.microsoft.com/Forums/en-US/134983e8-049c-40e1-a212-312fa637698b/iolecomandtargetexec-for-commands-with-parameters?forum=vsx

Then, it should work, either using dte.ExecuteCommand or dte.Commands.Raise(...). See:

HOWTO: Pass parameters programmatically to a command from a Visual Studio add-in http://www.visualstudioextensibility.com/articles/add-ins/

Carlos Quintero
  • 4,300
  • 1
  • 11
  • 18
  • Thanks so much for your reply. I guess my question didn't make this clear, but I'm more having trouble with how to define a Command in such a way that it will TAKE parameters. Like, I can successfully pass parameters to build-in commands that take them, but I've defined my own Command using an MSDN walkthrough and I'm not sure how to alter it to allow the Command to accept parameters. Does that make sense? Thanks again. – James Connolly Dec 05 '14 at 23:55
  • I see. I have expanded my answer – Carlos Quintero Dec 06 '14 at 09:28
  • Thanks a lot. Your edit gave me enough information to keep with it and work it out. – James Connolly Dec 06 '14 at 20:16
  • Another requirement is to set ParametersDescription on the newly created OleMenuCommand. – Sergey Vlasov May 14 '19 at 20:06