public static ManagementClass GetClassObject(ManagementObject objIn, string user, string pw) { if (objIn == null) { throw new ArgumentException(); } try { ManagementPath path = objIn.Path; if (path.IsClass) { return((ManagementClass)objIn); } ManagementPath classPath = new ManagementPath(path.NamespacePath + ":" + path.ClassName); ObjectGetOptions options = new ObjectGetOptions(null, true); ConnectionOptions connectOpts = new ConnectionOptions( "", //locale user, //username pw, //password "", //authority ImpersonationLevel.Impersonate, AuthenticationLevel.Connect, true, //enablePrivileges null //context ); ManagementScope scope = (path.Server == WmiHelper.DNS2UNC(Dns.GetHostName())) ? new ManagementScope(path.NamespacePath) : new ManagementScope(path.NamespacePath, connectOpts); ManagementClass classObj = new ManagementClass(scope, classPath, options); return(classObj); } catch (Exception) { return(null); } }
public static ManagementObject GetClassObject(string server, string ns, string className, string user, string pw) { if (ns == string.Empty || className == string.Empty) { throw new ArgumentException(); } try { ManagementPath classPath = new ManagementPath(WmiHelper.MakeClassPath(server, ns, className)); ObjectGetOptions options = new ObjectGetOptions(null, true); ConnectionOptions connectOpts = new ConnectionOptions( "", //locale user, //username pw, //password "", //authority ImpersonationLevel.Impersonate, AuthenticationLevel.Connect, true, //enablePrivileges null //context ); ManagementScope scope = (server == WmiHelper.DNS2UNC(Dns.GetHostName())) ? new ManagementScope(WmiHelper.MakeNSPath(server, ns)) : new ManagementScope(WmiHelper.MakeNSPath(server, ns), connectOpts); ManagementObject classObj = new ManagementObject(scope, classPath, options); return(classObj); } catch (Exception e) { MessageBox.Show(WMISys.GetString("WMISE_Exception", e.Message, e.StackTrace)); return(null); } }
private bool EnumNamespaces(string parent, int num) //recursively adds namespaces to the drop-down box { try { //show the node TreeNode dummy = new TreeNode(""); TreeNode[] children = new TreeNode[] { dummy }; TreeNode nsNode = new TreeNode(parent, //(int)schema_icons.SCHEMA_NS_CLOSED, //(int)schema_icons.SCHEMA_NS_CLOSED, children); nsNode.Collapse(); this.Nodes.Insert(num, nsNode); string nsPath = WmiHelper.MakeClassPath(server, parent, "__NAMESPACE"); string nsScopePath = WmiHelper.MakeNSPath(server, parent); ConnectionOptions connectOpts = new ConnectionOptions("", //locale this.connectAs, //username this.password, //password "", //authority ImpersonationLevel.Impersonate, AuthenticationLevel.Connect, true, //enablePrivileges null //context ); ManagementScope scope = (this.server == WmiHelper.DNS2UNC(Dns.GetHostName())) ? new ManagementScope(nsScopePath): new ManagementScope(nsScopePath, connectOpts); ManagementClass nsClass = new ManagementClass(scope, new ManagementPath(nsPath), new ObjectGetOptions(null, true)); ManagementObjectCollection subNSCollection = nsClass.GetInstances(); IEnumerator eInstances = ((IEnumerable)subNSCollection).GetEnumerator(); while (eInstances.MoveNext()) { num++; ManagementObject obj = (ManagementObject)eInstances.Current; PropertyCollection props = (PropertyCollection)obj.Properties; string NameOut = ""; string curName = props["Name"].Value.ToString(); //skip localized namespace //NOTE: this assumes that localized namespaces are always leaf if (curName.ToUpper().IndexOf("MS_", 0) == 0) { continue; } //skip root\security namespace (we don't want to expose it) if (curName.ToUpper() == "SECURITY" && parent.ToUpper() == "ROOT") { continue; } //skip root\directory\ldap namespace (BUGBUG: change this in Beta2 when we can do asynchronous class enumerations) if (curName.ToUpper() == "LDAP" && parent.ToUpper() == "ROOT\\DIRECTORY") { continue; } if (parent != "") { NameOut = parent + "\\" + curName; } else { NameOut = curName; } EnumNamespaces(NameOut, num); } return(true); } catch (Exception e) { MessageBox.Show(WMISys.GetString("WMISE_Exception", e.Message, e.StackTrace)); return(false); } }
internal bool ShowClasses(TreeNode curNode) { try { //check that this is a namespace: if (curNode.Parent != null) { return(false); } this.Cursor = Cursors.WaitCursor; curNode.Nodes.Clear(); curNSExpanded = curNode; curClassList.Clear(); //spawn a thread that would handle cancel dialog //Thread cancelDlgThread = new Thread (new ThreadStart(CancelDlgThread)); //cancelDlgThread.Start(); string nsPath = WmiHelper.MakeNSPath(server, curNSExpanded.Text); ConnectionOptions connectOpts = new ConnectionOptions( "", //locale this.connectAs, //username this.password, //password "", //authority ImpersonationLevel.Impersonate, AuthenticationLevel.Connect, true, //enablePrivileges null //context ); ManagementScope scope = (this.server == WmiHelper.DNS2UNC(Dns.GetHostName())) ? new ManagementScope(nsPath): new ManagementScope(nsPath, connectOpts); QueryOptions enumOpts = new QueryOptions(null, //context new TimeSpan(Int64.MaxValue), //timeout 50, //block size false, //non-rewindable true, //return immediately true, //use amended quals true, //ensure locatable false, //prototype only false //direct read ); ManagementObjectSearcher searcher = new ManagementObjectSearcher(scope, new ObjectQuery("select * from meta_class"), enumOpts); ManagementObjectCollection objs = searcher.Get(); ManagementObjectCollection.ManagementObjectEnumerator objEnum = objs.GetEnumerator(); while (objEnum.MoveNext() && !bCancelled) { ManagementClass obj = (ManagementClass)objEnum.Current; if (FilterPass(obj)) { TreeNode child = new TreeNode(obj.Path.RelativePath); curNSExpanded.Nodes.Add(child); //curClassList.Add(obj.Path.RelativePath, obj.Path.RelativePath); } } if (!bCancelled) { /* * for (int i = 0; i < curClassList.Count; i++) * { * * //TreeNode child = new TreeNode(curClassList.GetByIndex(i).ToString()); * //curNSExpanded.Nodes.Add(child); * } * * * cancelDlg.DialogResult = DialogResult.None; * cancelDlg.Hide(); * cancelDlg.Dispose(); * cancelDlg = null; */ } else { curClassList.Clear(); //re-set NS node: curNSExpanded.Nodes.Clear(); //show the node TreeNode dummy = new TreeNode(""); curNSExpanded.Nodes.Add(dummy); curNSExpanded.Collapse(); } this.OnNSExpandComplete(curNode.Text); this.Cursor = Cursors.Default; return(true); } catch (Exception e) { MessageBox.Show(WMISys.GetString("WMISE_Exception", e.Message, e.StackTrace)); this.Cursor = Cursors.Default; return(false); } }
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); } }
// private NewChildNode newChildNode = null; // // CONSTRUCTORS // // <doc> // <desc> // Main constructor. // Parameters: // string server (in, machine name) // string pathIn (in, WMI path without the server name , e.g. "root\default:MyClass") // </desc> // </doc> public WMIClassNode(string pathIn, string user, string pw) { try { path = pathIn; connectAs = user; password = pw; //parse the path to get server, namespace and class name Int32 separ = path.IndexOf("\\", 2); if (separ == -1) { //invalid path throw (new ArgumentException(WMISys.GetString("WMISE_InvalidPath", pathIn), "pathIn")); } serverName = path.Substring(2, separ - 2); pathNoServer = path.Substring(separ + 1, path.Length - separ - 1); //split pathNoServer into namespace and classname parts (':' is the separator) Int32 colon = pathNoServer.IndexOf(':', 0); if (colon == -1) { //invalid path throw (new ArgumentException(WMISys.GetString("WMISE_InvalidPath, pathIn"), "pathIn")); } nsName = pathNoServer.Substring(0, colon); className = pathNoServer.Substring(colon + 1, pathNoServer.Length - colon - 1); //Get and cache mgmtObj 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); mgmtObj = new ManagementClass(scope, new ManagementPath(path), new ObjectGetOptions(null, true) //use amended ); //NOTE: let's exercise the retrieved object to validate it //This will throw if object is invalid string classPath = mgmtObj.ClassPath.Path; } catch (Exception exc) { //MessageBox.Show(WMISys.GetString("WMISE_Exception", exc.Message, exc.StackTrace)); throw (exc); } }
/// <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); } }