public static ISWbemObject GetClassObject(String server, String ns, String className) { if (ns == string.Empty || className == string.Empty) { throw new ArgumentException(); } try { ISWbemLocator wbemLocator = WmiHelper.wbemLocator; //(ISWbemLocator)(new SWbemLocator()); ISWbemServices wbemServices = wbemLocator.ConnectServer(server, ns, "", //user: blank defaults to current logged-on user "", //password: blank defaults to current logged-on user "", //locale: blank for current locale "", //authority: NTLM or Kerberos. Blank lets DCOM negotiate. 0, //flags: reserved null); //context info: not needed here ISWbemObject classObj = wbemServices.Get(className, (int)WbemFlagEnum.wbemFlagUseAmendedQualifiers, null); return(classObj); } catch (Exception e) { MessageBox.Show(WMISys.GetString("WMISE_Exception", e.Message, e.StackTrace)); return(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) { try { path = pathIn; //parse the path to get server, namespace and class name Int32 separ = path.IndexOf("\\", 2); if (separ == -1) { //invalid path throw (new Exception("Invalid argument")); } 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 Exception("Invalid argument")); } nsName = pathNoServer.Substring(0, colon); className = pathNoServer.Substring(colon + 1, pathNoServer.Length - colon - 1); //Get and cache wmiObj ISWbemLocator wbemLocator = (ISWbemLocator)(new SWbemLocator()); wbemServices = wbemLocator.ConnectServer(serverName, nsName, "", //user: blank defaults to current logged-on user "", //password: blank defaults to current logged-on user "", //locale: blank for current locale "", //authority: NTLM or Kerberos. Blank lets DCOM negotiate. 0, //flags: reserved null); //context info: not needed here if (wbemServices == null) { throw new Exception("Could not connect to WMI"); } wmiObj = wbemServices.Get(className, 0, //(int)WbemFlagEnum.wbemFlagUseAmendedQualifiers, null); if (wmiObj == null) { throw new Exception("Could not get WMI object" + path); } } catch (Exception exc) { MessageBox.Show("Exception: " + exc.Message + "\n\rTrace: " + exc.StackTrace); throw (exc); } }
/// <summary> /// The object retrieves its serialization info. /// </summary> public WMIAssocGroupNode(SerializationInfo info, StreamingContext context) { try { try { rm = new ResourceManager("Microsoft.VSDesigner.WMI.Res", // Name of the resource. ".", // Use current directory. null); } catch (Exception) { //do nothing, will use static RM } //De-serialize string data mambers String associationPath = info.GetString("associationPath"); String targetClass = info.GetString("targetClass"); String targetRole = info.GetString("targetRole"); //De-serialize strings necessary to restore the source object String sourcePath = info.GetString("sourcePath"); String sourceNS = info.GetString("sourceNS"); String sourceServer = info.GetString("sourceServer"); //Restore SWbemObject for "source" ISWbemLocator wbemLocator = WmiHelper.WbemLocator; //(ISWbemLocator)(new SWbemLocator()); ISWbemServices wbemServices = wbemLocator.ConnectServer(sourceServer, sourceNS, "", //user: blank defaults to current logged-on user "", //password: blank defaults to current logged-on user "", //locale: blank for current locale "", //authority: NTLM or Kerberos. Blank lets DCOM negotiate. 0, //flags: reserved null); //context info: not needed here ISWbemObject sourceInst = wbemServices.Get(sourcePath, (int)WbemFlagEnum.wbemFlagUseAmendedQualifiers, null); theComp = new AssocGroupComponent(sourceInst, associationPath, targetClass, targetRole); } catch (Exception exc) { MessageBox.Show(WMISys.GetString("WMISE_Exception", exc.Message, exc.StackTrace)); throw (exc); } }
private bool ShowClasses(TreeNode curNode) { //show hourglass Cursor curSave = classList.Cursor; classList.Cursor = Cursors.WaitCursor; try { //check that this is a namespace: if (curNode.Parent != null) { return(false); } curNode.Nodes.Clear(); ISWbemServices services = (ISWbemServices)wbemLocator.ConnectServer(machineName, curNode.Text, "", "", "", "", 0, null); ISWbemObjectSet objSet = services.SubclassesOf("", 0, null); IEnumerator eClasses = ((IEnumerable)objSet).GetEnumerator(); SortedList classNameList = new SortedList(100); while (eClasses.MoveNext()) { ISWbemObject obj = (ISWbemObject)eClasses.Current; string childName = FilterPass(obj); if (childName != string.Empty) { classNameList.Add(childName, childName); } } for (int i = 0; i < classNameList.Count; i++) { TreeNode child = new TreeNode(classNameList.GetByIndex(i).ToString()); curNode.Nodes.Add(child); } classList.Cursor = curSave; return(true); } catch (Exception e) { MessageBox.Show(WMISys.GetString("WMISE_Exception", e.Message, e.StackTrace)); classList.Cursor = curSave; return(false); } }
/// <summary> /// The object retrieves its serialization info. /// </summary> public ExtrinsicEventQueryNode(SerializationInfo info, StreamingContext context) { try { try { rm = new ResourceManager("Microsoft.VSDesigner.WMI.Res", // Name of the resource. ".", // Use current directory. null); } catch (Exception) { //do nothing, will use static RM } serverName = info.GetString("Server"); nsName = info.GetString("NS"); query = info.GetString("Query"); state = SubscriptionState.Stopped; rm = new ResourceManager("Microsoft.VSDesigner.WMI.Res", // Name of the resource. ".", // Use current directory. null); //Get and cache wbemServices ISWbemLocator wbemLocator = WmiHelper.WbemLocator; //(ISWbemLocator)(new SWbemLocator()); wbemServices = wbemLocator.ConnectServer(serverName, nsName, "", //user: blank defaults to current logged-on user "", //password: blank defaults to current logged-on user "", //locale: blank for current locale "", //authority: NTLM or Kerberos. Blank lets DCOM negotiate. 0, //flags: reserved null); //context info: not needed here if (wbemServices == null) { throw new Exception(WMISys.GetString("WMISE_WMIConnectFailed")); } browseObject = new ExtrinsicEventQueryComponent(serverName, nsName, query, this); } catch (Exception exc) { MessageBox.Show(WMISys.GetString("WMISE_Exception", exc.Message, exc.StackTrace)); throw (exc); } }
/// <summary> /// This is the method to call in case you cannot QI for ISWbemObject /// on the cached SWbemObject (bug in interop? urt?) /// </summary> /// <param name="objPath"> </param> /// <param name="services"> propriately</param> /// <param name="serverName"> </param> /// <param name="nsName"> </param> static public ISWbemObject GetObject(string objPath, ISWbemServices services, //in - out string serverName, string nsName) { try { try { ISWbemSecurity sec = services.Security_; } catch (Exception) { //MessageBox.Show ("refreshing services in wmihelper"); services = null; services = WbemLocator.ConnectServer(serverName, nsName, string.Empty, string.Empty, string.Empty, string.Empty, 0, null); } ISWbemObject obj = services.Get(objPath, (int)WbemFlagEnum.wbemFlagUseAmendedQualifiers, null); //MessageBox.Show ("refreshing object in wmihelper"); return(obj); } catch (Exception e) { MessageBox.Show(WMISys.GetString("WMISE_Exception", e.Message, e.StackTrace)); return(null); } }
public static ISWbemObject GetClassObject(ISWbemObject objIn) { if (objIn == null) { throw new ArgumentException(); } try { ISWbemObjectPath path = objIn.Path_; if (path.IsClass) { return(objIn); } ISWbemLocator wbemLocator = WmiHelper.wbemLocator; //(ISWbemLocator)(new SWbemLocator()); ISWbemServices wbemServices = wbemLocator.ConnectServer(path.Server, path.Namespace, "", //user: blank defaults to current logged-on user "", //password: blank defaults to current logged-on user "", //locale: blank for current locale "", //authority: NTLM or Kerberos. Blank lets DCOM negotiate. 0, //flags: reserved null); //context info: not needed here ISWbemObject classObj = wbemServices.Get(path.Class, (int)WbemFlagEnum.wbemFlagUseAmendedQualifiers, null); return(classObj); } catch (Exception e) { //MessageBox.Show(WMISys.GetString("WMISE_Exception", e.Message, e.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) { try { try { rm = new ResourceManager("Microsoft.VSDesigner.WMI.Res", // Name of the resource. ".", // Use current directory. null); } catch (Exception) { //do nothing, will use static RM } path = pathIn; //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 wmiObj ISWbemLocator wbemLocator = WmiHelper.WbemLocator; //(ISWbemLocator)(new SWbemLocator()); wbemServices = wbemLocator.ConnectServer(serverName, nsName, "", //user: blank defaults to current logged-on user "", //password: blank defaults to current logged-on user "", //locale: blank for current locale "", //authority: NTLM or Kerberos. Blank lets DCOM negotiate. 0, //flags: reserved null); //context info: not needed here wmiObj = wbemServices.Get(className, (int)WbemFlagEnum.wbemFlagUseAmendedQualifiers, null); } catch (Exception exc) { //MessageBox.Show(WMISys.GetString("WMISE_Exception", exc.Message, exc.StackTrace)); throw (exc); } }
private bool EnumNamespaces(String parent, int num) //recursively adds namespaces to the drop-down box { try { try { wbemServices = wbemLocator.ConnectServer(machineName, parent, "", "", "", "", 0, null); } catch (Exception e) { //could not connect to the namespace //The most common cause is "Access denied" if (parent.ToLower() == "root") { //could not connect to Root, put error description text in the treeview TreeNode errNode = new TreeNode(e.Message); classList.Nodes.Add(num, errNode); } return(false); } //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(); classList.Nodes.Add(num, nsNode); ISWbemObjectSet objSet = wbemServices.InstancesOf("__NAMESPACE", 0, null); IEnumerator eInstances = ((IEnumerable)objSet).GetEnumerator(); while (eInstances.MoveNext()) { num++; ISWbemObject obj = (ISWbemObject)eInstances.Current; ISWbemPropertySet props = (ISWbemPropertySet)obj.Properties_; string NameOut = ""; string curName = props.Item("Name", 0).get_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); } }