[GIS] How to programmatically get a reference to an ICommand inside ArcGIS

arcobjects

I'm having problems trying to get a ICommand using the method ICommandBars.Find. The ICommand is there, but it's returning null to me.

Any ideas?

    public static ICommandItem GetCommandItemByName(string progId)
    {
        UID uid = new UIDClass();
        uid.Value = progId;

        Type t = Type.GetTypeFromProgID("esriFramework.AppRef");
        object o = Activator.CreateInstance(t);

        IApplication application = o as IApplication;
        ICommandBars commandBars = application.Document.CommandBars;
        ICommandItem commandItem = commandBars.Find(uid,false,true);

        return commandItem;
    }

The result is always null :(.

I need to get a reference to this specific command because it contains some fields and properties that I specified (a IWorkspace object, if it matters)

Thanks for your help!

Best Answer

Hmm, that's interesting, seems like I remember this working differently before 10.0. I just did a test and even though the command is on a visible toolbar, another command was unable to find it. If I set noCreate to false it found it though.

At any rate, instead of having commands find each other, I recommend using an application extension to manage data that needs to be shared among command/tools. Each command would set a reference the extension in ICommand.Oncreate.