コード例 #1
0
        public override ContextMenuItem[] GetContextMenuItems()
        {
            try
            {
                if (this.IsNewInstance)
                {
                    return(new ContextMenuItem[0]);
                }

                ManagementClass  localObj = this.mgmtClassObj;
                MethodCollection methods  = null;

                try
                {
                    int methodCount = localObj.Methods.Count;
                }
                catch (Exception)
                {
                    ConnectionOptions connectOpts = new ConnectionOptions(
                        "",                 //locale
                        this.connectAs,     //username
                        this.password,      //password
                        "",                 //authority
                        ImpersonationLevel.Impersonate,
                        AuthenticationLevel.Connect,
                        true,                 //enablePrivileges
                        null                  //context
                        );

                    ManagementScope scope = (this.serverName == WmiHelper.DNS2UNC(Dns.GetHostName())) ?
                                            new ManagementScope(WmiHelper.MakeNSPath(serverName, nsName)):
                                            new ManagementScope(WmiHelper.MakeNSPath(serverName, nsName), connectOpts);



                    localObj = new ManagementClass(scope,
                                                   new ManagementPath(mgmtClassObj.Path.Path),
                                                   new ObjectGetOptions(null,
                                                                        true) //use amended
                                                   );
                }
                methods = localObj.Methods;

                ContextMenuItem[] theMenu = new ContextMenuItem[methods.Count + 2];

                int i = 0;

                //TODO: retrieve non-static methods for the class and add them to context menu
                MethodCollection.MethodEnumerator methEnum = methods.GetEnumerator();

                while (methEnum.MoveNext())
                {
                    Method meth = methEnum.Current;

                    if (WmiHelper.IsImplementedMethod(meth) && !WmiHelper.IsStaticMethod(meth))
                    {
                        //add method name to context menu
                        theMenu[i++] = new ContextMenuItem("&" + i.ToString() + " " + meth.Name + "...",                 //TODO: hotkey???
                                                           new EventHandler(OnExecuteMethod));
                    }
                }

                if (IsNewInstance)
                {
                    theMenu[i] = new ContextMenuItem(WMISys.GetString("WMISE_InstNode_SaveNewInstanceCM"),
                                                     new EventHandler(CommitNewInstance));
                }

                return(theMenu);
            }

            catch (Exception exc)
            {
                MessageBox.Show(WMISys.GetString("WMISE_Exception", exc.Message, exc.StackTrace));
                return(null);
            }
        }
コード例 #2
0
        /// <summary>
        /// Context menu for a class object contains static method names
        /// </summary>
        public override ContextMenuItem[] GetContextMenuItems()
        {
            try
            {
                //System.Threading.Thread.CurrentThread.ApartmentState = ApartmentState.MTA;

                ManagementClass  localObj = mgmtObj;
                MethodCollection methods  = null;

                try
                {
                    int methodCount = localObj.Methods.Count;
                }
                catch (Exception)
                {
                    ConnectionOptions connectOpts = new ConnectionOptions(
                        "",                 //locale
                        this.connectAs,     //username
                        this.password,      //password
                        "",                 //authority
                        ImpersonationLevel.Impersonate,
                        AuthenticationLevel.Connect,
                        true,                 //enablePrivileges
                        null                  //context
                        );

                    ManagementScope scope = (this.serverName == WmiHelper.DNS2UNC(Dns.GetHostName())) ?
                                            new ManagementScope(WmiHelper.MakeNSPath(serverName, nsName)):
                                            new ManagementScope(WmiHelper.MakeNSPath(serverName, nsName), connectOpts);


                    localObj = new ManagementClass(scope,
                                                   new ManagementPath(path),
                                                   new ObjectGetOptions(null,
                                                                        true) //use amended
                                                   );
                }

                methods = localObj.Methods;

                /*
                 * string propNames = "";
                 * foreach (System.Management.Property prop in mgmtObj.Properties)
                 * {
                 *      propNames += prop.Name + "\n";
                 * }
                 * MessageBox.Show(propNames);
                 *
                 * MessageBox.Show("class has " + mgmtObj.Properties.Count + " properties");
                 *
                 * string methNames = "";
                 * foreach (Method meth in methods)
                 * {
                 *      methNames += meth.Name + "\n";
                 * }
                 * MessageBox.Show(methNames);
                 *
                 */
                Project[] projects      = VSUtils.GetProjects(GetNodeSite());
                bool      enableCodeGen = false;
                if (projects != null && projects.Length != 0)
                {
                    ICodeGenerator codeGen = VSUtils.MapProjectGuidToCodeGenerator(new Guid(projects[0].Kind));
                    if (codeGen == null)
                    {
                        MessageBox.Show("codeGen is null, project is: " + projects[0].Kind);
                    }
                    enableCodeGen = (codeGen != null);
                }

                ContextMenuItem[] theMenu = new ContextMenuItem[methods.Count + 2];

                //theMenu[0] = new ContextMenuItem("&View All Instances...", new EventHandler(OnExpandAll));
                //theMenu[1] = new ContextMenuItem("&Filter Instances...", new EventHandler(OnAddInstanceFilter));

                theMenu[0] = new ContextMenuItem(WMISys.GetString("WMISE_ClassNode_CreateNewInstance"),
                                                 new EventHandler(OnCreateNewInstance),
                                                 false);

                theMenu[1] = new ContextMenuItem(WMISys.GetString("WMISE_ClassNode_GenerateWrapper"),
                                                 new EventHandler(OnGenerateWrapper),
                                                 enableCodeGen);

                int i = 2;

                //Retrieve static methods for the class and add them to context menu
                MethodCollection.MethodEnumerator methEnum = methods.GetEnumerator();

                while (methEnum.MoveNext())
                {
                    Method meth = methEnum.Current;

                    if (WmiHelper.IsStaticMethod(meth) && WmiHelper.IsImplementedMethod(meth))
                    {
                        //add method name to context menu
                        theMenu[i] = new ContextMenuItem("&" + i.ToString() + " " + meth.Name + "...",                 //TODO: hotkey???
                                                         new EventHandler(OnExecuteMethod));
                        i++;
                    }
                }

                methEnum = null;

                return(theMenu);
            }

            /*
             * catch (ManagementException exc)
             * {
             * MessageBox.Show("ManagementException hr is " + exc.ErrorCode.ToString());
             * MessageBox.Show(WMISys.GetString("WMISE_Exception", exc.Message, exc.StackTrace));
             *
             * return null;
             * }
             */
            catch (Exception exc)
            {
                MessageBox.Show(WMISys.GetString("WMISE_Exception", exc.Message, exc.StackTrace));
                return(null);
            }
        }