/// <summary> /// Given an instance of an association object and a path to one of its /// endpoints (the "source"), this method returns the role of the other endpoint /// (the "target") in this association. /// </summary> /// <param name="assocInstance"> </param> /// <param name="sourcePath"> </param> static public String GetAssocTargetRole(ISWbemObject assocInstance, ISWbemObjectPath sourcePath) { try { IEnumerator enumAssocProps = ((IEnumerable)(assocInstance.Properties_)).GetEnumerator(); while (enumAssocProps.MoveNext()) { ISWbemProperty curProp = (ISWbemProperty)enumAssocProps.Current; if (curProp.CIMType != WbemCimtypeEnum.wbemCimtypeReference) { continue; } else { //confirm that this is not the source if ((String.Compare(curProp.get_Value().ToString(), sourcePath.Path, true)) != 0) { return(curProp.Name); } } } return(String.Empty); } catch (Exception e) { MessageBox.Show(WMISys.GetString("WMISE_Exception", e.Message, e.StackTrace)); return(String.Empty); } }
public WMIObjectPropertyTable(ISWbemObject wmiObjIn, PropertyFilters propFiltersIn, GridMode gridModeIn, bool showOperatorsIn, bool showOriginIn, bool showKeysIn) { try { if (wmiObjIn == null) { throw (new ArgumentNullException("wmiObj")); } wmiObj = wmiObjIn; propFilters = propFiltersIn; gridMode = gridModeIn; showOperators = showOperatorsIn; showOrigin = showOriginIn; showKeys = showKeysIn; Initialize(); } catch (Exception exc) { MessageBox.Show(WMISys.GetString("WMISE_Exception", exc.Message, exc.StackTrace)); } }
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); } }
static public String GetMethodDescription(String methName, ISWbemObject curObj) { try { ISWbemQualifier descrQual = null; ISWbemMethod verboseMeth = null; if (!curObj.Path_.IsClass) { ISWbemObject classObj = WmiHelper.GetClassObject(curObj); verboseMeth = classObj.Methods_.Item(methName, 0); } else { verboseMeth = curObj.Methods_.Item(methName, 0); } descrQual = verboseMeth.Qualifiers_.Item("Description", 0); return(descrQual.get_Value().ToString()); } catch (Exception) { //2880: removed message here return(""); } }
// <doc> // <desc> // Ctor from WMIPropertyDescriptor // </desc> // </doc> internal WMIPropertyDescriptor(ManagementObject mgmtObjIn, ManagementObject mgmtClassObjIn, ISWbemObject wbemObjIn, ISWbemObject wmiClassObjIn, String propNameIn, bool commitImmediately) : base(propNameIn, null) { if (wbemObjIn == null) { throw (new ArgumentNullException("wbemObjIn")); } if (propNameIn == string.Empty) { throw (new ArgumentNullException("propNameIn")); } wmiObj = wbemObjIn; wmiClassObj = wmiClassObjIn; mgmtObj = mgmtObjIn; mgmtClassObj = mgmtClassObjIn; propName = propNameIn; CommitImmediately = commitImmediately; Initialize(wbemObjIn); }
static public String GetPropertyDescription(String propName, ISWbemObject curObj) { try { if (curObj == null) { throw new ArgumentNullException("curObj"); } ISWbemProperty verboseProp = null; if (!curObj.Path_.IsClass) { ISWbemObject classObj = WmiHelper.GetClassObject(curObj); verboseProp = classObj.Properties_.Item(propName, 0); } else { verboseProp = curObj.Properties_.Item(propName, 0); } string descr = string.Empty; ISWbemQualifier descrQual = verboseProp.Qualifiers_.Item("Description", 0); descr = descrQual.get_Value().ToString(); return(descr); } catch (Exception) { return(""); } }
private void Initialize(ISWbemObject wbemObj) { try { ISWbemObjectPath path = (ISWbemObjectPath)wbemObj.Path_; //get the property ISWbemPropertySet props = (ISWbemPropertySet)wbemObj.Properties_; prop = props.Item(propName, 0); if (path.IsClass) { genus = 1; classProp = prop; } else //instance { genus = 2; classProp = ((ISWbemPropertySet)wmiClassObj.Properties_).Item(propName, 0); } mgmtProp = mgmtObj.Properties[propName]; mgmtClassProp = mgmtClassObj.Properties[propName]; } catch (Exception exc) { MessageBox.Show(WMISys.GetString("WMISE_Exception", exc.Message, exc.StackTrace)); } }
/// <summary> /// IsEvent /// </summary> /// <param name="obj"> </param> static public bool IsEvent(ISWbemObject obj) { try { //handle __ExtrinsicEvent class separately, since it is not its own parent if (obj.Path_.RelPath.ToString() == "__ExtrinsicEvent") { return(true); } Object[] arParents = (Object[])obj.Derivation_; for (int i = 0; i < arParents.Length; i++) { if (arParents[i].ToString() == "__ExtrinsicEvent") { return(true); } } return(false); } catch (Exception e) { MessageBox.Show(WMISys.GetString("WMISE_Exception", e.Message, e.StackTrace)); return(false); } }
// // CONSTRUCTORS // // <doc> // <desc> // Main constructor. // </desc> // </doc> public WMIInstanceNode(ISWbemObject instObj, ISWbemObject classObj) { 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 } wmiObj = instObj; wmiClassObj = classObj; ISWbemObjectPath wbemPath = wmiObj.Path_; path = wbemPath.Path; serverName = wbemPath.Server; nsName = wbemPath.Namespace; className = wbemPath.Class; IsNewInstance = (path == string.Empty); } catch (Exception exc) { MessageBox.Show(WMISys.GetString("WMISE_Exception", exc.Message, exc.StackTrace)); throw (exc); } }
// // 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); } }
public WMIEnumPropertyDescriptor(ManagementObject mgmtObjIn, ManagementObject mgmtClassObjIn, ISWbemObject wbemObjIn, ISWbemObject wmiClassObjIn, String propNameIn, bool commitImmediately) : base(mgmtObjIn, mgmtClassObjIn, wbemObjIn, wmiClassObjIn, propNameIn, commitImmediately) { }
private bool UpdateClassDescription(ClassListControls curControl) { string ns = string.Empty; string className = string.Empty; if (curControl == ClassListControls.ClassTreeView) { if (classList.SelectedNode != null) { ns = classList.SelectedNode.Parent.Text; className = classList.SelectedNode.Text; } } else //this is a selected class list { //see if this is a multiple selection (or nothing is selected) and clear description in this case if (selectedClassList.SelectedIndices.Count != 1) { descr.Text = string.Empty; return(true); } string relPath = selectedClassList.SelectedItem.ToString(); ns = relPath.Substring(0, relPath.IndexOf(":")); className = relPath.Substring(relPath.IndexOf(":") + 1); // MessageBox.Show(path); } ISWbemObject obj = WmiHelper.GetClassObject(machineName, ns, className); string theDescr = WmiHelper.GetClassDescription(obj); if (theDescr != string.Empty) { //Add special handling for newlines: change all "\n" to System.Environment.NewLine: int i = -1; string theRest = theDescr; string theStart = string.Empty; while ((i = theRest.IndexOf("\n")) >= 0) { theStart = theStart + theRest.Substring(0, i) + System.Environment.NewLine; theRest = theRest.Substring(i + 1); } theDescr = theStart + theRest; descr.Text = theDescr; return(true); } else { descr.Text = WMISys.GetString("WMISE_NoDescr"); return(false); } }
/// <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); } }
public WMIArrayConverter(ISWbemObject wbemObjIn, String propNameIn) { if (wbemObjIn == null || propName == string.Empty) { throw new ArgumentNullException(); } wbemObj = wbemObjIn; propName = propNameIn; }
public AssocGroupComponent(ISWbemObject assocInstance, ISWbemObject sourceObj) { sourceInst = sourceObj; associationPath = assocInstance.Path_.Path; associationClass = assocInstance.Path_.Class; //now, inspect assocInstance object to find target role and path targetClass = WmiHelper.GetAssocTargetClass(assocInstance, sourceObj.Path_); targetRole = WmiHelper.GetAssocTargetRole(assocInstance, sourceObj.Path_); }
/// <summary> /// IsAssociation /// </summary> /// <param name="obj"> </param> static public bool IsAssociation(ISWbemObject obj) { try { return(CheckObjectBoolQualifier(obj, "association")); } catch (Exception e) { MessageBox.Show(WMISys.GetString("WMISE_Exception", e.Message, e.StackTrace)); return(false); } }
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); } }
// <doc> // <desc> // Ctor from SWbemObject // </desc> // </doc> public WMIObjectComponent(ISWbemObject wbemObjIn, ISWbemObject wbemClassObjIn) { if (wbemObjIn == null) { throw (new ArgumentNullException("wbemObjIn")); } wmiObj = wbemObjIn; wmiClassObj = wbemClassObjIn; SystemPropertyDictionary = new Dictionary(NUM_OF_SYSTEM_PROPS); Initialize(); }
/// <summary> /// IsSystem /// </summary> /// <param name="obj"> </param> static public bool IsSystem(ISWbemObject obj) { try { String NameOut = obj.Path_.RelPath; return(NameOut.Substring(0, 2) == "__"); } catch (Exception e) { MessageBox.Show(WMISys.GetString("WMISE_Exception", e.Message, e.StackTrace)); return(false); } }
/// <summary> /// CheckObjectBoolQualifier /// </summary> /// <param name="obj"> </param> /// <param name="qualName"> </param> static private bool CheckObjectBoolQualifier(ISWbemObject obj, String qualName) { try { ISWbemQualifierSet qualSet = obj.Qualifiers_; ISWbemQualifier qual = (ISWbemQualifier)qualSet.Item(qualName, 0); return(Convert.ToBoolean(qual.get_Value())); } catch (Exception) { //NOTE that if the qualifier is not present, "Not found" will be returned //Return false in this case return(false); } }
private void OnO(Object source, EventArgs args) { try { if (gridIn != null) { gridIn.AcceptChanges(); } ISWbemObject objOut = wmiObj.ExecMethod_(meth.Name, inParms, 0, null); gridOut.WMIObject = objOut; } catch (Exception exc) { MessageBox.Show(WMISys.GetString("WMISE_Exception", exc.Message, exc.StackTrace)); } }
/// <summary> /// Given an instance of an association object and a path to one of its /// endpoints (the "source"), this method returns "path" to the other endpoint /// (the "target"). /// </summary> /// <param name="assocInstance"> </param> /// <param name="sourcePath"> </param> static public String GetAssocTargetClass(ISWbemObject assocInstance, ISWbemObjectPath sourcePath) { try { IEnumerator enumAssocProps = ((IEnumerable)(assocInstance.Properties_)).GetEnumerator(); while (enumAssocProps.MoveNext()) { ISWbemProperty curProp = (ISWbemProperty)enumAssocProps.Current; if (curProp.CIMType != WbemCimtypeEnum.wbemCimtypeReference) { continue; } else { //get CIMTYPE property qualifier value String refValue = curProp.Qualifiers_.Item("CIMTYPE", 0).get_Value().ToString(); //get rid of "ref:" prefix: refValue = refValue.Substring(4); //confirm that this is not the source if ((String.Compare(refValue, sourcePath.Class, true) != 0) && (String.Compare(refValue, sourcePath.Path, true) != 0)) { //if this is a path, leave only the class name, //which is the part after the last backslash char[] separ = new char[] { '\\' }; string[] pathParts = refValue.Split(separ); refValue = pathParts[pathParts.Length - 1]; return(refValue); } } } return(String.Empty); } catch (Exception e) { MessageBox.Show(WMISys.GetString("WMISE_Exception", e.Message, e.StackTrace)); return(String.Empty); } }
/// <summary> /// Property is not writeable only if "read" qualifier is present and its value is "true" /// Also, for dynamic classes, absence of "write" qualifier means that the property is read-only. /// </summary> /// <param name="obj"> </param> /// <param name="prop"> </param> static public bool IsPropertyWriteable(ISWbemObject obj, ISWbemProperty prop) { //collect all the info: bool isDynamic = CheckObjectBoolQualifier(obj, "dynamic"); bool hasWrite = CheckPropertyQualifierExistence(prop, "write"); bool writeValue = CheckPropertyBoolQualifier(prop, "write"); bool hasRead = CheckPropertyQualifierExistence(prop, "read"); bool readValue = CheckPropertyBoolQualifier(prop, "read"); if ((!isDynamic && !hasWrite && !hasRead) || (!isDynamic && hasWrite && writeValue) || (isDynamic && hasWrite && writeValue)) { return(true); } return(false); }
public static String GetDisplayName(ISWbemObject obj) { try { ISWbemObject verboseObj = obj; if (!obj.Path_.IsClass) { verboseObj = WmiHelper.GetClassObject(obj); } ISWbemQualifier dispName = verboseObj.Qualifiers_.Item("DisplayName", 0); return(dispName.get_Value().ToString()); } catch (Exception) { return(String.Empty); } }
static public String GetClassDescription(ISWbemObject obj) { try { ISWbemObject verboseObj = obj; if (!obj.Path_.IsClass) { verboseObj = WmiHelper.GetClassObject(obj); } ISWbemQualifier descrQual = verboseObj.Qualifiers_.Item("Description", 0); return(descrQual.get_Value().ToString()); } catch (Exception) { return(""); } }
public AssocGroupComponent(ISWbemObject sourceInstIn, String associationPathIn, String targetClassIn, String targetRoleIn) { sourceInst = sourceInstIn; associationPath = associationPathIn; //to get association class name from path, leave only //the part after the last backslash char[] separ = new char[] { '\\' }; string[] pathParts = associationPath.Split(separ); associationClass = pathParts[pathParts.Length - 1]; //MessageBox.Show("associationPath is " + associationPath); //MessageBox.Show("associationClass is " + associationClass); targetClass = targetClassIn; targetRole = targetRoleIn; }
static public bool IsPropertyWriteableWithTest(ISWbemObject obj, ISWbemProperty prop) { Object curValue = prop.get_Value(); prop.set_Value(ref curValue); int flags = (int)((int)WbemChangeFlagEnum.wbemChangeFlagUpdateSafeMode | (int)WbemChangeFlagEnum.wbemChangeFlagUpdateOnly | (int)WbemFlagEnum.wbemFlagUseAmendedQualifiers); try { obj.Put_(flags, null); return(true); } catch (Exception) { return(false); } }
/// <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); } }
// <doc> // <desc> // Ctor from WMIPropertyDescriptor // </desc> // </doc> internal WMISystemPropertyDescriptor(ISWbemObject wbemObjIn, String propNameIn, Object propValueIn ) : base(propNameIn, null) { if (wbemObjIn == null) { throw (new ArgumentNullException("wbemObjIn")); } if (propNameIn == string.Empty) { throw (new ArgumentException(WMISys.GetString("WMISE_PropDescr_NoProp"), "propNameIn")); } wmiObj = wbemObjIn; propValue = propValueIn; propName = propNameIn; }