locked
SDK Package: OleMenuCommandService..FindCommand always returns NULL. RRS feed

  • Question

  • kind of extension : SDK Package
    Visal Studio version : Visual Studio 2008
    Windows version : Windows XP Professional version 2002 Service Pack 3
    .NET language : C#


    code to reproduce the problem :

    var menuCommandService = GetService(typeof(IMenuCommandService)) as OleMenuCommandService;

    MenuCommand
    start = menuCommandService.FindCommand(new CommandID(VSConstants.GUID_VSStandardCommandSet97, (int)Microsoft.VisualStudio.VSConstants.VSStd97CmdID.Start));

    start.Enabled =
    false;

    The problem happens on the developer machine.
     

    Friday, September 18, 2009 2:45 PM

Answers

  • Hi  Jean-Michel,

    Sorry for the delay.

    Based on my research and consulting, the only commands that you will be able to find via OleMenuCommandService are ones that were created in managed code and registered with the service.  The shell commands (and a vast majority of VS commands in general) do not meet this criteria.  Please see this code:

      OleMenuCommandService mcs = GetService(typeof(IMenuCommandService)) as OleMenuCommandService;
      CommandID menuCommandID = new CommandID(GuidList.guidVSPackage2CmdSet, (int)PkgCmdIDList.cmdidMyCommand);
      MenuCommand menuItem = new MenuCommand(MenuItemCallback, menuCommandID );
      mcs.AddCommand( menuItem );
      menuItem = mcs.FindCommand(menuCommandID) as MenuCommand;
    


    menuItem will return the command in your GuidList after excuting FindCommand function, because this command are registered with OleMenuCommandService. In general we can't get the shell command, for the command system is not set up to allow arbitrary third parties to change visibility on things.

    Best Regards,
    Nancy 
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.
    • Marked as answer by Nancy Shao Monday, September 28, 2009 2:32 AM
    Thursday, September 24, 2009 5:49 AM

All replies

  • Hi Jean-Michel,

    Based on my understanding, menuCommandService will return NULL after GetService function, you'd better debug your code and check it. I think we need to use GetService function with special IComponent interface as following shows or we can't get the IMenuCommandService:

            public override void Initialize(IComponent comp)
            {
                base.Initialize(comp);
    
                IMenuCommandService mcs = (IMenuCommandService)comp.Site.
                            GetService(typeof(IMenuCommandService));
                MenuCommand mc = new MenuCommand(new EventHandler(OnF1Help), StandardCommands.F1Help);
                mc.Enabled = true;
                mc.Visible = true;
                mc.Supported = true;
                mcs.AddCommand(mc);
                System.Windows.Forms.MessageBox.Show("Initialize() has been invoked.");
            }
    


    Please refer to this link for more information: http://msdn.microsoft.com/en-us/library/system.componentmodel.design.menucommand.aspx

    Best Regards,
    Nancy
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.
    Monday, September 21, 2009 8:11 AM
  • Hello Nancy,

    I don't have any problem getting the IMenuCommandService. It is never null. The problem is the return of the FindCommand function, which is always null.
    Monday, September 21, 2009 11:25 AM
  • Hi  Jean-Michel,

    Sorry for the delay.

    Based on my research and consulting, the only commands that you will be able to find via OleMenuCommandService are ones that were created in managed code and registered with the service.  The shell commands (and a vast majority of VS commands in general) do not meet this criteria.  Please see this code:

      OleMenuCommandService mcs = GetService(typeof(IMenuCommandService)) as OleMenuCommandService;
      CommandID menuCommandID = new CommandID(GuidList.guidVSPackage2CmdSet, (int)PkgCmdIDList.cmdidMyCommand);
      MenuCommand menuItem = new MenuCommand(MenuItemCallback, menuCommandID );
      mcs.AddCommand( menuItem );
      menuItem = mcs.FindCommand(menuCommandID) as MenuCommand;
    


    menuItem will return the command in your GuidList after excuting FindCommand function, because this command are registered with OleMenuCommandService. In general we can't get the shell command, for the command system is not set up to allow arbitrary third parties to change visibility on things.

    Best Regards,
    Nancy 
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.
    • Marked as answer by Nancy Shao Monday, September 28, 2009 2:32 AM
    Thursday, September 24, 2009 5:49 AM